zoukankan      html  css  js  c++  java
  • Codeforces Round #578 (Div. 2) 训练总结及题解

    这场A读错了题12mins1A,B题卡读题18mins1A,C题还行13mins1A,D想了想觉得写起来有点慢,转头看E,发现比较简单,wa了2发,T了2发之后才A用了50mins+。F场后看的不太会,D题学了个二维预处理的方法。
    总结:

    1. 打现场赛的时候不能题不读完就开始做题,尤其签到要细想。
    2. E题的有一发T是函数传递T掉的。
      题解:
      A.
      题意:10个房间,L入住最左边,R入住最右边,数字代表第几间房间离开。问1e5次操作后,房间入住情况,01表示。
      思路:一开始读错题了,以为1e5个用二分做的。
      代码:
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define forn(i,n) for(int i =0;i<n;i++)
    #define for1(i,n) for(int i=1;i<=n;i++)
    #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const int maxn=  1e5+5;
    bool a[maxn];
    
    int main(){ 
        IO;
        set<int>l,r;
        int n;cin>>n;
        string s;cin>>s;
        forn(i,10){
            l.insert(i);
            r.insert(i);
        }
        forn(i,n){
            if(s[i]=='L'){
                int x = *l.begin();
                a[x] = 1;
                l.erase(l.find(x));
                r.erase(r.find(10-x-1));
            }
            else if(s[i]=='R'){
                int x = *r.begin();
                x = 10-x-1;
                a[x] = 1;
                l.erase(l.find(x));
                r.erase(r.find(10-x-1));
            }else{
                int x = s[i]-'0';
                a[x] = 0;
                l.insert(x);
                r.insert(10-x-1);
            }
        }
        forn(i,10) cout<<a[i];
    
    
    
    
        return 0;
    }
    

    B.
    题意:n个柱子,每个高度hi,两两之间跨越必须满足相邻且hi相差<=k。初始携带m块砖从第1个柱子出发,每次可以取出1-hi块砖或者放入一些砖,每放入一块砖hi++,取出hi–。问能否走到第n个柱子。n1e5
    思路:贪心,每次使这块砖为hi+1-k
    代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define forn(i,n) for(ll i =0;i<n;i++)
    #define for1(i,n) for(ll i=1;i<=n;i++)
    #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    const ll inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    
    
    int main(){ 
        IO;
        ll t;cin>>t;
        while(t--){
            ll n,m,k;cin>>n>>m>>k;
            vector<ll>a(n);forn(i,n) cin>>a[i];
            bool ok = 0;
            forn(i,n){
                if(i==n-1){
                    ok = 1;
                    break;
                }
                ll x = max(0ll,a[i+1]-k);
                if(a[i]>=x) m+=a[i]-x;
                else m-=(x-a[i]);
                //cerr<<m<<' '<<i<<'
    ';
                if(m<0)break;
            }
            if(ok) cout<<"YES"<<'
    ';
            else cout<<"NO"<<'
    ';
        }
    
    
    
    
    
        return 0;
    }
    

    C.
    题意:两个同心圆,一大一小,大的被分为n块,小的被分为m块,没有分割时12点钟方向有一个挡板。现在1e4次询问,问大或小的第x块能否走到大或小的第y块。
    思路:gcd去同边,O(1e4*logn+m)
    代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define forn(i,n) for(int i =0;i<n;i++)
    #define for1(i,n) for(int i=1;i<=n;i++)
    #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    
    
    int main(){ 
        IO;
        ll n,m,q;cin>>n>>m>>q;
        ll x = __gcd(n,m);
        ll a = n/x,b = m/x;
        while(q--){
            ll x,y,xx,yy;cin>>x>>y>>xx>>yy;
            y--,yy--;
            if(x==1) y/=a;
            else y/=b;
            if(xx==1) yy/=a;
            else yy/=b;
            if(y==yy) cout<<"YES"<<'
    ';
            else cout<<"NO"<<'
    ';
        }
        return 0;
    }
    

    D.
    题意:一个长2000的正方形矩阵,只有W和B两种字符,现在给了一个长度为k(1-2000)的橡皮,可以把这个正方形面积由b变为w。问擦一次可以使多少行和列的字符全为w.
    思路:二维预处理,取每个可贡献的行或列的可取点的矩形,预处理左上角++,右下角++,左下角–,右上角–。最终答案记录posij+=posi-1j posij+=posij-1 posij+==posi-1j-1。
    代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define forn(i,n) for(int i=0;i<n;i++)
    #define for1(i,n) for(int i=1;i<=n;i++)
    #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10)
    const int maxn = 2e3+5;
    int pos[maxn][maxn];
    string s[maxn];
    
    int main(){
    	IO;cout<<fixed;
    	int n,k;cin>>n>>k;
    	forn(i,n){
    		cin>>s[i];
    		for(auto &x:s[i]) x = x=='B';
    	}
    	forn(i,n) {
    		int l = n+1,r = -1;
    		forn(j,n)if(s[i][j]) l = min(j,l),r =  max(r,j);
    		if(r-l>k-1) continue;
    		if(l>r) pos[0][0]++;
    		else {
    			pos[max(0,i-k+1)][max(r-k+1,0)]++;
    			pos[max(0,i-k+1)][l+1]--;
    			pos[i+1][max(r-k+1,0)]--;
    			pos[i+1][l+1]++;
    		}
    	}
    	forn(j,n){
    		int l = n+1,r = -1;
    		forn(i,n) if(s[i][j]) l = min(i,l),r = max(r,i);
    		if(r-l>k-1) continue;
    		if(l>r) pos[0][0]++;
    		else{
    			pos[max(0,r-k+1)][max(0,j-k+1)]++;
    			pos[max(0,r-k+1)][j+1]--;
    			pos[l+1][max(0,j-k+1)]--;
    			pos[l+1][j+1]++;
    		}
    	}
    	// forn(i,n+1){
    	// 	forn(j,n+1) cerr<<pos[i][j]<<' ';
    	// 	cerr<<'
    ';
    	// }
    	int ans = 0,res = 0;
    	forn(i,n+1) forn(j,n+1){
    		if(i) pos[i][j]+=pos[i-1][j];
    		if(j) pos[i][j]+=pos[i][j-1];
    		if(i&&j) pos[i][j]-=pos[i-1][j-1];
    		res = max(pos[i][j],res);
    	}
    	cout << res <<'
    ';
    	return 0;
    }
    

    E.
    题意:1e5个字符串从长度1e6,从左到右,每次合并上一次的答案最长匹配当前字符串前缀的后缀。
    思路:kmp返回j指针最后所指,注意细节。
    代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define forn(i,n) for(int i =0;i<n;i++)
    #define for1(i,n) for(int i=1;i<=n;i++)
    #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const int maxn = 1e6+5;
    int nex[maxn];
    void getnex(string s){
        int len = s.size(),i = 0,j = -1;
        nex[0] = -1;
        while(i<len){
            if(j==-1||s[i]==s[j]) nex[++i] = ++j;
            else j = nex[j];
        }
    }
    int kmp(string s,string m){
        int len = s.size(),len2 = m.size(),i = 0,j = 0;
        while(i<len){
            if(s[i]==m[j]||j==-1) i++,j++;
            else j = nex[j];
        }
        return j;
    }
    
    int main(){ 
        IO;
        int n;cin>>n;
        vector<string>a(n);
        forn(i,n)cin>>a[i];
        string ans = "";
        ans+=a[0];
        int len = ans.size();
        for(int i = 1;i<n;i++){
            getnex(a[i]);
            string z = "";
            int len2 = a[i].size();
            if(len2<len)forn(j,len2) z+=ans[len-len2+j];
            else z = ans;
            //cerr<<z<<'
    ';
            int x = kmp(z,a[i]);
            for(int j = x;j<a[i].size();j++){
                ans+=a[i][j];
            }
            len += len2-x;
        }
        for(auto x:ans) cout<<x;
        cout<<'
    ';
        return 0;
    }
    

    F.
    题意:懒得描述了,等ac了补。

    人一我百,人十我万。
  • 相关阅读:
    【Android UI设计与开发】第17期:滑动菜单栏(二)开源项目SlidingMenu的示例
    [Usaco2008 Dec]Patting Heads
    Hibernate(六)——多对多关联映射
    2017第2周一小程序
    2017第一周日
    2017第一周六
    无论你如何努力,总会有人讨厌你
    word使用技巧-批量删除图片技巧
    Web跨域问题总结
    JAVA语言的本质优势
  • 原文地址:https://www.cnblogs.com/AlexPanda/p/12520314.html
Copyright © 2011-2022 走看看