Codefest19受虐記

date: 2019-08-28ios

前言

比賽連接:Codefest 19c++

A題

思路:

這是一道水題。你對着樣例遞推打一個表出來,會發現結果三個一組循環。spa

例如:A = [3, 4, 7, 3, 4, 7, 3, 4, 7, ...]code

好了,咱們只須要讀入第0和第1項,把他們的異或值做爲第2項,輸出n對3取模的那一項就好。ci

代碼:

#include<bits/stdc++.h>
using namespace std;

int T,a[3],n;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>T;
    while(T--){
        cin>>a[0]>>a[1]>>n;
        a[2]=a[0]^a[1];
        cout<<a[n%3]<<endl;
    }

    return 0;
}

B題

思路:

這道題很容易暴力而且超時,首先用O(N^2)二重循環枚舉去掉的區間,以後再O(NlogN)從頭至尾跑一遍判重,複雜度是O(N^3logN)get

可是,咱們先枚舉從頭開始的留下來的區間的長度(保證內部沒有重複的),對於每一次枚舉,右邊的留下的長度都知足單調性,可是應該不能夠二分(也可能我不會),相反,直接從右往左跑一遍判重就能夠了。複雜度O(N^2logN)(log是由於使用了set來判重)。it

代碼:

#include<bits/stdc++.h>
using namespace std;

int n;
int a[2005];
set<int> s;
int ans,l,r;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    ans=n;
    for(int i=0;i<=n;i++){
        if(s.find(a[i])!=s.end()){
            break;
        }
        s.insert(a[i]);
        set<int> t;
        for(int j=n;j>=1;j--){
            if(s.find(a[j])!=s.end()||t.find(a[j])!=t.end()){
                ans=min(ans,j-i);
                break;
            }
            t.insert(a[j]);
        }
    }
    cout<<ans<<endl;

    return 0;
}

C題

思路:

複製不少遍樣例就能夠AC(真的)。io

你把大的矩陣分割成不少4行4列的矩陣,以後你就能夠把樣例1的Output填上去。從左往右,從上往下第i個(從0開始數)矩陣的元素要加上16*iclass

代碼:

此處我沒有複製樣例直接使用以下矩陣做爲複製母版:test

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

其實他們的異或值也是相等的。

#include<bits/stdc++.h>
using namespace std;

int n;
int cur;
int g[1005][1005];

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    for(int i=0;i<n;i+=4){
        for(int j=0;j<n;j+=4){
            for(int k=0;k<4;k++){
                for(int l=0;l<4;l++){
                    g[i+k][j+l]=cur++;
                }
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout<<g[i][j]<<' ';
        }
        cout<<endl;
    }

    return 0;
}

結束

從D開始我都不會作,因此就醬~

相關文章
相關標籤/搜索