A. Kids Seating
这题是一题很简单的思维题。n~2n-1之间的数必然不会互相被整除,所有数字乘以2也就可以满足gcd不为1的条件,且范围在4n内。
#include "bits/stdc++.h"
using namespace std;
int main(void) {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
printf("%d ", 4 * n - 2 * i);
}
cout << endl;
}
return 0;
}
B. Saving the City
把并列的分块,再判断选择放炸弹还是分开炸。就是需要注意一下两个特判。
#include "bits/stdc++.h"
using namespace std;
const int MAXSIZE = 1e5 + 5;
typedef struct {
int before = 0;
int num = 0;
}mine;
mine mines[MAXSIZE];
int main(void) {
int t;
cin >> t;
while (t--) {
memset(mines, 0,sizeof(mines));
int a, b;
cin >> b >> a;
string str;
cin >> str;
int i = 0, j = 0;
while (i < str.length()) {
while (i < str.length() && str[i] == '0') {
++i;
mines[j].before++;
}
while (i < str.length() && str[i] == '1') {
mines[j].num++;
++i;
}
++j;
}
if (j == 1 && mines[0].num == 0) {
cout << 0 << endl;
continue;
}
int cnt = b;
for (int k = 1; k < j - 1; ++k) {
cnt += min(mines[k].before * a, b);
}
if (mines[j - 1].num > 0 && j > 1) {
cnt += min(mines[j - 1].before * a, b);
}
cout << cnt << endl;
}
return 0;
}
C. The Delivery Dilemma
先按照外卖的所需时间排序,最大值即位所有外卖送达所需的时间,随着外卖时间的减少所需要跑腿的时间增多,使二者尽量接近对方,在比对其中的最小值即为答案。
#include "bits/stdc++.h"
using namespace std;
const int MAXSIZE = 2 * 1e5 + 5;
struct tempStruct {
int a, b;
}places[MAXSIZE];
bool cmp(tempStruct a, tempStruct b) {
return a.a > b.a;
}
int main(void) {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", &places[i].a);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &places[i].b);
}
sort(places, places + n, cmp);
int sum = 0, k = 0;
while (places[k].a >= sum && k < n) {
sum += places[k++].b;
}
int ans = min(sum, places[k-1].a);
cout << ans << endl;
}
return 0;
}
D. Extreme Subtraction
这题是一题差分题,判断第一个数字是否大于后面所有负数差分和的绝对值即可。
#include "bits/stdc++.h"
using namespace std;
const int MAXSIZE = 30005;
int a[MAXSIZE], d[MAXSIZE];
int main(void) {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
d[0] = a[0];
int sum = 0;
for (int i = 1; i < n; ++i) {
d[i] = a[i] - a[i - 1];
if (d[i] < 0) sum += d[i];
}
if (sum + d[0] >= 0) {
cout << "YES" << endl;
}else {
cout << "NO" << endl;
}
}
return 0;
}