1. 程式人生 > 實用技巧 >Codeforces Round #661 (Div. 3) 題解

Codeforces Round #661 (Div. 3) 題解

Codeforces Round #661 (Div. 3)

  • A. Remove Smallest

  • #include<bits/stdc++.h>
    using namespace std;
    #pragma GCC optimize(2)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long double ld;
    const int N=100;
    int t,n,a[N];
    
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        cin>>t;
        while(t--) {
            cin>>n;
            for(int i=1;i<=n;++i) cin>>a[i];
            sort(a+1,a+1+n);
            int p=0;
            for(int i=2;i<=n;++i) p=max(p,a[i]-a[i-1]);
            if(p>1) cout<<"NO\n";
            else cout<<"YES\n";
        }
        return 0;
    }
    
  • B. Gifts Fixing

  • #include<bits/stdc++.h>
    using namespace std;
    #pragma GCC optimize(2)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long double ld;
    const ll N=100;
    ll n,t,a[N],b[N];
    
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        cin>>t;
        while(t--){
            cin>>n;
            ll ma=1e9+10,mb=1e9+10;
            for(ll i=1;i<=n;++i){
                cin>>a[i];
                ma=min(ma,a[i]);
            }
            for(ll i=1;i<=n;++i){
                cin>>b[i];
                mb=min(mb,b[i]);
            }
            ll ans=0;
            for(ll i=1;i<=n;++i){
                ll ta=a[i]-ma;
                ll tb=b[i]-mb;
                if(ta>tb){
                    ans+=tb;
                    ans+=abs(ta-tb);
                }else{
                    ans+=ta;
                    ans+=abs(tb-ta);
                }
            }
            cout<<ans<<"\n";
        }
        return 0;
    }
    
  • C. Boats Competition

  • #include<bits/stdc++.h>
    using namespace std;
    #pragma GCC optimize(2)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long double ld;
    const int N = 100;
    int t, n, a[N], ans, rec;
    int inq[N];
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cin >> t;
        while (t--) {
            cin >> n;
            memset(inq, 0, sizeof(int) * (n + 1));
            for (int i = 1; i <= n; ++i) cin >> a[i];
            sort(a + 1, a + 1 + n);
            //int l=2,r=a[n]*2;
            int s = a[n] + a[n - 1];
            ans = 0;
            for (; s >= 2; s--) {
                int ta = 0;
                memset(inq, 0, sizeof(int) * (n + 1));
                for (int i = 1; i <= n; ++i) {
                    if (inq[i]) continue;
                    int tmp = s - a[i];
                    inq[i] = 1;
                    int j = lower_bound(a + 1, a + 1 + n, tmp) - a ;
                    if (a[i] + a[j] == s) {
                        while (inq[j] && a[i] + a[j] == s) j++;
                        if (!inq[j] && a[i] + a[j] == s) {
                            ta++;
                            inq[j] = 1;
                            inq[i] = 1;
                        }
                    }
                }
                ans = max(ta, ans);
            }
            cout << ans << "\n";
        }
        return 0;
    }
    
  • D. Binary String To Subsequences

  • #include<bits/stdc++.h>
    using namespace std;
    #pragma GCC optimize(2)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long double ld;
    int t,n;
    string str;
    const int N=2e5+10;
    int pos[N];
    int rec[N];
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        cin>>t;
        while(t--){
            cin>>n;
            cin>>str;
            int len=str.size();
            memset(rec,-1,sizeof(int)*(n+1));
            int mcnt,cnt;
            mcnt=cnt=1;
            char w='0';
            //int flag=0;
            for(int i=0;i<len;++i){
                int j;
                for (j = i; j < len; ++j) {
                    if (str[j] == str[i]) {
                        if(rec[cnt]!=str[j]-w){
                            pos[j]=cnt;
                            rec[cnt]=str[j]-w;
                            cnt++;
                            mcnt=max(mcnt,cnt-1);
                        } else {
                            while(rec[cnt]==str[j]-w) cnt++;
                            pos[j]=cnt;
                            rec[cnt]=str[j]-w;
                            cnt++;
                            mcnt=max(cnt-1,mcnt);
                        }
                    } else {
                        cnt = 1;
                        break;
                    }
                }
                i = j-1;
            }
            cout<<mcnt<<"\n";
            for(int i=0;i<len;++i) cout<<pos[i]<<" ";
            cout<<"\n";
        }
        return 0;
    }
    

前四道都不是很難就不寫文字了,看程式碼就行 EF等空了填坑吧