Codeforces Round # 608 (Div. 2) solution to a problem

Codeforces Round # 608 (Div. 2) solution to a problem

Foreword

Topic links:Just for the convenience of the topic as a keyword to be able to find the solution to my problem only (escape

Codeforces 1271A

Codeforces 1271B

Codeforces 1271C

Codeforces 1271D

Codeforces 1271E

Important: There is no F's solution to a problem, we want to see the F problem solution to a problem of immortals can go

A. Suits

The meaning of problems

You \ (a \) tie, \ (b \) scarf, \ (c \) vest, \ (d \) jacket, there are two packages:

  • A tie, a jacket, spent \ (e \) yuan

  • A scarf, a vest, a jacket, spent \ (f \) yuan

How much money can be obtained to sell up to (not sold separately).(Profiteers)

practice

According to the meaning of the questions we get:

If there is (I \) \ species \ (E \) element set, it is the price is \ (i \ times e + min \ {di, b, c \} \ times f \) element, can be enumerated ( Note that \ (I \) , \ (DI \) , \ (B \) , \ (C \) can not appear negative).

program

#include<bits/stdc++.h>
using namespace std;

int a,b,c,d,e,f,ans;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>a>>b>>c>>d>>e>>f;
    for(int i=0;i<=min(a,d);i++){//数据范围小,直接暴力枚举
        ans=max(ans,i*e+min((d-i),min(b,c))*f);
    }
    cout<<ans<<endl;

    return 0;
}

B. Blocks

FST I really was too dishes

The meaning of problems

Give you a row \ (n \) squares, with each square in black or white, the color of each operation can be reversed two adjacent squares, and asked whether \ (3n \) in the number of invert into a whole row are the same color.

practice

Suppose the sequence is reversed to white, then run front to back again, if the current block is not white, and it is inverted block behind it, and finally finish the operation is complete, if a block is white on OK. Black Similarly, assuming that the sequence is reversed to black, then front to back run again, if the current block is not a black box to reverse it and behind it, and finally finish a box if the operation is finished in black on OK. Finally, if you can not output \ (--1 \) a.

program

#include<bits/stdc++.h>
using namespace std;

int n;
char s[205];
vector<int> op;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    cin>>s+1;
    //假设全是白色:
    for(int i=1;i<n;i++){
        if(s[i]=='B'){
            s[i]='W';
            s[i+1]=s[i+1]=='W'?'B':'W';
            op.push_back(i);
        }
    }
    //判断最后一个方块:
    if(s[n]=='W'){
        cout<<op.size()<<endl;
        for(int i=0;i<op.size();i++){
            cout<<op[i]<<' ';
        }
        cout<<endl;
        return 0;
    }
    //假设全是黑色:(此时可以直接再操作同一个序列)
    for(int i=1;i<n;i++){
        if(s[i]=='W'){
            s[i]='B';
            s[i+1]=s[i+1]=='W'?'B':'W';
            op.push_back(i);
        }
    }
    //判断最后一个方块
    if(s[n]=='B'){
        cout<<op.size()<<endl;
        for(int i=0;i<op.size();i++){
            cout<<op[i]<<' ';
        }
        cout<<endl;
        return 0;
    }
    cout<<-1<<endl;

    return 0;
}

C. Shawarma Tent

Simpler than B

The meaning of problems

Give you a plane Cartesian coordinate system(Just learned math eh)After school and give you a (n \) \ students coordinates (is the whole point), asking you to build a tent to buy Shawarma at a point different from the whole school, a magical if a student home from school after the path will buy(When consumers fool you), You will be asked to buy up to a few students, and this time the coordinates of the tent (you can either coordinate)(Profiteers)

Note: The whole point refers to the abscissa, ordinate points are integers. Magic path is only moved up and down between the (parallel to the transverse or longitudinal) of the two shortest path (length is the Manhattan distance between two points) (there may be multiple bar between two points).

practice

Obviously, the closer the school, there will be more students from the tent through, so long as the enumeration of schools up and down about four points to see which best on it.

program

#include<bits/stdc++.h>
using namespace std;

int n,sx,sy;
int x[200005],y[200005];
int l,r,u,d;
//l, r, u, d 分别表示左右上下的帐篷经过的学生个数

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>sx>>sy;
    for(int i=1;i<=n;i++){
        cin>>x[i]>>y[i];
        if(x[i]>sx)r++;
        if(x[i]<sx)l++;
        if(y[i]>sy)u++;
        if(y[i]<sy)d++;
        //对于经过帐篷的学生计数
    }
    if(r>=l&&r>=r&&r>=u&&r>=d){
        cout<<r<<endl;
        cout<<sx+1<<' '<<sy<<endl;
    }else
    if(l>=l&&l>=r&&l>=u&&l>=d){
        cout<<l<<endl;
        cout<<sx-1<<' '<<sy<<endl;
    }else
    if(u>=l&&u>=r&&u>=u&&u>=d){
        cout<<u<<endl;
        cout<<sx<<' '<<sy+1<<endl;
    }else
    if(d>=l&&d>=r&&d>=u&&d>=d){
        cout<<d<<endl;
        cout<<sx<<' '<<sy-1<<endl;
    }
    //这里的u>=u之类只是方便copy&paste罢了

    return 0;
}

D. Portals

