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;
}