gym102215 problem solution

A Rooms and Passages

The meaning of problems

To the number n, from the starting point, he has the right to go, meet the opposite occurred in front of a number of positive and stopped to ask how many steps can take for each origin.

analysis

  • Backwards recursion, if the starting point is a positive number, then certainly you can go ans[i]=ans[i+1]+1.
  • If the starting point is negative, it would have to look at the most forward position corresponding to the absolute value of the negative number appears in the back, but also by the absolute value of the most forward position behind the corresponding negative , so this should be a global update lst .

Code

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+50;
int a[N],n,pos[N],ans[N];
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int lst=n+1;
    for(int i=n;i>=1;i--){
        if(a[i]>0){
            ans[i]=ans[i+1]+1;
            pos[a[i]]=i;
        }else{
            if(!pos[-a[i]]){
                ans[i]=ans[i+1]+1;
            }else{
                lst=min(lst,pos[-a[i]]-1);
                ans[i]=lst-i+1;
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d%c",ans[i],i==n?'\n':' ');
    }
    return 0;
}

B Rearrange Columns

The meaning of problems

There are two lines of character strings, have #and .can rearrange the columns, such that #together.

analysis

  • Calculating two #, one #above and below, and not #, if not two #but #there has on the lower, can not.
  • Otherwise, the two #of you can put the middle.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+55;
char a[2][N];
int main(){
    scanf("%s",a[0]);
    scanf("%s",a[1]);
    int n=strlen(a[0]);
    int shang=0;
    int xia=0;
    int mei=0;
    int liang=0;
    for(int i=0;i<n;i++){
        if(a[0][i]=='#' && a[1][i]=='#'){
            liang++;
        }else if(a[0][i]=='#' && a[1][i]=='.'){
            shang++;
        }else if(a[0][i]=='.' && a[1][i]=='#'){
            xia++;
        }else{
            mei++;
        }
    }
    if(liang || (shang &&!xia) || (xia&&!shang)){
        printf("YES\n");
        for(int i=0;i<mei;i++){
            printf(".");
        }
        for(int i=0;i<shang+liang;i++){
            printf("#");
        }
        for(int i=0;i<xia;i++){
            printf(".");
        }
        printf("\n");
        for(int i=0;i<mei;i++){
            printf(".");
        }
        for(int i=0;i<shang;i++){
            printf(".");
        }
        for(int i=0;i<liang+xia;i++){
            printf("#");
        }
        printf("\n");
    }else{
        printf("NO\n");
    }
    return 0;
}

C Jumps on a Circle

The meaning of problems

A ring labeled 0 to p-1, starting at 0, 2, 3 ... jump every step, jump and asked how many positions will be visited after n steps.

analysis

After the jump min (n, 2p) times will certainly be back to square one again and then jump 1,2,3 ..., so simulation can be.

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e7+50;
ll n,p;
int vis[N];
int main(){
    scanf("%lld%lld",&p,&n);
    vis[0]=1;
    ll now=0;
    for(ll i=1;i<=min(n,2*p);i++){
        now=(now+i)%p;
        vis[now]=1;
    }
    int ans=0;
    for(ll i=0;i<p;i++){
        ans+=vis[i];
    }
    printf("%d\n",ans);
    return 0;
}

D Country Division

The meaning of problems

To a tree, each time asking for some point dyed red, at some point dyed blue, asked if he could make a cut in the side to reach the same color between each other, they can not reach each other in different colors.

analysis

  • Because it is a tree, if you can delete to delete an edge, then certainly enough.
  • 1 is assumed as the root, first find the point lca red, blue and then find the point lca, a side connected to the parent node if two lca not in the same sub-tree, then delete it.
  • If there are two lca ancestral relationship, such as red and blue lca lca of lca lca it is red, then red if the point in pointing blue red lca lca the edge of another, it can, otherwise, illustrate the point in red subtree blue dots.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
struct Edge{
    int v,next;
}e[N*2];
int d[N],fa[N][20],pw[20];
int cnt,head[N];
void init(){
    pw[0]=1;
    for(int i=1;i<=20;i++){
        pw[i]=pw[i-1]*2;
    }
    cnt=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v){
    e[cnt]=Edge{v,head[u]};
    head[u]=cnt++;
    e[cnt]=Edge{u,head[v]};
    head[v]=cnt++;
}
void dfs(int u){
    for(int i=1;pw[i]<=d[u];i++){
        fa[u][i]=fa[fa[u][i-1]][i-1];
    }
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa[u][0]){
            continue;
        }
        fa[v][0]=u;
        d[v]=d[u]+1;
        dfs(v);
    }
}
int lca(int x,int y){
    if(d[x]<d[y]){
        swap(x,y);
    }
    int tmp=d[x]-d[y];
    for(int i=0;pw[i]<=tmp;i++){
        if(tmp&pw[i]){
            x=fa[x][i];
        }
    }
    if(x==y){
        return x;
    }
    for(int i=19;i>=0;i--){
        if(fa[x][i]!=fa[y][i]){
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
}
int n,q,u,v,ri,bi,x,red[N],blue[N];
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    init();
    for(int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    dfs(1);
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&ri,&bi);
        scanf("%d",&red[1]);
        int rlc=red[1];
        for(int i=2;i<=ri;i++){
            scanf("%d",&red[i]);
            rlc=lca(rlc,red[i]);
        }
        scanf("%d",&blue[1]);
        int blc=blue[1];
        for(int i=2;i<=bi;i++){
            scanf("%d",&blue[i]);
            blc=lca(blc,blue[i]);
        }
        int lc=lca(rlc,blc);
        if(rlc!=lc && blc!=lc){
            printf("YES\n");
        }else if(rlc==lc){
            bool flag=false;
            for(int i=1;i<=ri;i++){
                if(lca(red[i],blc)==blc){
                    printf("NO\n");
                    flag=true;
                    break;
                }
            }
            if(!flag){
                printf("YES\n");
            }
        }else if(blc==lc){
            bool flag=false;
            for(int i=1;i<=bi;i++){
                if(lca(blue[i],rlc)==rlc){
                    printf("NO\n");
                    flag=true;
                    break;
                }
            }
            if(!flag){
                printf("YES\n");
            }
        }
    }
    return 0;
}

