Codeforces Round #568 (Div. 2)

B. Email from Polycarp

Meaning of the questions: Given an original string output string as a keyboard click button issues that may arise more original judge and the output string match

The hello and are matched heellooooooo

 

Pointer to sweep over the original string is greater than last priority 

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=1e6+6;
char s1[N],s2[N];
int main()
{
    int cas;RI(cas);
    while(cas--)
    {
        RS(s1);RS(s2);
        if(strlen(s1)>strlen(s2)){puts("NO");continue;}
        int last=-1;
        int lena=strlen(s1);
        int lenb=strlen(s2);
        int L=0;
        int ok=0;
        rep(i,0,lenb-1)
        {
            if(s2[i]==s1[L]&&L<lena)
            {
                L++;last=s2[i];continue;
            }
            else if( s2[i]==last )
            {
                continue;
            }
            else {L=0;break;}
        }
        if(L==lena)puts("YES");
        else puts("NO");
    }
    return 0;
}
View Code

 

C2. Exam in BerSU (hard version)

There are n items backpack weight limit as m

The i-th weight of the article the number n of output ai before seeking out the i-th minimum number of items enables the article into the backpack (i-answer must contain the i-th item)

Greedy to:

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=2e5+5;

int cnt[N],a[N];
int num;
int main()
{
    ll n,m,x;
    cin>>n>>m;
    rep(i,1,n)
    {
        RI(a[i]);int t=m-a[i],s=0;
        rep(j,1,100)
        {
            if(t>j*cnt[j])t-=j*cnt[j],s+=cnt[j];
            else {s+=t/j;break;}
        }
        printf("%d\n",i-1-s);
        ++cnt[a[i]];
    }
    return 0;
}
View Code

 

D. Extra Element

The meaning of problems: given number n can remove a requirement making a arithmetic sequence

When the game less considered a way to explode wa  

First be sorted first three digits were abc

There are three possible tolerance ba cb ca and simulation can solve the (considering the game when less ca)

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=2e6+5;
struct node
{
    int pos;
    ll v;
}a[N],b[N],c[N];
bool cmp(node a,node b)
{
    return a.v<b.v;
}
int main()
{
    int n;RI(n);
 
    if(n<=3){printf("1");return 0;}
 
    rep(i,1,n)scanf("%lld",&b[i].v),b[i].pos=i,a[i]=c[i]=b[i];
    sort(a+1,a+1+n,cmp);
    sort(b+1,b+1+n,cmp);
    sort(c+1,c+1+n,cmp);
 
    ll d=a[2].v-a[1].v;
    int cnt=0;
    int ok=1;
    int flag=a[1].pos;
 
    rep(i,2,n)
    {
        if(a[i].v-a[i-1].v==d)continue;
        else
        {
            cnt++;
            a[i].v=a[i-1].v;
            flag=a[i].pos;
            if(cnt>1)
            {
                ok=0;break;
            }
        }
    }
    if(ok)
    {
        printf("%d",flag);return 0;
    }
 
     d=b[3].v-b[2].v;
     cnt=1;
     ok=1;
     flag=b[1].pos;
 
    rep(i,3,n)
    {
        if(b[i].v-b[i-1].v==d)continue;
        else
        {
            ok=0;break;
        }
    }
    if(ok)
    {
        printf("%d",flag);return 0;
    }
 
    d=c[3].v-c[1].v;
    ok=1;
    flag=c[2].pos;
    rep(i,4,n)
    {
        if(c[i].v-c[i-1].v==d)continue;
        else
        {
            ok=0;break;
        }
    }
    if(ok)
    {
        printf("%d",flag);return 0;
    }
 
    printf("-1");
 
    return 0;
}
Dai码 of Ushi陋

 

Ultra-efficient solution to a problem:

