Codeforces Round 82 (Rated for Div. 2)

A题

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).

题目大意:

一个01串,让所有的1都连续在一起,问你要消除多少个0

思路:

获取首个1在字符串中的位置,然后后面但凡预见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 题

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

题意:

有n天需要工作。有这么一个规则,连续g天是好天气,再连续b天是坏天气。如果在好天气工作可以做好,要求至少有n/2向上取整天数做好的个数。你可以选择休息,但是有n天的工作等着你,也就是说随便你休息多少天,但是至少要n天工作。求最小的天数完成工作。

题解:

算出来最少工作的周期*好天气与坏天气的和。判断所需n%g是否为0来判断工作是否能正好完成

#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();
    }
}

发布了24 篇原创文章 · 获赞 5 · 访问量 1692

猜你喜欢

转载自blog.csdn.net/weixin_44091157/article/details/104337232