E Third-Party Software - 2

The meaning of problems

It requires at least a segment covering the entire range.

analysis

Greedy Classic title.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
struct node{
    int l,r,id;
    bool operator <(const node& rhs)const{
        if(l==rhs.l){
            return r>rhs.r;
        }else{
            return l<rhs.l;
        }
    }
}a[N];
int n,m;
vector<int> res;
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].l,&a[i].r);
        a[i].id=i;
    }
    sort(a+1,a+1+n);
    int now=1,i=1,ans=0;
    while(i<=n && now<=m){
        int mx=0;
        int k=0;
        while(i<=n && a[i].l<=now){
            if(a[i].r>mx){
                mx=a[i].r;
                k=i;
            }
            i++;
        }
        if(mx<=now-1){
            ans=-1;
            break;
        }
        res.push_back(a[k].id);
        now=mx+1;
        ans++;
    }
    if(now<=m){
        ans=-1;
    }
    if(ans==-1){
        printf("NO\n");
    }else{
        printf("YES\n");
        printf("%d\n",ans);
        for(int i=0;i<ans;i++){
            printf("%d%c",res[i],i==ans-1?'\n':' ');
        }
    }
    return 0;
}

F Friendly Fire

The meaning of problems

n-monster, a and B has two properties, if a property is greater than another attribute monster B, can kill the monster, is now playing at the same time to select two monsters, you may also die, to make the dead monster a sum of the maximum properties.

analysis

  • A reference to the entry of foreign friends thought https://codeforces.com/blog/entry/67178
  • Considered separately and a sort of b is incremented, to give two sequences, and then enumerate monster first sequence, and a value of the second sequence monsters can defeat this monster added priority queue because certainly these monsters be killed, so directly out of a maximum value, and then look at whether defeat the monster monster out of the enumeration, and then consider whether to update the answer.
  • If you remove the monster is in itself, it removed one attention judgment empty queue, if useless to remove the monsters, but also to re-queue before the Continue .

Code

