Problem - A - Codeforces
解题思路:
对每个需要的数字进行计数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int two = 2;
int zero = 3;
int five = 1;
int three = 1;
int one = 1;
int flag = 0;
int ok = 0;
for (int i = 0; i < n; i++)
{
int now;
cin >> now;
if (now == 2&&two>0)
{
two--;
}
if (now == 0 && zero > 0)
{
zero--;
}
if (now == 5 && five > 0)
{
five--;
}
if (now == 1 && one > 0)
{
one--;
}
if (now == 3 && three > 0)
{
three--;
}
if (two + zero + five + one+three == 0&&flag==0)
{
flag = 1;
ok = i+1;
}
}
if (flag == 1)
{
cout << ok << endl;
}
else
cout << 0 << endl;
}
}
Problem - B - Codeforces
解题思路:
所有人进行排序,对于大于等于x的人们,单独一队,不足的抱团看看最低能力*数量是否满足
#include <bits/stdc++.h>
using namespace std;
int n, t;
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n, x;
cin >> n >> x;
vector<int>arr(n);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr.begin(), arr.end(),cmp);
int alone = 0;
int j = -1;
for (int i = 0; i < n; i++)
{
if (arr[i] >= x)
{
alone++;
}
else
{
j = i;
break;
}
}
int I = 1;
if (j == -1)
{
goto there;
}
while (1)
{
if (I * arr[j] >= x)
{
alone++;
I = 1;
j++;
}
else
{
I++;
j++;
}
if (j >= n)
{
break;
}
}
there:
cout << alone << endl;
}
}
Problem - C - Codeforces
解题思路:
对于偶数个数,是不可能实现的,
对于奇数个数,如果我把所有数字逆序排放,是不是就成功了?
确实是的,就这么解了,但是原理呢?
放当前数要刚好离他目标位置差x个,放一个,x++,就实现了移动一次,只有一个数在原位
来看代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n % 2 == 0) {
cout << "-1\n";
continue;
}
vector<int> ans(n+1);
int now = n;
for (int i = 0; i < n; i++)
{
ans[i] = (now - i + n) % n;
if (ans[i] == 0)
ans[i] = n;
cout << ans[i] << " ";
}
cout << endl;
}
return 0;
}
Problem - D - Codeforces
解决方案:
把桌子平均分给每一层,向上取整,模拟最差情况,只讨论最差情况的第一层。
每层容量m,若最差情况是放x个。那么第一层有m-x个空余,这m-x空余位就插入x中间,尽量隔开他们。
来看代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--)
{
int n, m, k;
cin >> n >> m >> k;
int now = (k + n- 1) / n;//每行放多少桌子
int ok = m - now+1;//一行被桌子分成几部分
int sum = (now + ok - 1) / ok;
cout << sum << endl;
}
}