zoukankan      html  css  js  c++  java
  • Codeforces Round #686 (Div. 3)

    Codeforces Round #686 (Div. 3)

    Codeforces Round #686

    A.Special Permutation

    水题 直接先输出2-n,在输出1

    #include <bits/stdc++.h>
    using namespace std;
    
    int main()
    {
     
        int t;
        cin >> t;
        while(t--){
            int n;
            cin >> n;
            for(int i = 2; i <= n ; i++){
                cout << i << " ";
            }
            cout << 1 << endl;
        }
     
        return 0;
    }
    

    B. Unique Bid Auction

    第二题写的时候最开始还没过,很郁闷,最后仔细想了下,发现最后for判断的时候只用判断a[i] > 0 而且只要有答案了直接就break就可以了。

    写这个题可以反思一下

    第一次没过不要紧,我debug的思路是从条件出发,就和做数学题一样,这个题主要的两个条件就是唯一性和最小性只要能把握这两点答案就肯定是正确的了

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn =2e5+10;
    int a[maxn];
    
    int main()
    {
    
        int t; /// t -> 2e4
        cin >> t;
        while(t--){
            int n;  /// n -> 2e5 __. O(n);
            cin >> n;
            memset(a,0,sizeof a );
            for(int i = 1; i <= n ; i++){
                int temp;
                cin >>temp;
                if(a[temp] == 0){
                    a[temp] = i;
                }
                else{
                    a[temp] = -1;
                }
            }
    
            int win = 0;
            int winNum = 300000;
            for(int i = 1; i <= n; i++){
                if( a[i] > 0 ){
                    win = a[i];
                    winNum = i;
                    break;
                }
            }
    
            if(win > 0) cout << win << endl;
            else cout << -1 << endl;
        }
    
        return 0;
    }
    

    C. Sequence Transformation

    这个题,我炸裂。。。
    我开始想的是要把每组数都筛出来,存下来,然后去csdn看了大佬的代码,是我想多了。。。。直接边读边判就可以

    #include <bits/stdc++.h>
    using namespace std;
    
    void file()
    {
        #ifdef ON
        #else
            freopen("d:/workProgram/test.txt","r",stdin);
        #endif
    
    }
    int a[1000000];
    
    int main()
    {
        ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        //file();
        int t ;
        cin >> t;
        map<int,int> mp;///记录上次这个数出现的位置;
        map<int ,int> mmp;///记录这个数的操作数;
        while(t--){
            int n ;
            cin >> n;
            if(n == 1){
                int temp;
                cin >>temp;
                cout << 0 << endl;
                continue;
            }
    
            mp.clear();
            mmp.clear();
    
            for(int i = 1; i <= n; i++){
    
                cin >> a[i];
                if( i - mp[a[i]] > 1 ){ ///i-mp[a[i]]是判断两个相同数是否相邻
                    mmp[a[i]]++;
                    mp[a[i]] = i;
                }
                else {
                    mp[a[i]] = i;
                }
            }
    
            mmp[a[n]]--;///因为每个数字最后出现在哪是不确定的,如果不在最后一位
                        ///所以最后一个数先减1,后面会加回去的
    
            int ans = 0x3f3f3f3f;
            for(int i = 1 ; i <= n; i++){
                ans = min( ans, mmp[a[i]] + 1 );
            }
    
            cout << ans << endl;
        }
        //cout <<0x3f<<endl;
        return 0;
    }
    
    

    之前自己模拟的代码。。。

    #include <bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> PII;
    const int maxn =1e5 + 10;
    
    int a[maxn];
    PII pa[maxn];
    
    void file()
    {
        #ifdef ONLINE_JUDGE
        #else
            freopen("D:/workProgram/test.txt","r",stdin);
        #endif
    
    }
    
    bool cmp(PII a,PII b)
    {
        return a.first < b.first;
    }
    
    int main()
    {
        file();
        int t;
        cin >> t;
        while(t--){
            int n ;
            cin >> n;
            map<int,int> m;
            for(int i = 0; i < n; i++){
                cin >> a[i];
                pa[i].first = a[i];
                pa[i].second = i;
            }
    
            int flag = 1;
            for(int i = 1 ; i < n ; i++){
                //cout << pa[i].first << " "<<pa[i - 1].first<<endl;
                if(pa[i].first != pa[i - 1].first){
                        //cout << "no"<<endl;
                    flag = 0;
                    break;
                }
            }
    
            if(flag == 1){
                cout << 0 << endl;
                continue;
            }
    
            sort(pa,pa+n,cmp);
    //        for(int i = 0 ; i < n ; i++){
    //            cout << "I "<<pa[i].first << " "<<pa[i].second<<endl;
    //        }
    
            set<int > q;
            int ans = 1000000;
            for(int i = 0 ; i  < n ; i++){
                if(pa[i].first == pa[i+1].first){
                    q.insert(pa[i].second);
                    continue;
                }
                if(pa[i].first != pa[i+1].first){
                    q.insert(pa[i].second);
                    q.insert(0);
                    q.insert(n - 1);
    //                for(auto item : q) cout << item << " ";
    //                cout <<endl;
                    int cnt = 0;
                    int las = 0;
                    for(auto item : q){
                        if(item != n-1){
    //                        cout<<"I" << item << las << endl;
                            if( (item - 1 - las) != 0   ){
                                cnt++;
                                las = item;
                            }
                            else{
                                //cout<<"I " << pa[item].first << " "<< pa[item - 1].first<<endl;
                                if( a[item] == a[item - 1] ){
                                    continue;
                                    las = item;
                                }
                                else{
                                    cnt++;
                                    las = item;
                                }
                            }
    
                        }
                    }
                    if(cnt < ans)ans = cnt;
    //              cout << cnt<<endl;
    //              cout <<endl;
                    q.clear();
                }
            }
            cout << ans<< endl;
        }
    
    
        return 0;
    }
    
    
  • 相关阅读:
    新版眼保健操图解(转)
    新经理从IBM来
    宝宝六个月零五天,从床上摔下。。。。。
    [转] 教小朋友画画
    宝宝辅食食谱(4、5、6、7个月)
    永远的乔布斯
    北京一场大暴雨,暴出几句经典语
    [转] 想做永久夫妻就要看
    宝宝身高和体重踪迹
    go语言基础之包和自定义包与main包
  • 原文地址:https://www.cnblogs.com/hoppz/p/14045705.html
Copyright © 2011-2022 走看看