Codeforces Round #780 (Div. 3)A-D題解
A. Vasya and Coins
Vasya decided to go to the grocery store. He found in his wallet aa coins of 11 burle and bb coins of 22 burles. He does not yet know the total cost of all goods, so help him find out ss (s>0s>0): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins.
For example, if a=1a=1 and b=1b=1 (he has one 11-burle coin and one 22-burle coin), then:
- he can pay 11 burle without change, paying with one 11-burle coin,
- he can pay 22 burle without change, paying with one 22-burle coin,
- he can pay 33 burle without change by paying with one 11-burle coin and one
- he cannot pay 44 burle without change (moreover, he cannot pay this amount at all).
So for a=1a=1 and b=1b=1 the answer is s=4s=4.
InputThe first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.
The description of each test case consists of one line containing two integers
For each test case, on a separate line print one integer ss (s>0s>0): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all.
Example input5 1 1 4 0 0 2 0 0 2314 2374output
4 5 1 1 7063
題目大意:每個樣例給出一元硬幣與兩元硬幣的數量,問這些硬幣不能湊出的最小正整數;
思路:簽到題一個,不解釋 答案只有兩種,即1或a+b*2;
AC程式碼:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int t; 4 typedef long long ll; 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 cin.tie(0); 9 cin>>t; 10 while(t--){ 11 ll a,b; 12 cin>>a>>b; 13 if(a==0&&b==0){ 14 cout<<"1"<<'\n'; 15 } 16 else if(a==0){ 17 cout<<"1"<<'\n'; 18 } 19 else if(b==0){ 20 cout<<a+1<<'\n'; 21 } 22 else{ 23 cout<<a+2*b+1<<'\n'; 24 } 25 } 26 return 0; 27 }
B. Vlad and Candies
Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were nn types of candies, there are aiai candies of the type ii (1≤i≤n1≤i≤n).
Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose any of them). To get the maximum pleasure from eating, Vlad does not want to eat two candies of the same type in a row.
Help him figure out if he can eat all the candies without eating two identical candies in a row.
InputThe first line of input data contains an integer tt (1≤t≤1041≤t≤104) — the number of input test cases.
The following is a description of tt test cases of input, two lines for each.
The first line of the case contains the single number nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of types of candies in the package.
The second line of the case contains nn integers aiai (1≤ai≤1091≤ai≤109) — the number of candies of the type ii.
It is guaranteed that the sum of nn for all cases does not exceed 2⋅1052⋅105.
OutputOutput tt lines, each of which contains the answer to the corresponding test case of input. As an answer, output "YES" if Vlad can eat candy as planned, and "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
Example input6 2 2 3 1 2 5 1 6 2 4 3 4 2 2 2 1 3 1 1000000000 999999999 1 1output
YES NO NO YES YES YES
題目大意:給定一個數組,每次陣列中最大的那個數減一,且不能連續在同一個位置執行操作,問是否能夠令整個陣列全部為零;
思路:(一開始想著開佇列或者dfs什麼的,後來發現這只是一個簡單的判斷)只要這個陣列中最大的數與第二大的數相差不超過1,那麼這兩個數便可以來回迴圈,過程中便可順便消除其他數。
如果不滿足的話,那麼最大的數減去一後只能再次進行操作,違反題意;
AC程式碼:
#include<bits/stdc++.h> using namespace std; int t; typedef long long ll; ll n,a[200001]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ cin>>n; if(n==1){ cin>>a[1]; if(a[1]>1) cout<<"NO"<<'\n'; else cout<<"YES"<<'\n'; continue; } ll sum=0,maxn,er; for(int i=1;i<=n;i++){ cin>>a[i]; sum+=a[i]; } sort(a+1,a+1+n); maxn=a[n]; sum-=maxn; if(a[n]-1<=a[n-1]) cout<<"YES"<<'\n'; else cout<<"NO"<<'\n'; } return 0; }C. Get an Even String
A string a=a1a2…ana=a1a2…an is called even if it consists of a concatenation (joining) of strings of length 22 consisting of the same characters. In other words, a string aa is even if two conditions are satisfied at the same time:
- its length nn is even;
- for all odd ii (1≤i≤n−11≤i≤n−1), ai=ai+1ai=ai+1 is satisfied.
For example, the following strings are even: "" (empty string), "tt", "aabb", "oooo", and "ttrrrroouuuuuuuukk". The following strings are not even: "aaa", "abab" and "abba".
Given a string ss consisting of lowercase Latin letters. Find the minimum number of characters to remove from the string ss to make it even. The deleted characters do not have to be consecutive.
InputThe first line of input data contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases in the test.
The descriptions of the test cases follow.
Each test case consists of one string ss (1≤|s|≤2⋅1051≤|s|≤2⋅105), where |s||s| — the length of the string ss. The string consists of lowercase Latin letters.
It is guaranteed that the sum of |s||s| on all test cases does not exceed 2⋅1052⋅105.
OutputFor each test case, print a single number — the minimum number of characters that must be removed to make ss even.
Example input6 aabbdabdccc zyx aaababbb aabbcc oaoaaaoo bmefbmuywoutput
3 3 2 0 2 7
題目大意:滿足以下兩個條件的成為美的字串:1.字元數為偶數。2.相同的字元相連且數量不少於2。對於每次操作可以刪除連續的n個字元,問最少經過幾次操作可以讓給定的字串完美。
思路:列舉任意不相鄰的兩個相同字元,刪掉中間的所有字元(即求出任意不相鄰的字元的位置後刪除),便利一邊即可。
AC程式碼:
#include<bits/stdc++.h> using namespace std; int t,n,m; const int maxn=5500; int a[maxn],c[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; a[i]+=a[i-1]; } for(int len=1;len<=n;len++){ c[len]=-1e9; for(int i=1;i+len-1<=n;i++) c[len]=max(c[len],a[i+len-1]-a[i-1]); } int ans=0; for(int i=0;i<=n;i++){ for(int len=1;len<=n;len++){ ans=max(ans,c[len]+min(i,len)*m); } cout<<ans<<" "; } cout<<'\n'; } return 0; }
D. Maximum Product Strikes Back
You are given an array aa consisting of nn integers. For each ii (1≤i≤n1≤i≤n) the following inequality is true: −2≤ai≤2−2≤ai≤2.
You can remove any number (possibly 00) of elements from the beginning of the array and any number (possibly 00) of elements from the end of the array. You are allowed to delete the whole array.
You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them.
The product of elements of an empty array (array of length 00) should be assumed to be 11.
InputThe first line of input data contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases in the test.
Then the descriptions of the input test cases follow.
The first line of each test case description contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) —the length of array aa.
The next line contains nn integers a1,a2,…,ana1,a2,…,an (|ai|≤2|ai|≤2) — elements of array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
OutputFor each test case, output two non-negative numbers xx and yy (0≤x+y≤n0≤x+y≤n) — such that the product (multiplication) of the array numbers, after removing xx elements from the beginning and yy elements from the end, is maximal.
If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be 11.
Example input5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2output
0 2 3 0 2 0 0 1 1 0
題目大意:給定一個序列,分別刪除n個前端的數和m個後端的數使其經過操作後的序列乘積最大,若最大為負數,則輸出1;
思路:可以看出最後的序列中肯定不能存在0,那我們就要列舉所有沒有0的子序列模擬,而題目中給出序列數的範圍是【-2,2】,對於每個子序列,首先判斷序列中小於0的數是偶數還是奇數,如果是奇數便要去掉一個小於0的數。
而對於子序列的成績,因為不含0並且已經保證乘積為正數的時候,便可以累加絕對值為2的數的數量,數量越多,乘積便越大;
AC程式碼
#include<bits/stdc++.h> using namespace std; const int maxn=200002; typedef pair<int,int>P; int t,n,a[maxn]; int ans,l,r; void solve(int begin,int end) { int zero=0,er=0,cnt=0; for(int i=begin;i<=end;i++){ if(a[i]<0) zero++; if(abs(a[i])==2) er++; } if(zero%2==0){ if(er>ans){ l=begin,r=end; ans=er; } return ; } for(int i=begin;i<=end;i++){ if(abs(a[i])==2) cnt++; if(a[i]<0){ if(er-cnt>ans){ l=i+1,r=end; ans=er-cnt; } break; } } cnt=0; for(int i=end;i>=begin;i--){ if(abs(a[i])==2) cnt++; if(a[i]<0){ if(er-cnt>ans){ l=begin,r=i-1; ans=er-cnt; } break; } } return ; } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>t; while(t--) { cin>>n; vector<P>v; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ if(a[i]!=0){ int j; for(j=i+1;j<=n;j++){ if(a[j]==0) break; } j--; v.push_back({i,j}); i=j; } } ans=-1e9; for(auto it:v){ solve(it.first,it.second); } if(ans<0) cout<<'0'<<' '<<n<<'\n'; else cout<<l-1<<' '<<n-r<<'\n'; } return 0; }
感謝大佬閱讀Orz,祝各位大佬早日AK!