链接: link
A、Plus One on the Subset
思路:差分排序就好了
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 9;
int n, a[N];
bool cmp(int a, int b) {
return a > b;
}
void solve() {
sort(a + 1, a + 1 + n, cmp);
int ok = 0, f = 0;
for(int i = 2; i <= n; i++) {
if(!f && a[i] != a[i - 1]) {
f = 1;
}
ok += abs(a[i] - a[i - 1]);
}
printf("%lld\n", ok);
}
signed main() {
int t;
scanf("%lld", &t);
while(t--) {
scanf("%lld", &n);
for(int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
solve();
}
return 0;
}
B、Make AP
思路:等差数列的性质 a a+d a+2d
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 9;
int a, b, c;
void solve() {
int x = 2 * b;
if(a + c == x) {
puts("YES");
return;
}
if(a + c >= x) {
if((a + c) % x == 0) puts("YES");
else puts("NO");
} else {
if((x - a) % c == 0 || (x - c) % a == 0) puts("YES");
else puts("NO");
}
}
signed main() {
int t;
scanf("%lld", &t);
while(t--) {
scanf("%lld%lld%lld", &a, &b, &c);
solve();
}
return 0;
}
C、Division by Two and Permutation
思路:将小于n的都添加,然后大于的不断暴力,看是否满足1-n都出现了一次
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 9;
int n;
void solve() {
unordered_map<int, int> vis;
vector<int> a;
for(int i = 0; i < n; i++) {
int x;
scanf("%lld", &x);
if(x <= n && vis[x] == 0) vis[x]++;
else {
a.push_back(x);
}
}
int ok = 0;
if(!a.empty()) {
for(int i = 0; i < a.size(); i++) {
while(a[i] > n || (a[i] <= n && vis[a[i]] > 0)) {
a[i] >>= 1;
}
if(a[i] == 0) {
ok = -1;
break;
} else {
vis[a[i]]++;
}
}
}
for(int i = 1; i <= n; i++) {
if(ok == -1||!vis[i]) ok = -1;
}
if(ok) puts("NO");
else puts("YES");
}
signed main() {
int t;
scanf("%lld", &t);
while(t--) {
scanf("%lld", &n);
solve();
}
return 0;
}
D、Palindromes Coloring
思路:二分
#include <bits/stdc++.h>
using namespace std;
int t, n, k, ok;
char s[200010];
int cnt[100];
bool check(int x) {
int tmp = ok, t = n - ok * 2;//对数 以及剩下的个数
for(int i = 1; i <= k; i++) {
tmp -= x / 2;
if(x & 1)t -= 1;
}
if(tmp < 0 || t + tmp * 2 < 0) return false;
return true;
}
signed main() {
scanf("%d", &t);
while(t-- && scanf("%d%d", &n, &k)) {
scanf("%s", s);
for(int i = 0; i < n; i++) cnt[s[i] - 'a']++;
for(int i = 0; i < 26; i++) {
ok += cnt[i] / 2;
}
// cout<<"ok"<<ok<<endl;
memset(cnt, 0, sizeof cnt);
int l = 1, r = n;
while(l < r) {
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
printf("%d\n", l);
ok = 0;
}
return 0;
}