Codeforces Round #823 (Div. 2)(A~D) (analog, binary, math, string construction)

A. Planets

Satellite shooting, simple simulation questions

#include<bits/stdc++.h>
using namespace std;
int a[105];
int main()
{
    int t;cin>>t;
    while(t--){map<int,int>mp;
        int n,c;cin>>n>>c;
        for(int i=0;i<n;i++){cin>>a[i];、mp[a[i]]++;}
        int ans=0;
        for(auto i:mp)
            if(i.second>=c)ans+=c;
            else ans+=i.second;
        cout<<ans<<endl;
    }
    return 0;
}

B. Meeting on the Line

The meaning of the question: the time it takes for everyone to reach x0 is ti +| x0 - xi |, where should x0 be set

Ideas:

Divide the time and check whether the time is appropriate. Finally, get the appropriate shortest time, and use this shortest time to find the overlapping place with the largest range that everyone can move, then this overlapping place is the answer. At the same time, how to check is also to find intervals. If some intervals do not overlap, then this time is not suitable.

Notice:

Remember to output 10 digits when outputting, otherwise it will be WA

code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
double x[100005],t[100005],n,ans;
bool check(double mid){
    double r=1e18,l=0;
    for(int i=0;i<n;i++){
        if(mid<t[i])return 0;
        double time=mid-t[i];
        r=min(r,x[i]+time);
        l=max(l,x[i]-time);
    }
    ans=r;
    return r>=l;
}
signed main()
{
    int T;cin>>T;
    while(T--){
        cin>>n;
        for(int i=0;i<n;i++)cin>>x[i];
        for(int i=0;i<n;i++)cin>>t[i];
        double l=0,r=1e18;//模拟时间
        for(int i=1;i<=100;i++){
            double mid=(l+r)/2;
            if(check(mid)){r=mid;}
            else l=mid;
        }
        check(r);
        cout<<fixed<<setprecision(10)<<ans<<endl;
    }
    return 0;
}

C. Minimum Notation

simple simulation

Idea: record the number of 0~9 of the final output answer, and then output from small to large

1. Record the position of each number, enumerate the last digit of i from 0 to 9

2. Then record the number from the last digit of i-1 to  the last digit of i

2.1 If the number is 9, the number of 9+1

2.2 If the number is i, the number of i + 1

2.3 In the remaining cases, the number of (the number + 1) + 1

Then output from small to large

code:

#include<bits/stdc++.h>
using namespace std;
char s[200005],ss[200005];
int main()
{
    int T;cin>>T;
    while(T--){
        cin>>s;
        vector<int>v[10];
        int co[10]={0};
        int n=strlen(s);
        for(int i=0;i<n;i++){
            ss[i]=s[i];
            v[s[i]-'0'].push_back(i);
        }
        int l=0;
        for(int i=0;i<=9;i++){
            int r=v[i].size();
            if(r==0)continue;
            if(v[i][r-1]<l)continue;
            for(int q=l;q<=v[i][r-1];q++){
                if(s[q]==(i+'0'))co[i]++;
                else if(s[q]=='9')co[9]++;
                else co[s[q]-'0'+1]++;
            }
            l=v[i][r-1]+1;
        }
        for(int i=0;i<=9;){
            if(co[i]!=0)cout<<i;co[i]--;if(co[i]<=0)i++;
        }
        cout<<endl;
    }
    return 0;
}

D. Prefixes and Suffixes (string construction)

Question: There are two strings s and t, exchange the prefix of s and the suffix of t of the same length, can you make s equal to t?

(While the prerequisites below are another approach, I didn't learn...)

Preliminary knowledge: the next array represents the longest common prefix sum, that is, (except the current character) the longest common length of the prefix (from left to right) and suffix (from left to right), next[i]-1 is this length (If the first one is 0); if the first next value is -1, the next record is the longest common length.

For example, the following example: nextArray solution detailed explanation

pattern string a b a b a a a b a b a a
subscript 1 2 3 4 5 6 7 8 9 10 11 12
next array 0 1 1 2 3 4 2 2 3 4 5 6
void getnx(string &t,int *nx){
    for(int i=1,j=0;i<(int)t.size();i++){ //j记录每次nx[]的长度 i从1, j从0开始
        while(j&&t[i]!=t[j])
            j=nx[j-1];
        if(t[i]==t[j])j++;
        nx[i]=j;
    }
}

Idea: Refer to D. Prefixes and Suffixes (string/thinking/palindrome)

Let the string subscript start from 0, len=s.size()

It can be seen from the exchange that the relative positions of s1[i] and s2[len-i-1] are unchanged. For example, the relative positions of {b, 2} in the following figure are unchanged and symmetrical

insert image description here 

Therefore, if a pair is an even number of times, then it meets the meaning of the question

If a pair is an odd number of times,

           If the two characters are different, it must not work.
           If they are the same, only once, that is, in the middle

code:

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
map< pair<char,char>,int>mp;
int main()
{
    int T;cin>>T;
    while(T--){
        mp.clear();int n;cin>>n;
        cin>>s1>>s2;
        for(int i=0;i<n;i++){
            char a=s1[i],b=s2[n-i-1];
            if(a>b)swap(a,b);
            mp[{a,b}]++;
        }
        int qi=0;bool flag=1;
        for(auto i:mp){
            if(i.second%2==0)continue;
            if(i.first.first!=i.first.second){flag=0;break;}
            else{qi++;if(qi>1){flag=0;break;}}
        }
        if(flag)cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/zy98zy998/article/details/127112470