Codeforces Round #619 (Div. 2)

题目

A - Three Strings


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        bool flag=true;
        for(int i=0;i<a.size();i++){
            if(c[i]!=a[i]&&c[i]!=b[i]){
                flag=false;
                break;
            }
        }
        if(flag)
            puts("YES");
        else
            puts("NO");
    }
}

B - Motarack’s Birthday

By JLU_GLHF#, contest: Codeforces Round #619 (Div. 2), problem: (B) Motarack's Birthday, Accepted, #, Copy
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e18;
const ll maxn=1e5+10;
ll a[maxn];
ll ABS(ll x){
    return x<0?-x:x;
}
int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        ll MAX=0,MIN=INF;
        for(ll i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        for(ll i=1;i<=n;i++){
            if(a[i]!=-1){
                if((i>1&&a[i-1]==-1)||(i<n&&a[i+1]==-1))
                    MAX=max(MAX,a[i]),MIN=min(MIN,a[i]);
            }
        }
        ll k;
        if(MAX==0&&MIN==INF)
            k=0;
        else
            k=MIN+(MAX-MIN+1)/2;
        for(int i=1;i<=n;i++){
            if(a[i]==-1)
                a[i]=k;
        }
        ll ans=0;
        for(int i=2;i<=n;i++){
            ans=max(ABS(a[i]-a[i-1]),ans);
        }
        cout<<ans<<' '<<k<<endl;
    }
}

C - Ayoub’s function

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n,m;
        cin>>n>>m;
        ll SIZE=(n+1)/(m+1)-1;
        ll a=(m+1)*(SIZE+2)-n-1;
        ll b=m+1-a;
        ll ans=n*(n+1)/2-a*SIZE*(SIZE+1)/2-b*(SIZE+1)*(SIZE+2)/2;
        cout<<ans<<endl;
    }
}

D - Time to Run

#include<bits/stdc++.h>
using namespace std;
vector<pair<string ,int>>vec,ans;
int main(){
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<n;i++){
        if(m-1){
            vec.push_back(make_pair("R",m-1));
            vec.push_back(make_pair("L",m-1));
        }
        vec.push_back(make_pair("D",1));
    }
    if(m-1){
        vec.push_back(make_pair("R",m-1));
    }
    for(int i=0;i<m-1;i++){
        if(n-1){
            vec.push_back(make_pair("U",n-1));
            vec.push_back(make_pair("D",n-1));
        }
        vec.push_back(make_pair("L",1));
    }
    if(n-1){
        vec.push_back(make_pair("U",n-1));
    }
    for(int i=0;i<vec.size();i++){
        if(k>=vec[i].second){
            k-=vec[i].second;
            ans.push_back(vec[i]);
        }
        else{
            if(k){
                ans.push_back(make_pair(vec[i].first,k));
                k=0;
            }
            break;
        }
    }
    if(k){
        puts("NO");
    }
    else{
        puts("YES");
        cout<<ans.size()<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i].second<<' '<<ans[i].first<<endl;
        }
    }
}

E - Nanosoft

很好的一个板子二维st板子,查找矩阵中的最大最小值,预处理O(nmlogn),查询O(1)。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
const int maxm = 10;
string s[maxn];
int R[4][maxn][maxn];
int n,m,q;
int val[maxn][maxn],f[maxm][maxm][maxn][maxn],lg[maxn];;
void Build_2D_Sparse_Table(int n, int m){
    int i, j, k1, k2;
 
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            f[0][0][i+1][j+1]=val[i][j];
        }
    }
 
    for(i = 2; i < maxn; i++)
        lg[i] = 1 + lg[i/2];
 
    for(i = 1; i <= n; i++)
        for(k2 = 1; (1 << k2) <= m; k2++)
            for(j = 1; j <= m - (1 << k2) + 1; j++)
                f[0][k2][i][j] = max(f[0][k2 - 1][i][j], f[0][k2 - 1][i][j + (1 << (k2 - 1))]);
 
    for(k1 = 1; (1 << k1) <= n; k1++)
        for(i = 1; i <= n - (1 << k1) + 1; i++)
            for(k2 = 0; (1 << k2) <= m; k2++)
                for(j = 1; j <= m - (1 << k2) + 1; j++)
                    f[k1][k2][i][j] = max(f[k1 - 1][k2][i][j], f[k1 - 1][k2][i + (1 << (k1 - 1))][j]);
}
 
int Query(int x1, int y1, int x2, int y2){
    int k1 = lg[x2 - x1 + 1], k2 = lg[y2 - y1 + 1];
    x2 = x2 - (1 << k1) + 1;
    y2 = y2 - (1 << k2) + 1;
    return max(max(f[k1][k2][x1][y1],f[k1][k2][x1][y2]),max(f[k1][k2][x2][y1],f[k1][k2][x2][y2]));
}
void color(char c,int fx,int fy,int k){
    int stx=0,sty=0;
    if(fx==-1)stx=n-1;
    if(fy==-1)sty=m-1;
    while(stx<n&&stx>=0){
        while(sty<m&&sty>=0){
            if(s[stx][sty]==c){
                R[k][stx][sty]=1;
                if(stx-fx>=0&&stx-fx<n&&sty-fy>=0&&sty-fy<m){
                    if(s[stx-fx][sty]==c&&s[stx][sty-fy]==c&&s[stx-fx][sty-fy]==c){
                        R[k][stx][sty]=min(R[k][stx-fx][sty],
                                         min(R[k][stx][sty-fy],R[k][stx-fx][sty-fy]))+1;
                    }
                }
            }
            sty+=fy;
        }
        sty=0;if(fy==-1)sty=m-1;
        stx+=fx;
    }
}
bool check(int r1,int c1,int r2,int c2,int mid){
    if(mid==0)return true;
    if(mid==min(r2-r1,c2-c1)+1)return false;
    r1=r1+mid-1;
    c1=c1+mid-1;
    r2=r2-mid;
    c2=c2-mid;
    if(r1>r2||c1>c2)return false;
    return Query(r1,c1,r2,c2)>=mid;
}
 
int main(){
    cin>>n>>m>>q;
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    color('R',1,1,0);
    color('Y',-1,1,1);
    color('G',1,-1,2);
    color('B',-1,-1,3);
    for(int i=0;i<n-1;i++){
        for(int j=0;j<m-1;j++){
            if(s[i][j]=='R'&&s[i+1][j]=='Y'&&s[i][j+1]=='G'&&s[i+1][j+1]=='B'){
                val[i][j]=min(R[0][i][j],min(R[1][i+1][j],min(R[2][i][j+1],R[3][i+1][j+1])));
            }
        }
    }
    Build_2D_Sparse_Table(n,m);
 
    for(int i=0;i<q;i++){
        int r1,c1,r2,c2;
        scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
        int l=0,r=min(r2-r1,c2-c1)+1;
        while(r-l>1){
            int mid=(l+r)/2;
            if(check(r1,c1,r2,c2,mid)){
                l=mid;
            }else{
                r=mid;
            }
        }
        printf("%d\n",l*l*4);
    }
}
Codeforces Round #619 (Div. 2)Codeforces Round #619 (Div. 2) GL&HF 发布了38 篇原创文章 · 获赞 6 · 访问量 1387 私信 关注
上一篇:Easy之619:只出现一次的最大数字


下一篇:Yarn架构