「賽後補題」HBCPC2018題目代碼與思路簡析

這次比賽(2018年第二屆河北省大學生程序設計競賽)雖然沒有打,但是題目還是要寫的。未完成的題目(還差比較硬核的四題)和思路分析會陸續更新完。

Problem A 2011 Mex Query

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2011-Mex Query
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define QUICKIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
using ll=long long;
using ull=unsigned long long;

int main()
{
QUICKIO

    unordered_map<int,int> m;
    int n,T; cin>>T;
    bool firstLine=true;
    while(T--)
    {
        //if(firstLine) firstLine=false;
        //else cout<<endl;
        cin>>n; m.clear();
        int maxm=0;
        rep(i,1,n)
        {
            int tmp; cin>>tmp;
            m[tmp]++;
            maxm=max(maxm,tmp);
        }
        rep(i,0,maxm)
        {
            if(m[i]==0)
            {
                cout<<i<<endl; break;
            }
        }
    }
    return 0;
}

Problem B 2012 icebound的商店

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2012-Icebound's Shops
 * Tips: A Simple DP.
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;
using ll=long long;
using ull=unsigned long long;
const int mod=1000000009;
ll dp[20][3005];
int w[20]={0,1,2};
/*
dp[i][j]=dp[1~i][j-w[1~i]]
*/
ll solve(int x,int y)
{
    if(dp[x][y]!=-1)
        return dp[x][y];
    else
    {
        //cout<<"?"<<endl;
        if(y==0) return dp[x][y]=1;
        ll ans=0;
        rep(i,1,x)
        {
            //printf("%d,%d\n",y,w[i]);
            if(y-w[i]>=0)
                ans=(ans+solve(i,y-w[i]))%mod;
            else break;
            //else if(y==w[i]) ans=(ans+solve(i,y-w[i])+1)%mod;
        }
        //printf("dp[%d][%d]=%lld\n",x,y,ans%mod);
        return dp[x][y]=ans%mod;
    }
}
int main()
{
    int T; cin>>T;
    memset(dp,-1,sizeof(dp));
    rep(i,3,15) w[i]=w[i-1]+w[i-2];
    while(T--)
    {
        int x; cin>>x;
        if(T>0)
            cout<<solve(15,x)<<endl;
        else cout<<solve(15,x);
        //rep(i,1,x) cout<<dp[i]<<" "; cout<<endl;
    }
    return 0;
}

Problem C 2013 Nim Game

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2013-Nim Game
 * Notice: The code finishes after my reading the answer.
 * Tips: A Simple Nim Game. Remember a xor b xor b = a,
 * And everything turns out to be okay.
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define QUICKIO                  \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
using namespace std;
using ll=long long;
using ull=unsigned long long;
ll s[1000005];
int main()
{
QUICKIO
    int T; cin>>T;
    while(T--)
    {
        int n,m; cin>>n>>m;
        s[0]=0;
        rep(i,1,n)
        {
            ll tmp; cin>>tmp;
            s[i]=s[i-1]^tmp;
        }
        ll ans=0,mod=1e9+7;
        rep(i,1,m)
        {
            int l,r; cin>>l>>r;
            ans=(ans*2+((s[r]^s[l-1])>0))%mod;    
        }
        cout<<ans<<endl;
    }
    return 0;
}

Problem D 2014 Defending Plan Support

未完成,待做。

Problem E 2015 Bitmap

未完成,待做。

Problem F 2016 神殿

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2016-Temple
 * Tips: It's a problem about implementation and greedy. ;)
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;
using ll=long long;
using ull=unsigned long long;
int main()
{
    ll l,r;
    bool firstLine=true;
    while(cin>>l>>r)
    {
        if(firstLine) firstLine=false;
        else cout<<endl;
        ll ans=l; int pos=-1;
        while(++pos<=32)
        {
            if(((ans>>(pos))&1)==0)
            {
                if(ans+(ll(1)<<pos)<=r)
                    ans+=(1<<pos);
            }
        }
        cout<<ans;
    }
    return 0;
}

Problem G 2017 K Multiple Longest CommomSubsequence

