A. Level Statistics
Subject
t represents the number of test groups, n represents the number of data groups of the game, and judges whether each test data is reasonable.
Ideas
- Must satisfy p [i], c [i] are non-decreasing.
- At every moment, p [i]> = c [i].
- There must be a change in p [i] that must be greater than or equal to c [i]
Code
//Powered by CK 2020:04:11
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int p[N], c[N], n;
void fun() {
int flag = 0;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> p[i] >> c[i];
if(c[i] > p[i]) flag = 1;
}
if(flag) {
cout << "NO" << endl;
return ;
}
for(int i = 1; i < n; i++) {
if(p[i] < p[i - 1] || c[i] < c[i - 1] || p[i] - p[i - 1] < c[i] - c[i - 1]) {
cout << "NO" << endl;
return ;
}
}
cout << "YES" << endl;
return ;
}
int main() {
// freopen("in.txt", "r", stdin);
int t;
cin >> t;
while(t--) fun();
return 0;
}
B. Middle Class
Subject
You can take any number and make it evenly divided to get the maximum number that satisfies the value greater than or equal to \ (x \) .
Ideas
When reading in, record the number of numbers greater than or equal to \ (x \) and the sum of greater than \ (x \) , and record the number of differences less than 0. Sort these numbers from largest to smallest, and then enumerate them one by one, continuously subtract the sum of the previous records, the sum is greater than or equal to 0, ans ++, when the sum is less than 0, directly break the output answer
Code
//Powered by CK 2020:04:11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll down[N], n1, n, x, ans;
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
ll t, temp;
cin >> t;
while(t--) {
n1 = ans= 0;
cin >> n >> x;
ll sum = 0;
for(ll i = 0; i < n; i++) {
cin >> temp;
temp -= x;
if(temp >= 0) {
ans++;
sum += temp;
}
else down[n1++] = -temp;
}
sort(down, down + n1);
for(ll i = 0; i < n1; i++) {
sum -= down[i];
if(sum >= 0) ans++;
else break;
}
cout << ans << endl;
}
return 0;
}
C. Circle of Monsters
Ideas
First get the value of a [i]-b [(i + n)-1% n] for an entire sequence, then find the minimum value as the answer by enumerating the starting point
Code
//Powered by CK 2020:04:11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
ll a[N], b[N], cost[N];
int n;
int main() {
// freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
ll sum = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%lld %lld", &a[i], &b[i]);
if(i) cost[i] = max((ll)0, a[i] - b[i - 1]), sum += cost[i];
}
cost[0] = max((ll)0, a[0] - b[n - 1]);
sum += cost[0];
ll ans = sum - cost[0] + a[0];
for(int i = 0; i < n; i++)
ans = min(ans, sum - cost[i] + a[i]);
printf("%lld\n", ans);
}
return 0;
}
D. Minimum Euler Cycle
Ideas
** Find the position of l ~ r in the smallest lexicographic order of a complete graph traversal. We can find that the minimum lexicographic order is obviously as follows
1 2 1 3 1 4 1 5 ………… 1 (n - 1) 1 n
2 3 2 4 2 5 ………… 2 (n - 1) 2 n
……………………………………………………………………
…………………………………………………………
………………………………………………
……………………………………
…………………………
(n - 1) n
1**
** There are a total of \ (n (n-1) + 1 \) numbers, the last line is special, there is only one **
So we only need to enumerate these numbers with a loop, the last number is specially judged.
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, l, r;
int main() {
// freopen("in.txt", "r", stdin);
int t;
cin >> t;
while(t--) {
scanf("%lld %lld %lld", &n, &l, &r);
ll sum = 1;
for(ll i = 1; i <= n; i++) {
// cout << i << endl;
if(sum + (n - i) * 2 <= l) {
sum += (n - i) * 2;
continue;
}
for(ll j = i + 1; j <= n; j++) {
if(sum >= l && sum <= r)
printf("%lld ", i);
sum++;
if(sum >= l && sum <= r)
printf("%lld ", j);
sum++;
if(sum > r) break;
}
if(sum > r) break;
}
if(sum <= r) printf("1 ");
printf("\n");
}
return 0;
}