#include<bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i=a;i<=(b);++i)
#define F2(i,a,b) for(int i=a;i<(b);++i)
#define dF(i,a,b) for(int i=a;i>=(b);--i)
#define MN 200005
#define ll long long
#define mod 998244353
int n;
int a[MN],b[MN],c[MN];
map<int,int>mp;
int cc;
void ad(int x){if(mp[x]==0)++cc;++mp[x];}
void su(int x){--mp[x];if(mp[x]==0)--cc;}
void ans(int v){F(i,1,n)if(b[i]==v){printf("%d\n",i);break;}}
int main(){
    scanf("%d",&n);
    if(n<=3)return puts("1"),0;
    F(i,1,n)scanf("%d",a+i),b[i]=a[i];
    sort(a+1,a+n+1);
    F(i,1,n-1)c[i]=a[i+1]-a[i];
    F(i,1,n-1)ad(c[i]);
    F(i,1,n){
        if(i==1){
            su(c[1]);
            if(cc==1)return ans(a[1]),0;
            ad(c[1]);
        }
        else if(i==n){
            su(c[n-1]);
            if(cc==1)return ans(a[n]),0;
            ad(c[n-1]);
        }
        else{
            su(c[i-1]),su(c[i]),ad(c[i-1]+c[i]);
            if(cc==1)return ans(a[i]),0;
            su(c[i-1]+c[i]),ad(c[i]),ad(c[i-1]);
        }
    }
    puts("-1");
    return 0;
}
View Code

 

E. Polycarp and Snakes

Meaning of the questions: Given a matrix of n rows and m columns represent nothing can be added above lowercase

Determining whether the output is required to meet all the letters article conditions:

Uppercase letters lowercase letters can be covered (that is to draw less)

Isotype and letters can be a straight line must be continuous (except where covered)

Letters must be in ascending order (this can be a good solution to completely cover direct)

Force violence can compare test codes force qaq

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=1e4;
char mp[N][N];
int n,m,ans[N][4],cnt,vis[N];
map<int,pair<int,int> >pos;
map<int,int>flag;
int ok=1;
 
void judge(int id,int x,int y,int flag)
{
    int x1=x,x2=x,y1=y,y2=y;
    ++cnt;
 
    if(flag==0)
    {
        ans[cnt][0]=ans[cnt][2]=x;
        ans[cnt][1]=ans[cnt][3]=y;
    }
    else if(flag==1)
    {
        rep(i,y,m)if(mp[x][i]==id)x2=x,y2=i;
        repp(i,y,1)if(mp[x][i]==id)x1=x,y1=i;
        ans[cnt][0]=x1;ans[cnt][1]=y1,ans[cnt][2]=x2,ans[cnt][3]=y2;
        rep(i,y1,y2)
        if(mp[x][i]<id||mp[x][i]=='.')ok=0;
    }
    else if(flag==2)
    {
        rep(i,x,n)if(mp[i][y]==id) x2=i,y2=y;
        repp(i,x,1)if(mp[i][y]==id)x1=i,y1=y;
        ans[cnt][0]=x1;ans[cnt][1]=y1,ans[cnt][2]=x2,ans[cnt][3]=y2;
        rep(i,x1,x2)
        if(mp[i][y]<id||mp[i][y]=='.')ok=0;
    }
    vis[cnt]=id;
}
 
 
int main()
{
    int cas;RI(cas);
    while(cas--)
    {
        cnt=0;
        vis[0]='a'-1;
        pos.clear();flag.clear();
        RII(n,m);
        rep(i,1,n)RS(mp[i]+1);
 
        ok=1;
        rep(i,1,n)rep(j,1,m)
        {
            if(islower(mp[i][j]))
            {
                if(!pos.count(mp[i][j]))
                pos[mp[i][j]]=make_pair(i,j);
                else
                {
                    if(pos[mp[i][j]].first!=i&&pos[mp[i][j]].second!=j)ok=0;
                    if(pos[mp[i][j]].first==i)
                    {
 
                        if(flag[mp[i][j]]==2){ok=0;break;}
                        else flag[mp[i][j]]=1;
                    }
                    else if(pos[mp[i][j]].second==j)
                    {
                        if(flag[mp[i][j]]==1){ok=0;break;}
                        else flag[mp[i][j]]=2;
                    }
                }
            }
        }
        if(!ok){puts("NO");continue;}
 
        rep(i,'a','z')
        if(pos.count(i))
        {
            judge(i,pos[i].first,pos[i].second,flag[i]);
            if(!ok)break;
        }
 
        if(!ok){puts("NO");}
        else
        {
            puts("YES");
            cout<<vis[cnt]-'a'+1<<endl;
 
            rep(i,1,cnt)
            {
                int temp=vis[i]-vis[i-1];
                while(temp--)
                printf("%d %d %d %d\n",ans[i][0],ans[i][1],ans[i][2],ans[i][3]);
            }
        }
    }
    return 0;
}
View Code

 

