Posted onEdited onInACMWord count in article: 3.7kReading time ≈14 mins.
人与人的差距。。。
补下题,正好 12 月 8 号要去考 CCF-CSP
A. Line Breaks
B. Transfusion
C. Uninteresting Number
D. Digital string maximization
E. Three Strings
F. Maximum modulo equality
G. Tree Destruction
Codeforces
Round 991 (Div. 3)
A. Line Breaks
Description
Kostya has a text consisting
of words made up of Latin
alphabet letters. He also has two strips on which he must write the
text. The first strip can hold
characters, while the second can hold as many as needed.
Kostya must choose a number
and write the first words from
on the first strip, while all the
remaining words are written on the second strip. To save space, the
words are written without gaps, but each word must be entirely on one
strip.
Since space on the second strip is very valuable, Kostya asks you to
choose the maximum possible number such that all words fit on the first strip
of length .
Input
The first line contains an integer — the number of test cases.
The first line of each test case contains two integers and — the number of words in the list and
the maximum number of characters that can be on the first strip.
The next lines contain one
word of lowercase Latin letters
where the length of does not
exceed .
Output
For each test case, output the maximum number of words such that the first words have a total length of no more
than .
#include<bits/stdc++.h> #define int long long #define endl '\n' typedeflonglong ll; usingnamespace std; voidsolve(){ int n, m; cin >> n >> m; string s; int flag = 1; int count = 0; int ct = 0; for (int i = 0; i < n; ++i) { cin >> s; if (flag && count + int(s.size()) <= m) { ++ct; count += int(s.size()); } else { flag = 0; continue; } } cout << ct << endl; } signedmain(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return0; }
B. Transfusion
Description
You are given an array of
length . In one operation, you can
pick an index from to inclusive, and do one of the
following actions:
Decrease by , then increase by .
Decrease by , then increase by .
After each operation, all the values must be non-negative. Can you
make all the elements equal after any number of operations?
Input
First line of input consists of one integer — the number of test
cases.
First line of each test case consists of one integer .
Second line of each test case consists of integers .
It is guaranteed that the sum of of all test cases doesn't exceed .
Output
For each test case, print "YES" without quotation marks if it is
possible to make all the elements equal after any number of operations;
otherwise, print "NO" without quotation marks.
You can print answers in any register: "yes", "YeS", "nO" — will also
be considered correct.
You are given a number with a
length of no more than .
You can perform the following operation any number of times: choose
one of its digits, square it, and replace the original digit with the
result. The result must be a digit (that is, if you choose the digit x,
then the value of must be less
than ).
Is it possible to obtain a number that is divisible by through these operations?
Input
The first line contains an integer — the number of test cases.
The only line of each test case contains the number , without leading zero. The length of
the number does not exceed .
Output
For each test case, output "YES" if it possible to obtain a number
divisible by using the described
operations, and "NO" otherwise.
You can output each letter in any case (lowercase or uppercase). For
example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as
a positive answer.
#include<bits/stdc++.h> #define int long long #define endl '\n' usingnamespace std; voidsolve(){ string num; cin >> num; int ct2 = 0, ct3 = 0; int sum = 0; for (char d : num) { if (d == '2') { ++ct2; } elseif (d == '3') { ++ct3; } sum += d - '0'; } if (sum % 9 == 0) { cout << "YES" << endl; } else { // brute force --- 枚举 for (int i = 0; i <= ct2; i++) { for (int j = 0; j <= ct3; j++) { if ((sum + i * 2 + j * 6) % 9 == 0) { cout << "YES" << endl; return; } } } for (int i = 0; i <= ct3; i++) { for (int j = 0; j <= ct2; j++) { if ((sum + i * 6 + j * 2) % 9 == 0) { cout << "YES" << endl; return; } } } cout << "NO" << endl; } } signedmain(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return0; }
D. Digital string
maximization
Description
You are given a string ,
consisting of digits from to
. In one operation, you can pick
any digit in this string, except for or the leftmost digit, decrease it by
, and then swap it with the digit
left to the picked.
For example, in one operation from the string , you can get or .
Find the lexicographically maximum string you can obtain after any
number of operations.
Input
The first line of the input consists of an integer — the number of
test cases.
Each test case consists of a single line consisting of a digital
string , where denotes
the length of . The string does
not contain leading zeroes.
It is guaranteed that the sum of of all test cases doesn't exceed .
Output
For each test case, print the answer on a separate line.
#include<bits/stdc++.h> #define int long long #define endl '\n' usingnamespace std; voidsolve(){ string s; cin >> s; int n = s.size(); int max, value; for (int i = 0; i < n; ++i) { max = i; for (int j = i + 1; j < n; ++j) { if (j - i >= 9) break; // 差距达到 9 时,被一直前移的数字变为 0. value = s[j] - (j - i); if (value > s[max] - (max - i)) { max = j; } } // 前移最大数字,substr慢死了。。 // s = s.substr(0, i) + char(s[max] - (max - i)) + s.substr(i, max - i) + // s.substr(max + 1); if (i != max) { char tmp = char(s[max] - (max - i)); for (int j = max; j > i; --j) { s[j] = s[j - 1]; } s[i] = tmp; } } cout << s << endl; } signedmain(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return0; }
E. Three Strings
Description
You are given three strings: , and , consisting of lowercase Latin letters.
The string was obtained in the
following way:
At each step, either string
or string was randomly chosen,
and the first character of the chosen string was removed from it and
appended to the end of string c, until one of the strings ran out. After
that, the remaing characters of the non-empty string were added to the
end of .
Then, a certain number of characters in string were randomly changed.
For example, from the strings and , without character replacements,
the strings , , could be obtained.
Find the minimum number of characters that could have been changed in
string .
Input
The first line of the input contains a single integer — the number of
test cases.
The first line of each test case contains one string of lowercase
Latin letters — the first string, where denotes the length of string .
The second line of each test case contains one string of lowercase
Latin letters — the second string, where denotes the length of string .
The third line of each test case contains one string of lowercase
Latin letters —
the third string.
It is guaranteed that the sum of across all test cases does not exceed
. Also, the sum of across all test cases does not exceed
.
Output
For each test case, output a single integer — the minimum number of
characters that could have been changed in string .
#include<bits/stdc++.h> #define int long long #define endl '\n' usingnamespace std; voidsolve(){ string a, b, c; cin >> a >> b >> c; int la = a.size(), lb = b.size(); vector<vector<int>> dp(la + 1, vector<int>(lb + 1, la + lb)); dp[0][0] = 0; for (int i = 0; i <= la; ++i) { for (int j = 0; j <= lb; ++j) { if (i < la) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (int)(a[i] != c[i + j])); if (j < lb) dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (int)(b[j] != c[i + j])); } } cout << dp[la][lb] << endl; } signedmain(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return0; }
F. Maximum modulo equality
Description
You are given an array of
length and queries , .
For each query, find the maximum possible , such that all elements are equal
modulo . In other words, , where — is the remainder
of division by . In particular, when can be infinite, print .
Input
The first line contains a single integer — the number of
test cases.
The first line of each test case contains two integers , — the length of the array and the number
of queries.
The second line of each test case contains integers — the elements
of the array.
In the following lines of each
test case, two integers , are provided — the range of the
query.
It is guaranteed that the sum of across all test cases does not exceed
, and the sum of does not exceed .
Output
For each query, output the maximum value described in the statement.
#include<bits/stdc++.h> #define ll long long #define par pair<ll,ll> #define prr pair<ll,par> #define st first #define nd second #define ld long double #define MX 1000000000000ll #define MO 1000000007ll // #define MO 998244353ll #define MN 0.000001 #define MXM 2000002 #define MXN 200002 #define PP 131ll #define LG 20 usingnamespace std; inlinevoidrd(ll &x){x=0;short f=1;char c=getchar();while((c<'0'||c>'9')&&c!='-') c=getchar();if(c=='-') f=-1,c=getchar();while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();x*=f;} inlinevoidpt(ll x){if(x<0) putchar('-'),x=-x;if(x>9) pt(x/10);putchar(x%10+'0');} /* BBBBBBB ZZZZZZZZ MM MM BB BB ZZ MMM MMM BB BB ZZ MMMM MMMM BBBBBBB ZZ MM MM MM MM BB BB ZZ MM MM MM MM BB BB ZZ MM MMM MM BBBBBBB ZZZZZZZZ MM M MM */ ll T=1,n,nl,q,a[MXN]; ll st[MXN][LG]; ll ask(ll l,ll r){ if(l>r) return0; ll x=log2(r-l+1); return __gcd(st[l][x],st[r-(1<<x)+1][x]); } voidsolve(){ rd(n),rd(q); for(ll i=1;i<=n;i++) rd(a[i]),st[i][0]=abs(a[i]-a[i-1]); nl=log2(n); for(ll k=1;k<=nl;k++) for(ll i=1;i+(1<<(k-1))<=n;i++) st[i][k]=__gcd(st[i][k-1],st[i+(1<<(k-1))][k-1]); while(q--){ ll l,r; rd(l),rd(r);l++; pt(ask(l,r)),putchar(' '); } puts(""); }intmain(){rd(T);while(T--) solve();}
ST表
官方题解
2050F - Maximum modulo equality
Let's look at two arbitrary integers and . Now we want to find the maximum , which satisfies . We know that x m = y m, then , because
they have the same remainder by .
That means that any which is a
divisor of will satisfy the
required condition.
Now let's generalize the idea we've obtained to the segment: means that , and , and , and . So, must be a divisor
of , , , at the same time. That
means that should be , where is the greatest common
divisor. , when all the elements
on the segment are equal.
Let's build an array consisting of differences of adjacent elements;
now we can use sparse table to find GCD on the segments efficiently.