#include <bits/stdc++.h>
using namespace std;
const int N=3e5+50;
int n;
struct node{
    int a,b,id;
}a[N],b[N];
bool cmp1(node a,node b){
    if(a.a==b.a){
        return a.b<b.b;
    }else{
        return a.a<b.a;
    }
}
bool cmp2(node a,node b){
    if(a.b==b.b){
        return a.a<b.a;
    }else{
        return a.b<b.b;
    }
}
priority_queue<pair<int,int> > q;
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].a,&a[i].b);
        a[i].id=i;
        b[i]=a[i];
    }
    sort(a+1,a+1+n,cmp1);
    sort(b+1,b+1+n,cmp2);
    int ans=0;
    int le=0,ri=0;
    bool up=false;
    for(int i=1,j=1;i<=n;i++){
        while(j<=n && b[j].b<=a[i].a){
            q.push({b[j].a,j});
            j++;
        }
        if(q.empty()){
            continue;
        }
        int mx=q.top().first;
        int idx=q.top().second;
//        printf("%d %d %d\n",i,a[i].a,a[i].b);
//        printf("%d %d %d %d\n",idx,mx,b[idx].id,a[i].id);
        if(b[idx].id==a[i].id){
//            printf("%d F\n",i);
            auto tmp=q.top();
            q.pop();
            if(q.empty()){
                q.push(tmp);
                continue;
            }
            mx=q.top().first;
            idx=q.top().second;
            q.push(tmp);
        }
        int t=mx+(b[idx].a>=a[i].b?a[i].a:0);
        if(t>ans){
            up=true;
            ans=t;
            le=a[i].id;
            ri=b[idx].id;
        }
    }
    printf("%d\n",ans);
    if(!up){
        printf("1 2\n");
    }else{
        printf("%d %d\n",le,ri);
    }
    return 0;
}

H Missing Number

The meaning of problems

0-n has a total number of n + 1, now deleted a number obtained by deleting the number of interactions.

I is the interaction of the interrogation bit number x is 0 or 1.

analysis

  • Ask first least significant bit of each number, respectively, count the number 1, it is clear if no shortage of the number of words, number 1 should be \ (\ FRAC {n-1} {2} + \) , whereby determination can be missing this is a number 0 or 1.

  • You can then exclude half the number of non-compliance, continue to ask the first two.
  • Deposit with the set numerical subscript (ie, the first few numbers) is better written.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
set<int> ns,t;
int main(){
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){
        ns.insert(i);
    }
    //从最低位开始询问
    int i=0;
    int res;
    int ans=0;
    while(ns.size()>=1){
        int sz=ns.size();
        t.clear();
        int b=0;
        for(auto it=ns.begin();it!=ns.end();it++){
            int x=*it;
            //询问第x个数的第i位
            cout << "? " << x << " " << i << endl;
            cout.flush();
            cin >> res;
            if(res){
                t.insert(x);
                b++;
            }
        }
        if(b<(sz+1)/2){
            //缺少的数第i位为1
            ans+=(1<<i);
            ns=t;
        }else{
            for(auto it=t.begin();it!=t.end();it++){
                ns.erase(*it);
            }
        }
        i++;
    }
    cout << "! " << ans << endl;
    return 0;
}

I Painting a Square

The meaning of problems

A small square side length b to a side length of a large square of coloring, the smallest number of steps need to ask to go.

analysis

  • Along the edges of the spiral went is optimal.
  • So to go along this edge of the two long sides, the situation is transformed into a small square of the ab.
  • Local ran out so manually stack simulation, but run out on the evaluation machine.

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b;
ll fun(ll a,ll b){
//    printf("%lld %lld\n",a,b);
    if(a-b<b){
        return 3ll*(a-b);
    }else{
        return fun(a-b,b)+2ll*(a-b)+b;
    }
}
stack<ll>st;
ll solve(ll a,ll b){
    ll aa=a;
    while(aa-b>=b)
    {
        st.push(aa);
        aa-=b;
    }
    ll ans=3ll*(aa-b);
    while(!st.empty())
    {
        ans=ans+2ll*(st.top()-b)+b;
        st.pop();
    }
    return ans;
}
int main(){
    scanf("%lld%lld",&a,&b);
//    ll ans=solve(a,b);
    ll ans=fun(a,b);
    printf("%lld\n",ans);
    return 0;
}

J The Power of the Dark Side - 2

The meaning of problems

n individuals, each person has three attributes, if there are two attributes strictly greater than words can beat another man and asked for everyone, if property values ​​can be adjusted before each battle, the maximum number of people can beat.

