すのはら荘春原庄的雪

Codeforces Round #780 (Div. 3)A-D题解

Toretto·2022-04-01 23:14·132 次阅读

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 22-burle coin,
  • 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.

Input

The first line of the input contains an integer tt (1t1041≤t≤104) — the number of test cases in the test.

The description of each test case consists of one line containing two integers aiai and bibi (0ai,bi1080≤ai,bi≤108) — the number of 11-burle coins and 22-burles coins Vasya has respectively.

Output

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
input
5
1 1
4 0
0 2
0 0
2314 2374
output
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 (1in1≤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.

Input

The first line of input data contains an integer tt (1t1041≤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 (1n21051≤n≤2⋅105) — the number of types of candies in the package.

The second line of the case contains nn integers aiai (1ai1091≤ai≤109) — the number of candies of the type ii.

It is guaranteed that the sum of nn for all cases does not exceed 21052⋅105.

Output

Output 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
input
6
2
2 3
1
2
5
1 6 2 4 3
4
2 2 2 1
3
1 1000000000 999999999
1
1
output
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=a1a2ana=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 (1in11≤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.

Input

The first line of input data contains an integer tt (1t1041≤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|21051≤|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 21052⋅105.

Output

For each test case, print a single number — the minimum number of characters that must be removed to make ss even.

Example
input
6
aabbdabdccc
zyx
aaababbb
aabbcc
oaoaaaoo
bmefbmuyw
output
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 (1in1≤i≤n) the following inequality is true: 2ai2−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.

Input

The first line of input data contains an integer tt (1t1041≤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 (1n21051≤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 21052⋅105.

Output

For each test case, output two non-negative numbers xx and yy (0x+yn0≤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
input
5
4
1 2 -1 2
3
1 1 -2
5
2 0 -2 2 -1
3
-2 -1 -1
3
-1 -2 -2
output
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!

 
posted @   cbmango  阅读(132)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示