Codeforces Round 82 (Rated for Div. 2)

A title

You are given a string s. Each character is either 0 or 1.

You want all 1’s in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1’s form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.

You may erase some (possibly none) 0’s from the string. What is the minimum number of 0’s that you have to erase?

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

Then t lines follow, each representing a test case. Each line contains one string s (1≤|s|≤100); each character of s is either 0 or 1.

Output
Print t integers, where the i-th integer is the answer to the i-th testcase (the minimum number of 0’s that you have to erase from s).

Example
input
3
010011
0
1111000
output
2
0
0

Note
In the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).

Subject to the effect:

A 01 string, so that all together 1 are continuous, ask you to eliminate the number 0

Ideas:

Get the first position in the string, and then it comes back on the statistics on the foreseeable 0

#include <iostream>
#include<string>
#include<stdio.h>
using namespace std;
 
int main()
{
    int m;
    cin>>m;
    getchar();
    while(m--)
    {
 
        string str;
        getline(cin,str);
        int d = str.size();
        int i=0;
        while(1)
        {
            if(str[i]=='0')
            i++;
            else break;
        }
        int sum=0;
        int conut=0;
        while(i<d)
        {
            if(str[i]=='1'){
                sum+=conut;
                conut=0;
                i++;
            }
            else if(str[i]=='0'){
                conut++;
                i++;
            }
 
        }
        cout<<sum<<endl;
    }
}

B title

National Project

Your company was appointed to lay new asphalt on the highway of length n. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.

Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are g days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next b days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again g good days, b bad days and so on.

You can be sure that you start repairing at the start of a good season, in other words, days 1,2,…,g are good.

You don’t really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the n=5 then at least 3 units of the highway should have high quality; if n=4 then at least 2 units should have high quality.

What is the minimum number of days is needed to finish the repair of the whole highway?

Input
The first line contains a single integer T (1≤T≤104) — the number of test cases.

Next T lines contain test cases — one per line. Each line contains three integers n, g and b (1≤n,g,b≤109) — the length of the highway and the number of good and bad days respectively.

Output
Print T integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.

Example
inputCopy
3
5 1 1
8 10 10
1000000 1 1000000
outputCopy
5
8
499999500000

Meaning of the questions:

There are n need to work day. There is such a rule, consecutive days of good weather g, b days in a row and then bad weather. If the weather is good you can do the work, it requires at least n / 2 rounded up the number of good number of the day. You can choose to rest, but there is n days of work waiting for you, that is to say whatever you want to rest a few days, but at least n days work. The minimum number of days required to complete the work.

answer:

Calculated for at least a period of fine weather and bad weather and. Analyzing the desired n% g is 0 determines whether the work can be completed just

#include <iostream>
#include<stdio.h>
typedef long long ll;
using namespace std;

int t;
long long ans;
int main()
{
    long long n,g,b,tmp;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld",&n,&g,&b);
        tmp=n;
        n=(n+1)>>1;
        if(n%g==0) ans=(g+b)*(n/g)-b;
        else ans=(g+b)*(n/g)+n%g;
        printf("%lld\n",max(ans,tmp));
    }
    return 0;
}

C

Perfect Keyboard

Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.

Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn’t care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn’t like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi… is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).

Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?

Input
The first line contains one integer T (1≤T≤1000) — the number of test cases.

Then T lines follow, each containing one string s (1≤|s|≤200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.

Output
For each test case, do the following:

if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
inputCopy
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
outputCopy
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

题意 输入一行字符串,每个相邻的字符在键盘上也是相邻的,如果可以输出YES和键盘布局,不然输出NO

#include <bits/stdc++.h>
using namespace std;
char a[55];
bool vis[26];
void ls(){
    memset(vis,0,sizeof(vis));
    memset(a,0,sizeof(a));
    string s;cin>>s;
    int j=27;
    a[j]=s[0];
    vis[s[0]-'a']=1;
    for(int i=1;i<s.size();i++){
        if(vis[s[i]-'a']){
            if(a[j-1]==s[i])
                j--;
            else if(a[j+1]==s[i])
                j++;
            else{
                cout<<"NO"<<endl;
                return;
            }
        }
        else{
            if(!a[j-1]){
                j--;
            }
            else if(!a[j+1]){
                j++;
            }
            else{
                cout<<"NO"<<endl;
                return;
            }
            a[j]=s[i];
            vis[a[j]-'a']=1;
        }
    }
    cout<<"YES"<<endl;
    for(int i=0;i<26;i++){
        if(!vis[i]){
            cout<<char('a'+i);
        }
    }
    for(int i=0;i<55;i++){
        if(a[i]>='a'&&a[i]<='z'){
            cout<<a[i];
        }
    }
    cout<<endl;
}
int main() {
    int t;cin>>t;
    while(t--){
        ls();
    }
}

Published 24 original articles · won praise 5 · Views 1692

Guess you like

Origin blog.csdn.net/weixin_44091157/article/details/104337232