ABC 223 A~F (C measure, D topological sort, E thinking, F block)

Link

A Exact Price

Take the remainder of n to see if it is 0, pay attention to the special judgment of 0.

B String Shifting

Find the smallest and largest, we find that he has a circular string, then we can extract n strings and sort them.

C - Doukasen

We walk from both ends to the middle, and see which end reaches the junction first, and then we will move forward to the next block. The speed will change. When we reach the other end of the junction, the current time can be subtracted (the speed remains unchanged), so we Just compare how long it takes to walk through the last block at both ends, and take the smaller one. It is wrong to use ll for double length.

ll n,m;
 
struct node
{
    
    
    double a, b;
}q[MAX];
bool cmp(node a,node b){
    
    
    return a.a<b.a;
}
void solve()
{
    
    
    cin>>n;    
    for(int i=1;i<=n;i++){
    
    
        cin>>q[i].a>>q[i].b;                
    }
    ///sort(q+1,q+1+n,cmp);
    
    ll l=1,r=n;
    double sum=0.0;
    while(l<=r){
    
            
        if(l!=r){
    
    
            double t1,t2;
            t1=q[l].a*1.0/q[l].b;
            t2=q[r].a*1.0/q[r].b;
            //cout<<t1<< " "<< t2<<endl;
            if(t1>t2){
    
    
                sum+=t2*q[l].b;
                q[l].a-=t2*q[l].b;
                r--;
            }else if(t2>t1){
    
    
                sum+=q[l].a;
                q[r].a-=t1*q[r].b;
                l++;
            }else if(t1==t2){
    
    
                sum+=q[l].a;
                l++;
                r--;
            }
        }else {
    
    
            sum+=q[l].a*1.0/2;            
            l++;r--;
            break;
        }  
    }
    printf("%.20lf\n",sum);
}

D - Restricted Permutation

Title:

Given n points, m conditions:

  • Two arguments per condition, a,b, means that a appears before b,.
    After reading this condition, did you think of it, precondition, isn't this what topological sorting means? write directly;
ll n,m;
std::vector<ll> v[MAX];
ll in[MAX],out[MAX];
ll b[MAX],a[MAX];
priority_queue<ll,vector<ll>,greater<ll> >mx;
void topsort(){
    
    
    
    for(ll i=1;i<=n;i++){
    
    
        if(in[i]==0){
    
    
            mx.push(i);
            b[i]==1;
        }
    }
    ll cnt=0;
    while(!mx.empty()){
    
    
        ll top = mx.top();
        a[++cnt]=top;
        mx.pop();
        for(int i=0 ;i<v[top].size();i++){
    
    
            ll j=v[top][i];
            in[j]--;
            if(b[j]==0&&in[j]==0){
    
    
                b[j]=1;
                mx.push(j);
            }
        }
    }
    if(cnt==n) {
    
    
        for(int i=1;i<=n;i++) cout<< a[i]<<" ";
    }else puts("-1");
}
void solve()
{
    
    
    cin>>n>>m;
    for(int i=1;i<=m;i++){
    
    
        ll x,y;
        cin>>x>>y;
        in[y]++;
        out[x]++;
        v[x].push_back(y);
    }
    topsort();
}

E - Placing Rectangles

Title:

Very simple: just give you an n × mn\times mn×The rectangle of m , let you make three rectangles with areas a, b, c, and see if you can fit it.

analyze:

If we put three rectangles in one rectangle how many kinds of that?
We can know it by drawing:
the first type is three horizontally; the
second type is three vertical; the
third type is one vertical and two horizontal: the
fourth type is one horizontal and two vertical:
Okay, that's all There are four kinds, then we can directly judge. If any one can be let go, it means that we can let go. If we can’t let go, we can’t let go.