應爲Common。

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2017-K Multiple Longest Commom Subsequence
 * Notice: The code finishes after my reading the answer.
 * Tips: It's adapted from the classical dp problem. However,
 * its rescriction makes it more interesting and difficult. ;)
 * * *
 * Okay, Let's think about the meaning of K Multiple Longest
 * 'Commom'. That means we need to build a com-sub (I make this
 * abbreviation of common subsequence) based on the same substring.
 * Oh f@ck, see the self-documented code! ;)
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define ZERO(x) memset((x),0,sizeof(x))
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define QUICKIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pi=pair<int,int>;
using pii=pair<pi,int>;
int k,n,m,dp[1005][1005];
int a[1005],b[1005];
int pa[1005],pb[1005];
int main()
{
QUICKIO
    int T; cin>>T;
    while(T--)
    {
        cin>>k>>n>>m;
        ZERO(dp);
        memset(pa,0,sizeof(pa));
        memset(pb,0,sizeof(pb));
        rep(i,1,n) cin>>a[i];
        rep(i,1,m) cin>>b[i];
        queue<int> q[1005];
        rep(i,1,n)
        {
            q[a[i]].push(i);
            if(q[a[i]].size()==k)
            {
                pa[i]=q[a[i]].front();
                q[a[i]].pop();
            }
        }
        rep(i,1,n) while(!q[i].empty()) q[i].pop();
        rep(i,1,m)
        {
            q[b[i]].push(i);
            if(q[b[i]].size()==k)
            {
                pb[i]=q[b[i]].front();
                q[b[i]].pop();
            }
            else if(q[b[i]].size()==1) pb[i]=0;
        }
        rep(i,1,n)
            rep(j,1,m)
            {
                int ans=max(dp[i-1][j],dp[i][j-1]);
                if(a[i]==b[j] && pa[i]!=0 && pb[j]!=0)
                    ans=max(ans,dp[pa[i]-1][pb[j]-1]+k);
                dp[i][j]=max(dp[i][j],ans);
            }
        cout<<dp[n][m]<<endl;
    }
    return 0;
}

Problem H 2018 跑圖

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2018-Running out of poisonous areas
 * Tips: The origin algorithm is not fast enough.
 * A BFS is better choice.
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define ZERO(x) memset((x),0,sizeof(x))
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define QUICKIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pi=pair<int,int>;
using pii=pair<pi,int>;
bool matrix[505][505];
int dist[505][505];
bool vis[505][505];
int dx[]={0,1,0,-1},
    dy[]={1,0,-1,0};
int main()
{
QUICKIO

    int n,m;
    //bool firstLine=true;
    while(cin>>n>>m)
    {
        ///if(firstLine) firstLine=false;
        //else cout<<endl;
        ZERO(matrix);
        ZERO(dist);
        ZERO(vis);
        queue<pii> q;
        rep(i,1,n)
            rep(j,1,m)
            {
                cin>>matrix[i][j];
                if(matrix[i][j])
                {
                    q.push(MP(MP(i,j),0));
                    dist[i][j]=0;
                    vis[i][j]=true;
                }

            }
        while(!q.empty())
        {
            auto now=q.front(); q.pop();
            rep(i,0,3)
            {
                int tx=now.fi.fi+dx[i],
                    ty=now.fi.se+dy[i];
                if(tx>=1 && tx<=n && ty>=1 && ty<=m && !vis[tx][ty])
                {
                    vis[tx][ty]=true;
                    dist[tx][ty]=now.se+1;
                    q.push(MP(MP(tx,ty),now.se+1));
                }
            }
        }
        rep(i,1,n)
            rep(j,1,m)
            {
                if(j==m) 
                    cout<<dist[i][j]<<endl;
                else cout<<dist[i][j]<<" ";
            }
    }
    return 0;
}

Problem I 2019 Power Seq

未完成。待做。

Problem J 2020 Beautiful Array

未完成。待做。

Problem K 2021 520

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2021-520
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;
using ll=long long;
using ull=unsigned long long;
ll quick_mod(ll a, ll b, ll n)
{
    if(b==0) return 1%n;
    else if(b==1) return a%n;
    else
    {
        int tmp=quick_mod(a,b/2,n);
        return ((((tmp%n)*tmp)%n)*quick_mod(a,b%2,n))%n;
    }
}
int main()
{
    ll n; cin>>n;
    cout<<quick_mod(2,n,20180520); 
    return 0;
}

Problem L 2022 icebound的賬單

/*
 * Operation China Wall
 * Author: Zuiho
 * Date: 2018-05
 * Problem: 2022-icebound's bill
 */
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;
using ll=long long;
using ull=unsigned long long;

int main()
{
    ll n; cin>>n;
    ll sum=0;
    rep(i,1,n)
    {
        int tmp; cin>>tmp;
        sum+=tmp;
    }
    if(sum>0) cout<<"icebound is happy.";
    else if(sum==0) cout<<"icebound is ok.";
    else cout<<"icebound is sad.";
    return 0;
}

ps:估計下一屆的難度不會這麼easy了2333
單機車七題還是難度不足的,不過也是考慮到廣大兄弟院校了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章