analysis

  • Consider the conditions of each person can be defeated, it is clear that the sum of the value of the other property at least (a minimum of two property values ​​+2).
  • So everyone can beat the lower limit of seeking out, sort, and then enumerate the sum of everyone's property, upper_bound () found the number to beat, and then determine whether or not these people there themselves.

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+55;
ll a[N][3];
ll sum[N];
int n;
vector<ll> v;
vector<int> ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld%lld%lld",&a[i][0],&a[i][1],&a[i][2]);
        sum[i]=a[i][0]+a[i][1]+a[i][2];
        sort(a[i],a[i]+3);
        v.push_back(a[i][0]+a[i][1]+2);
    }
    sort(v.begin(),v.end());
    for(int i=1;i<=n;i++){
        int k=upper_bound(v.begin(),v.end(),sum[i])-v.begin();
        ll t=sum[i]-(a[i][0]+a[i][1]+2);
        if(t>=0){
            k--;
        }
        ans.push_back(k);
    }
    for(int i=0;i<n;i++){
        printf("%d%c",ans[i],i==n-1?'\n':' ');
    }
    return 0;
}

K Deck Sorting

The meaning of problems

After the analysis is intended to give a title string containing only RGB, from which a withdrawal sequence, and the remaining sequences pieces together, so that the same character string spliced ​​together.

analysis

  • 6 full enumeration arrangement, i.e. where the stitched string, enumeration RBGs assumed, then R must all be removed. All remaining G, B to consider is, if B is after the last R, that is to follow R out, if it is in front of the first G, G left it to follow, otherwise, that is, if the B final R and a front and then behind the first G, that is not legitimate.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+55;
char s[N];
set<char> lt;
char idx[]={'R','G','B'};
int main(){
    scanf("%s",s+1);
    int n=strlen(s+1);
    for(int i=1;i<=n;i++){
        lt.insert(s[i]);
    }
    int siz=lt.size();
    if(siz<3){
        printf("YES\n");
    }else{
        for(int i=0;i<3;i++){
            char le=idx[i];
            for(int j=0;j<3;j++){
                if(i==j){
                    continue;
                }
                char ri=idx[j];
                char md=idx[3-i-j];
                int leri=-1;
                int rile=-1;
                for(int k=n;k>=1;k--){
                    if(s[k]==le){
                        leri=k;
                        break;
                    }
                }
                for(int k=1;k<=n;k++){
                    if(s[k]==ri){
                        rile=k;
                        break;
                    }
                }
                bool flag=true;
                for(int k=1;k<=n;k++){
                    if(s[k]==md){
                        if(k<leri && k>rile){
                            flag=false;
                            break;
                        }
                    }
                }
                if(flag){
                    printf("YES\n");
                    return 0;
                }
            }
        }
        printf("NO\n");
    }
    return 0;
}

M Shlakoblock is live!

The meaning of problems

There are properties of n and p v properties, now have the option of adding something attributes v 1, each seeking the most desirable thing to p.

analysis

The larger the p, v 1 plus the greater of the expected contribution to the affirmation, so sort of p, v and then add 1 to update the answer, until there is no impact on the answer to exit.

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1050;
int t,n;
struct node{
    ll zi,mu;
};
bool cmp(node a,node b){
    return a.zi*b.mu>a.mu*b.zi;
}
struct info{
    int p,v,id;
    bool operator <(const info& rhs)const{
        return p>rhs.p;
    }
}a[N];
vector<int> ans;
int main(){
    // freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        ll zi=0,mu=0;
        for(int i=1;i<=n;i++){
            scanf("%lld%lld",&a[i].p,&a[i].v);
            zi+=a[i].p*a[i].v;
            mu+=a[i].v;
            a[i].id=i;
        }
        sort(a+1,a+1+n);
        ans.clear();
        node tt{zi,mu};
        for(int i=1;i<=n;i++){
            zi+=a[i].p;
            mu++;
            node tmp={zi,mu};
            if(cmp(tmp,tt)){
                ans.push_back(a[i].id);
                tt=tmp;
            }else{
                break;
            }
        }
        ll g=__gcd(tt.zi,tt.mu);
        printf("%lld/%lld\n",tt.zi/g,tt.mu/g);
        int siz=ans.size();
        sort(ans.begin(),ans.end());
        printf("%d\n",siz);
        if(!siz){
            printf("\n");
        }
        for(int i=0;i<siz;i++){
            printf("%d%c",ans[i],i==siz-1?'\n':' ');
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zxcoder/p/11699333.html