
📢📢📢传送门
A. Preparing for the Olympiad
Monocarp and Stereocarp are preparing for the Olympiad. There are n n n days left until the Olympiad. On the i i i-th day, if Monocarp plans to practice, he will solve a i a_i ai problems. Similarly, if Stereocarp plans to practice on the same day, he will solve b i b_i bi problems.
Monocarp can train on any day he wants. However, Stereocarp watches Monocarp and follows a different schedule: if Monocarp trained on day i i i and KaTeX parse error: Expected 'EOF', got '&' at position 3: i &̲lt; n, then Stereocarp will train on day ( i + 1 ) (i+1) (i+1).
Monocarp wants to organize his training process in a way that the difference between the number of problems he solves and the number of problems Stereocarp solves is as large as possible. Formally, Monocarp wants to maximize the value of ( m − s ) (m-s) (m−s), where m m m is the number of problems he solves, and s s s is the number of problems Stereocarp solves. Help Monocarp determine the maximum possible difference in the number of solved problems between them.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 3 1 \le t \le 10^3 1≤t≤103) — the number of test cases.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1≤n≤100).
The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 100 1 \le a_i \le 100 1≤ai≤100).
The third line contains n n n integers b 1 , b 2 , … , b n b_1, b_2, \dots, b_n b1,b2,…,bn ( 1 ≤ b i ≤ 100 1 \le b_i \le 100 1≤bi≤100).
Output
For each test case, print a single integer — the maximum possible difference between the number of problems Monocarp solves and the number of problems Stereocarp solves.
解题思路及AC代码
签到题
#include<bits/stdc++.h>
#define int long long
using namespace std;
using i64 = long long;
const int N = 2e5 + 10;
void solve(){
int n;
cin >> n;
vector<int> a(n),b(n);
for(int i = 0;i < n;i ++){
cin >> a[i];
}
for(int i = 0;i < n;i ++){
cin >> b[i];
}
int ans = a[n - 1];
for(int i = 0;i < n - 1;i ++){
if(a[i] > b[i + 1]){
ans += (a[i] - b[i + 1]);
}
}
cout << ans << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while(t --){
solve();
}
}
B. Journey
Monocarp decided to embark on a long hiking journey.
He decided that on the first day he would walk a a a kilometers, on the second day he would walk b b b kilometers, on the third day he would walk c c c kilometers, on the fourth day, just like on the first, he would walk a a a kilometers, on the fifth day, just like on the second, he would walk b b b kilometers, on the sixth day, just like on the third, he would walk c c c kilometers, and so on.
Monocarp will complete his journey on the day when he has walked at least n n n kilometers in total. Your task is to determine the day on which Monocarp will complete his journey.
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
Each test case consists of one line containing four integers n n n, a a a, b b b, c c c ( 1 ≤ n ≤ 1 0 9 1 \le n \le 10^9 1≤n≤109; 1 ≤ a , b , c ≤ 1 0 6 1 \le a, b, c \le 10^6 1≤a,b,c≤106).
Output
For each test case, output one integer — the day on which Monocarp will have walked at least n n n kilometers in total and will complete his journey.
解题思路及AC代码
签到题
#include<bits/stdc++.h>
#define int long long
using namespace std;
using i64 = long long;
const int N = 2e5 + 10;
void solve(){
int n,a,b,c;
cin >> n >> a >> b >> c;
int res = a + b + c;
int cnt = n / res;
cnt *= 3;
n %= res;
if(n){
n -= a;
cnt ++;
}
if(n > 0){
n -= b;
cnt ++;
}
if(n > 0){
n -= c;
cnt ++;
}
cout << cnt << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while(t --){
solve();
}
}
C. Preparing for the Exam
Monocarp is preparing for his first exam at the university. There are n n n different questions which can be asked during the exam, numbered from 1 1 1 to n n n. There are m m m different lists of questions; each list consists of exactly n − 1 n-1 n−1 different questions. Each list i i i is characterized by one integer a i a_i ai, which is the index of the only question which is not present in the i i i-th list. For example, if n = 4 n = 4 n=4 and a i = 3 a_i = 3 ai=3, the i i i-th list contains questions [ 1 , 2 , 4 ] [1, 2, 4] [1,2,4].
During the exam, Monocarp will receive one of these m m m lists of questions. Then, the professor will make Monocarp answer all questions from the list. So, Monocarp will pass only if he knows all questions from the list.
Monocarp knows the answers for k k k questions q 1 , q 2 , … , q k q_1, q_2, \dots, q_k q1,q2,…,qk. For each list, determine if Monocarp will pass the exam if he receives that list.
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
Each test case consists of three lines:
- the first line contains three integers n n n, m m m and k k k ( 2 ≤ n ≤ 3 ⋅ 1 0 5 2 \le n \le 3 \cdot 10^5 2≤n≤3⋅105; 1 ≤ m , k ≤ n 1 \le m, k \le n 1≤m,k≤n);
- the second line contains m m m distinct integers a 1 , a 2 , … , a m a_1, a_2, \dots, a_m a1,a2,…,am ( 1 ≤ a i ≤ n 1 \le a_i \le n 1≤ai≤n; KaTeX parse error: Expected 'EOF', got '&' at position 5: a_i &̲lt; a_{i+1});
- the third line contains k k k distinct integers q 1 , q 2 , … , q k q_1, q_2, \dots, q_k q1,q2,…,qk ( 1 ≤ q i ≤ n 1 \le q_i \le n 1≤qi≤n; KaTeX parse error: Expected 'EOF', got '&' at position 5: q_i &̲lt; q_{i+1}).
Additional constraints on the input:
- the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3⋅105.
Output
For each test case, print a string of m m m characters. The i i i-th character should be 1 if Monocarp passes the exam if he receives the i i i-th question list, 0 if Monocarp won’t pass.
解题思路及AC代码
简单构造
#include<bits/stdc++.h>
#define int long long
using namespace std;
using i64 = long long;
const int N = 2e5 + 10;
void solve(){
int n,m,k;
cin >> n >> m >> k;
vector<int> a(m),b(n);
for(int i = 0;i < m;i ++){
cin >> a[i];
}
int cnt = n - k;
for(int i = 0;i < k;i ++){
int x;
cin >> x;
x --;
b[x] = 1;
}
for(int i = 0;i < m;i ++){
a[i] --;
if((!b[a[i]] && cnt == 1) || cnt == 0){
cout << "1";
}else{
cout << "0";
}
}
cout << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while(t --){
solve();
}
}
D. Counting Pairs
You are given a sequence a a a, consisting of n n n integers, where the i i i-th element of the sequence is equal to a i a_i ai. You are also given two integers x x x and y y y ( x ≤ y x \le y x≤y).
A pair of integers ( i , j ) (i, j) (i,j) is considered interesting if the following conditions are met:
- KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le i &̲lt; j \le n;
- if you simultaneously remove the elements at positions i i i and j j j from the sequence a a a, the sum of the remaining elements is at least x x x and at most y y y.
Your task is to determine the number of interesting pairs of integers for the given sequence a a a.
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
Each test case consists of two lines:
- The first line contains three integers n , x , y n, x, y n,x,y ( 3 ≤ n ≤ 2 ⋅ 1 0 5 3 \le n \le 2 \cdot 10^5 3≤n≤2⋅105, 1 ≤ x ≤ y ≤ 2 ⋅ 1 0 14 1 \le x \le y \le 2 \cdot 10^{14} 1≤x≤y≤2⋅1014);
- The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^{9} 1≤ai≤109).
Additional constraint on the input: the sum of n n n across all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.
Output
For each test case, output one integer — the number of interesting pairs of integers for the given sequence a a a.
解题思路及AC代码
二分找到x,y的边界值,建议使用STL中的lower_bound,不然边界问题很容易出问题,我这里就是手写的二分调了半天。
#include<bits/stdc++.h>
#define int long long
using namespace std;
using i64 = long long;
const int N = 2e5 + 10;
void solve(){
int n,x,y;
cin >> n >> x >> y;
int ans = 0;
vector<int> a(n + 1);
int sum = 0;
for(int i = 1;i <= n;i ++){
cin >> a[i];
sum += a[i];
}
// cout << sum << "\n";
sort(a.begin() + 1,a.end());
for(int i = 1;i < n;i ++){
int res = sum;
res -= a[i];
if(res <= x){
continue;
}
int l = i + 1,r = n;
while(l < r){
int mid = l + r + 1 >> 1;
res -= a[mid];
if(res < x){
r = mid - 1;
}else{
l = mid;
}
res += a[mid];
}
res = sum - a[i];
int l1 = i + 1,r1 = n;
while(l1 < r1){
int mid = l1 + r1 >> 1;
res -= a[mid];
if(res > y){
l1 = mid + 1;
}else{
r1 = mid;
}
res += a[mid];
}
// cout << l << " " << l1 << "\n";
if(l > l1 || (l == l1 && res - a[l] >= x && res - a[l] <= y))
ans += abs(l - l1) + 1;
}
cout << ans << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while(t --){
solve();
}
}
444



被折叠的 条评论
为什么被折叠?



