Codeforces Round #590 (Div. 3)

Posted lin1874

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #590 (Div. 3)相关的知识,希望对你有一定的参考价值。

A. Equalize Prices Again

签到题;

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
int a[MAXN];
 
int main()
    int T;
    cin>>T;
    while(T--)
        int n;
        cin>>n;
        int sum=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
            sum+=a[i];
        
        int ans=sum/n;
        if(ans*n<sum)cout<<ans+1<<endl;
        else cout<<ans<<endl;
 
    
 
 
 
 
    return 0;
View Code

B2. Social Network (hard version)

map+queue

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+10;
int a[MAXN];
int n,k;
queue<int>que;
map<int,int>mp;
vector<int>vec;
int main()
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    
    for(int i=1;i<=n;i++)
        if(k>0)
            if(mp[a[i]]==0)
                que.push(a[i]);
                k--;
                mp[a[i]]=1;
            
            else continue;
        
        else
            if(mp[a[i]]==1)
                continue;
            
            else
                int w=que.front();que.pop();
                mp[w]=0;
                mp[a[i]]=1;
                que.push(a[i]);
            
        
    
    while(!que.empty())
        vec.push_back(que.front());que.pop();
    
    reverse(vec.begin(),vec.end());
    cout<<vec.size()<<endl;
    for(int i=0;i<vec.size();i++)
        cout<<vec[i]<< ;
    cout<<endl;
 
 
 
 
    return 0;
View Code

C. Pipes

爆搜(居然写了快一个小时)

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+10;
int a[5][MAXN];
bool vis[5][MAXN];
string s;
int n;//last  up 1 down 2 left 3
bool dfs(int x,int y,int last)
    //cout<<"x="<<x<<‘ ‘<<"y="<<y<<"last="<<last<<endl;
    if(!vis[x][y])return false;
    if(y==(n+1)&&x==2)return true;
    if(y>=(n+1))return false;
    if(x<1||x>2)return false;
    if(a[x][y])
        if(last==1||last==2)
            vis[x][y+1]=true;
            if(dfs(x,y+1,3))return true;
            else vis[x][y+1]=false;
        
        else if(last==3)
            if(x==1)
                vis[x+1][y]=true;
                if(dfs(x+1,y,1))return true;
                else vis[x+1][y]=false;
            
            else 
                vis[x-1][y]=true;
                if(dfs(x-1,y,2))return true;
                else vis[x-1][y]=false;
            
        
    
    else 
        if(last==3)
            vis[x][y+1]=true;
            if(dfs(x,y+1,3))return true;
            else vis[x][y+1]=false;
        
    
    return false;

int main()
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
        cin>>n;
        for(int i=1;i<=2;i++)
            cin>>s;
            for(int j=0;j<n;j++)
                if((s[j]-0)<=2)a[i][j+1]=0;
                else a[i][j+1]=1;
                vis[i][j+1]=false;
            
        
        vis[1][n+1]=vis[2][n+1]=false;
        vis[1][1]=true;
        if(dfs(1,1,3))
            cout<<"YES"<<endl;
        
        else cout<<"NO"<<endl;
    
 
 
 
 
    return 0;
View Code

D. Distinct Characters Queries

线段树:二进制存字母

技术图片
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
int tree[MAXN*4];
int tot=0;
char s[MAXN];
char t[5];
int n;
void build(int root,int l,int r)
    if(l==r)
        tree[root]=1<<(s[l]-a);
        return ;
    
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    tree[root]=tree[root<<1]|tree[root<<1|1];

void gao(int root,int l,int r,int p,char x)
    if(l==r)
        int w=1<<(x-a);
        tree[root]=w;
        return ;
    
    int mid=(l+r)>>1;
    if(p<=mid)gao(root<<1,l,mid,p,x);
    else gao(root<<1|1,mid+1,r,p,x);
    tree[root]=tree[root<<1]|tree[root<<1|1];

int query(int root,int l,int r,int L,int R)
    if(L>r||R<l)return 0;
    if(l>=L&&R>=r)return tree[root];
    int mid=(l+r)>>1;
    int res=0;
    res|=query(root<<1,l,mid,L,R);
    res|=query(root<<1|1,mid+1,r,L,R);
    return res;
 

int main()
    scanf("%s",s+1);
    n=strlen(s+1);
    build(1,1,n);
    int T;
    scanf("%d",&T);
    while(T--)
        int o;
        scanf("%d",&o);
        if(o==1)
            int p;
            scanf("%d%s",&p,t);
            //cout<<p<<" "<<t[0]<<endl;
            gao(1,1,n,p,t[0]);
        
        else 
            int l,r;
            scanf("%d%d",&l,&r);
            int res=query(1,1,n,l,r);
            int ans=0;
            while(res)
                if(res&1)ans++;
                res>>=1;
            
            printf("%d\n",ans);
        
    
 
 
 
    return 0;
View Code

 

以上是关于Codeforces Round #590 (Div. 3)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #590 (Div. 3)

Codeforces Round #590 (Div. 3) C. Pipes

Codeforces Round #590 (Div. 3)

Codeforces Round #590 (Div. 3)

CodeForces Round #590 (Div 3)

Codeforces Round #590 (Div. 3)