Problems face a long ah, really do not want to translate

The meaning of problems

You have to take \ (n \) cities. In the beginning you have \ (k \) soldiers. Take the first \ (i \) cities, you need to \ (a_i \) soldiers (without loss of soldiers during the attack), after the capture of you will catch \ (b_i \) after two young men to expand your team, you take can send a soldier to defend, the defense will make your score increase \ (C_i \) , the loss of a soldier. Defense in two ways:

  • You can defend on your location \ (i \) cities

  • There \ (m \) one-way portal, from \ (u \) to \ (v \) , if you \ (u \) , you can send troops to defend \ (v \) (must be directly portal connected city)

You have from the first \ (1 \) seat has been to capture the first \ (n \) cities, if you can not capture all the cities, you're lost, output \ (- 1 \) , if win, find the maximum score .

practice

Greedy do calculates \ (req_i = max \ {a_ {i + 1}, req_ {i + 1} - b_ {i + 1} \} | req_n = 0 \) represents the section \ (I \) cities overcome and there \ (b_i \) after soldiers join, you complete the game requires a minimum of soldiers. After there will be part of the soldiers vacated, the \ (i \) cities new empty out the number of soldiers referred to as \ (fr_i \) , and then with a large root heap to dispatch soldiers to the city just fine.

In this case, the \ (i \) cities can guard its last city referred to as \ (def_i \) , from \ (1 \) to \ (i \) of idle soldiers apparently can guard it (idle soldiers you can follow you to the next city).

program

#include<bits/stdc++.h>
using namespace std;

int n,m,k;
int a[5005],b[5005],c[5005],fr[5005];
vector<int> g[5005];//无用数组
int def[5005],req[5005];
priority_queue<pair<int,int> > pq;
int ans;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i]>>c[i];
        def[i]=i;
    }
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);//无用语句
        def[v]=max(def[v],u);
    }
    for(int i=n;i>=1;i--){
        req[i]=max(a[i+1],req[i+1]-b[i+1]);cerr<<i<<' '<<req[i]<<endl;
    }
    //计算req[i]
    int cur=k;
    for(int i=1;i<=n;i++){
        if(cur<a[i]){
            cout<<-1<<endl;
            return 0;
        }
        cur+=b[i];
    }
    //↑判断是否能赢↑
    cur=k;
    for(int i=1;i<=n;i++){
        cur+=b[i];
        fr[i]=cur-req[i];cur=req[i];
        pq.push(make_pair(c[i],i));
    }
    //计算fr[i],初始化以重量为第一关键字降序排列的堆
    while(!pq.empty()){
        int val=pq.top().first,x=pq.top().second;
        pq.pop();
        int y=def[x];
        while(!fr[y]&&y>0)y--;
        if(y==0)continue;
        fr[y]--;//贪心地选择最后一个能选的城市的空闲士兵
        ans+=val;
    }
    cout<<ans<<endl;

    return 0;
}

E. Common Number

The meaning of problems

E do the people should know it. . . And face very brief question I do not write (escape

practice

Defined \ (K_i \) comprising \ (I \) number of paths.

First, the odd and even considered separately, you will find the same \ (the n-\) , the same is odd or even, \ (the y-\) bigger \ (k_y \) is certainly smaller, so monotone, can be bipartite .

We look for a single number, how to calculate the number of paths contain its article:

The calculation of the path of the other way round, can be drawn in reverse thrust path, only even be a plus, but all numbers can be multiplied by two. How many records can be obtained in this way the number on it. But this is too slow, so we need a faster way:

By observing the results, two times by the same, from a minimum value up (only two multiplied) up to a maximum value (after multiplying each have two plus one), all the numbers between are attainable, so long as we like to calculate the minimum and maximum complexity -. Because certainly by two plus a growing faster than, the number of times for each of one kind multiplied by two, the length of this interval is the number of paths is added to it.

program

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll n,k;

bool check(ll m){
    ll l=m,r=m,res=0;
    if(!(m&1)){r++;}//当m是偶数时给r增加1
    for(int i=1;;i++){//其实i没有什么用
        res+=min(n,r)-l+1;//添加区间长度到返回值
        //for(int j=l;j<=min(r,n);j++)cerr<<j<<' ';
        l<<=1;
        r=(r<<1)+1;//计算下一个l和r
        if(l>n)break;//当l大于n时就没有计算的必要了,直接退出
        
    }//cerr<<endl;
    return res>=k;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>k;
    //for(int i=1;i<=n;i++)check(i);
    ll l=1,r=(n+1)/2,m,ans;//二分奇数
    while(l<=r){
        m=(l+r)>>1;
        if(check(2*m-1)){
            ans=2*m-1;
            l=m+1;
        }else{
            r=m-1;
        }
    }
    l=1;r=n/2;//二分偶数
    while(l<=r){
        m=(l+r)>>1;
        if(check(2*m)){
            ans=max(ans,2*m);//由于ans已经取了奇数的值,所以这里是max
            l=m+1;
        }else{
            r=m-1;
        }
    }
    cout<<ans<<endl;

    return 0;
}

Conclusion

In a point it out!

Sorry I can not help not to question F of people, so I (escape

Guess you like

Origin www.cnblogs.com/BlahDuckling747/p/Codeforces-Round-608-Div-2-Ti-Jie.html