A:
把n个硬币分给三个人,判断三个人最后的硬币能不能相同。
先将原本三个人有的硬币abc取最大值,想要相同最起码少的两个人要达到最大值,之后,三人一人一个,对3取模即可。
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int a,b,c,n;
cin >> a >> b >> c >> n;
int x = max(a,b);
x = max(x, c);
n -= x - a;
n -= x - b;
n -= x - c;
if(n >= 0 && n % 3 == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
B:
一个机器人,只可以向上向右移动,判断能不能经过所有点。
先把所有点按y排序,再按x排序,有一个大概的经过顺序。
遍历所有点,如果当前点比后面的点x大,或者y大,都无法再回去,不可以。
记录走向即可。
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 2e3 + 50;
struct node
{
int x;
int y;
}a[maxn];
bool cmp(node a, node b)
{
if(a.y != b.y)
return a.y < b.y;
else
return a.x < b.x;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int flag = 0;
int s[maxn] = {0};
for(int i = 0; i < n; i++)
{
cin >> a[i].x >> a[i].y;
}
sort(a, a + n, cmp);
int cnt = 0;
for(int j = 0; j < a[0].x; j++)
{
s[cnt] = 1;
cnt++;
}
for(int j = 0; j < a[0].y; j++)
{
s[cnt] = 2;
cnt++;
}
for(int i = 1; i < n; i++)
{
if(a[i].x < a[i-1].x || a[i].y < a[i-1].y)
{
//cout << a[i].x << " " << a[i].y << " " << a[i-1].x << " " << a[i-1].y << endl;
flag = 1;
break;
}
for(int j = 0; j < a[i].x - a[i-1].x; j++)
{
s[cnt] = 1;
// cout << "执行";
cnt++;
}
for(int j = 0; j < a[i].y - a[i-1].y; j++)
{
// cout << "ZHIXNG";
s[cnt] = 2;
cnt++;
}
}
if(flag)
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
for(int i = 0; i < cnt; i++)
{
if(s[i] == 1)
cout << "R";
else if(s[i] == 2)
cout << "U";
}
cout << endl;
}
}
return 0;
}
C:
找三个不同的数abc,使得abc=n
从2开始三次方暴力,要求三个数互不相同。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int a = 0,b = 0,c = 0;
for(int i = 2; i*i*i <= n; i++)
{
if(n % i == 0)
{
a = i;
n /= i;
break;
}
}
for(int i = a + 1; i*i <= n; i++)
{
if(n % i == 0 && n / i != a && n / i != i && n / i > 1)
{
b = i;
c = n / i;
break;
}
}
if(a && b && c)
{
cout << "YES" << endl;
cout << a << " " << b << " " << c << endl;
}
else
cout <<"NO" << endl;
}
return 0;
}
D:
给你一个数组,定义它里面没有出现的最小非负整数。
可以对任意元素进行+x或-x操作。最大化这个数组中没出现的最小非负整数
对于每个数都可以进行+x或者-x。
所以每个数只需要记录a[i]%x即可。
遍历即可(妙啊)。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 4e5 + 50;
int a[maxn] = {0};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q, x;
cin >> q >> x;
int y, t = 0, ans = 0;
for(int i = 0; i < q; i++)
{
cin >> y;
a[y%x]++;
while(a[t] > 0)
{
a[t]--;
t = (t+1)%x;
ans++;
}
cout << ans << endl;
}
return 0;
}
E:
给你一个矩阵,问进行多少个操作可以是每个矩阵元素是它应该的数。
两种操作。
所以,我先对每一列进行遍历:
对一列进行记录:我不转有多少个满足的,我转1下有多少个满足的,我转2下有多少个满足的…
取最多满足的个数,剩下不满足的一个个进行赋值。
记录(转数+赋值数),取最小值,就是这一列的最小操作数。
对所有列都进行上述操作,求和即可。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 50;
int a[maxn];
int b[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, ans = 0;
cin >> n >> m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> a[i*n+j];
}
}
for(int j = 0; j < m; j++)
{
for(int i = 0; i < n; i++)
b[i] = 0;
for(int i = 0; i < n; i++)
{
if(a[i*n+j] <= n*m && (a[i*n+j]-j-1)%m==0)
{
int t = (a[i*n+j] - j - 1) / m;
int wei = (i-t+n) % n;
b[wei]++;
// cout << wei << endl;
}
}
int sum = 1e9;
for(int i = 0; i < n; i++)
{
sum = min(sum, n-b[i]+i);
}
cout <<sum << endl;
ans += sum;
}
cout << ans << endl;
return 0;
}