bool check1(ll a, ll b, ll c){
    
    
    ll a1=(a+x-1)/x;
    ll ty=y-a1;
    ll b1=(b+ty-1)/ty;
    ll c1=(c+ty-1)/ty;
    if(b1+c1<=x) return 1;
    
    a1=(a+y-1)/y;
    ty=x-a1;
    b1=(b+ty-1)/ty;
    c1=(c+ty-1)/ty;
    if(b1+c1<=y) return 1;
 
    return 0;
}
 
ll a,b,c;
 
void solve()
{
    
    
    cin>>x>>y>>a>>b>>c;
    if(a+b+c>x*y) puts("No");
    else{
    
    
        ///1
        ll a1=(a+x-1)/x;
        ll b1=(b+x-1)/x;
        ll c1=(c+x-1)/x;        
        if(a1+b1+c1<=y){
    
    
            puts("Yes");
            return ;
        }
        ///2
        a1 = (a+y-1)/y;
        b1 = (b+y-1)/y;
        c1 = (c+y-1)/y;
        if(a1+b1+c1<=x){
    
    
            puts("Yes");
            return ;
        }
        ///3
        if(check1(a,b,c)||check1(b,a,c)|| check1(c,b,a)){
    
    
            puts("Yes");
            return ;
        }
        puts("No");
    }
}

F - Parenthesis Checking

Title:

Given a sequence of parentheses of length n, perform q operations on it:

  • 1 l,r Swap the signs in the l,r positions.
  • 2 l,r asks l~r whether it is a canonical sequence.

analyze:

We can do it in blocks. If we exchange, we change two points, which affects the n \sqrt n where these two points are located.n Sequence, interrogation, we just need to look at both ends. The complexity is O ( nn ) O(n\sqrt n)O ( nn )


string str;
ll n,m;
ll a[MAX],b[MAX];
void solve()
{
    
    
    cin>>n>>m;
    ll num = 0 ;
    while(num*num <n) num++;
    cin>>str;
    for(int i=0;i*num<n;i++){
    
    
        a[i] = 0;
        b[i] = 0;
        for(int j=0;j<num;j++){
    
    
            if(str[i*num+j]=='(') a[i]++;
            else {
    
    
                a[i]--;
            }
            if(b[i] > a[i]){
    
    
                b[i] = a[i];
            }
        }
    }
    ll l, r, op; 
    while(m--){
    
    
        cin>>op>>l>>r;
        l--;r--;
        if(op==1){
    
    
            swap(str[l],str[r]);
            ll x = l / num;
            a[x] = 0;
            b[x] = 0;
            for(int j=0;j<num;j++){
    
    
                if(str[x*num+j]=='('){
    
    
                    a[x]++;
                }else {
    
    
                    a[x]--;
                }
                if(b[x]>a[x]) b[x]=a[x];
            }
            x = r / num;
            a[x] = 0;
            b[x] = 0;
            for(int j=0;j<num;j++){
    
    
                if(str[x*num+j]=='('){
    
    
                    a[x]++;
                }else {
    
    
                    a[x]--;
                }
                if(b[x]>a[x]) b[x]=a[x];
            }            
        }else {
    
    
            ll flag=0,sum=0;
            if(l/num <r/num){
    
    
                ll i = l/num;
                for(int j=l%num;j<num;j++){
    
    
                    if(str[i*num+j]=='(') sum++;
                    else sum--;
                    if(sum<0) flag++;
                }

                for(int i=l/num+1;i<r/num;i++){
    
    
                    if(sum+b[i]<0) flag++;
                    sum+=a[i];
                }
                i=r/num;
                for(int j=0;j<=r%num;j++){
    
    
                    if(str[i*num+j]=='(') sum++;
                    else sum--;
                    if(sum<0) flag++;
                }
                if(sum!=0) flag++;
            }else {
    
    
                ll i=l/num;
                for(int j=l%num;j<=r%num;j++ ){
    
    
                    if(str[i*num+j]=='(') sum++;
                    else sum--;
                    if(sum<0) flag++;
                }
                if(sum!=0) flag++;
            }
            if(flag>0) puts("No");
            else puts("Yes");
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326263485&siteId=291194637
Recommended