F. Two Pizzas

Meaning of the questions: There are n people everyone likes certain kinds of pizza ingredients with m each pizza has a price and contains several ingredients (less than a few ingredients 9)

They like to order two pizzas if a person needs all the ingredients to be satisfied that the person is satisfied  

How to order pizza in the case of most people to meet the minimum price

A pizza and then began to enumerate enumerate people directly out of time altogether triple loop

Considering the state of the small direct enumeration state  

The minimum price maintenance beginning of each state of the same pizza

Well, if the answer is to buy two of the same (ingredients pizza) on the wa

So to maintain the next smallest value

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
#define pb push_back
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
const int N=2e5+10;
 
int n,m,x,a,ans1=1,ans2=2,maxx=0,temp,price,peo[N],minn1[N],minn2[N],id[N],id2[N];
 
ll money=1e14;
int main()
{
    RII(n,m);
    rep(i,1,n)
    {
        int s=0;RI(x);
        while(x--)
        {
            RI(a);s|=1<<(a-1);
        }
        peo[s]++;
    }
    rep(i,0,1<<9)minn1[i]=minn2[i]=inf;
    rep(i,1,m)
    {
        int s=0,c;RII(c,x);
        while(x--)
        {
            RI(a);s|=1<<(a-1);
        }
        if(c<minn1[s]){minn1[s]=c;id[s]=i;}
        else if(c<minn2[s]){minn2[s]=c;id2[s]=i;}
    }
    int tot=1<<9;
    rep(i,0,tot)
    rep(j,i+1,tot)
    if(minn1[i]<inf&&minn1[j]<inf)
    {
 
        int cnt=0;
        rep(k,0,tot)if( ((i|j)&k)==k )cnt+=peo[k];
        if(cnt>maxx){maxx=cnt;ans1=id[i],ans2=id[j];money=minn1[i]+minn1[j];  }
        else if(cnt==maxx&&minn1[i]+minn1[j]<money){ans1=id[i],ans2=id[j];money=minn1[i]+minn1[j];}
    }
 
    rep(i,0,tot)
    if(minn1[i]<inf)
    {
        int cnt=0;
        rep(k,0,tot)if( ((i)&k)==k )cnt+=peo[k];
        if(cnt==maxx&&minn1[i]+minn2[i]<money){ans1=id[i],ans2=id2[i];money=minn1[i]+minn2[i];}
    }
 
    cout<<ans1<<" "<<ans2;
    return 0;
}
View Code

 

G1. Playlist for Polycarp (easy version)

Meaning of the questions: one has time to m n song

Each of the first two elements of time and kind (a total of three kinds)

Obtaining the number of requests that each schedule listing of schedule list total time and do not have exactly the same two types of song to listen continuously to m

Like pressure do [state] [last] indicates the type of the current state of a hearing and the last one to listen to songs

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=1e5;
ll dp[1<<16][4];
const int mod=1e9+7;
ll ans;
int n,m;
int t[N],v[N];
int main()
{
    RII(n,m);
    rep(i,1,n)RII(t[i],v[i]),dp[1<<(i-1)][ v[i] ]=1
 
    rep(state,0,(1<<n)-1 )
    rep(last,1,3)
    rep(now,1,n)
    {
        if(state&1<<(now-1))continue;
        if(last==v[now])continue;
        int k=state|(1<<now-1);
        dp[k][ v[now] ]=(dp[k][v[now]]+dp[state][last])%mod;
    }
    rep(state,1,(1<<n)-1)
    {
        int sum=0;
        rep(j,1,n)
        {
            if(state&(1<<(j-1)))
            sum+=t[j];
        }
        if(sum==m)
        {
            rep(k,1,3)
            ans=(ans+dp[state][k])%mod;
        }
    }
    cout<<ans;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/bxd123/p/11111359.html