5.24 第十五屆中北大學ACM競賽水題題解

Powered by:AB_IN 局外人

A 俄羅斯方塊

純模擬,座標顛倒要注意,數組座標和題目座標不一樣。

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef long long ll;
using namespace std;
const ll maxn=1e6+10;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
ll n,id,y,a[15][15];
int main()
{
    n=read();
    while(n--){
        id=read();y=read();
        int x=1;
        if(id==1){
            while(!a[x][y]&&!a[x-1][y]&&!a[x][y+1]&&!a[x-1][y+1]&&x<=10)
                x++;
            x--;
            a[x][y]=a[x][y+1]=a[x-1][y]=a[x-1][y+1]=1;
        }
        if(id==2){
            while(!a[x][y]&&!a[x-1][y]&&!a[x][y+1]&&!a[x][y+2]&&x<=10)
                x++;
            x--;
            a[x][y]=a[x-1][y]=a[x][y+1]=a[x][y+2]=1;
        }
        if(id==3){
            while(!a[x][y]&&!a[x][y+1]&&!a[x][y+2]&&!a[x][y+3]&&x<=10)
                x++;
            x--;
            a[x][y]=a[x][y+1]=a[x][y+2]=a[x][y+3]=1;
        }
        if(id==4){
            while(!a[x][y]&&!a[x][y+1]&&!a[x-1][y+1]&&!a[x][y+2]&&x<=10)
                x++;
            x--;
            a[x][y]=a[x][y+1]=a[x-1][y+1]=a[x][y+2]=1;
        }
    }
    for(int i=1;i<=10;i++){
        for(int j=1;j<=10;j++){
            write(a[i][j]),putchar(32);
        }
        putchar(10);
    }
    return 0;
}

B 真的是簽到題

難題鎮樓。

print("NUC2020!!!")
print("NUC2020!!!")
print("NUC2020!!!")

C 港口

經典的拆分題。
其中b[]爲差分數組,即讓下標爲2-n的數組元素變成0。爲什麼沒1呢?因爲b[1]=a[1] 相當於一個計數的初始位置。
b[1] 只是控制原數組的大小,所以 b[1] 的這個數的值任取.
對於每次操作對區間 i ~ j 所有物品加一,b[i]++,b[j+1]--.
對於每次操作對區間 i ~ j 所有物品減一,b[i]--,b[j+1]++.
最後統計差分數組中2-n正數之和 和 負數之和,取最大值。(貪心思路)

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef long long ll;
using namespace std;
const ll maxn=1e6+10;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
ll a[maxn],b[maxn],ans,cnt,n;
int main()
{
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        b[i]=a[i]-a[i-1];
    }
    for(int i=2;i<=n;i++){
        if(b[i]>0) cnt+=b[i];
        else ans-=b[i];
    }
    write(max(cnt,ans));
}

E 簡單的線性代數

線代知識不能忘,取值一定取long long.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k,x,cnt,a[1010][1010];
int main()
{
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
        {
            scanf("%lld",&a[i][j]);
            a[i][j]=(-1)*a[i][j];
            if(i==j)
                a[i][j]++;
            cout<<a[i][j]<<" ";
        }
            cout<<endl;
    }
    return 0; 
}

F 集合操作

借鑑大佬代碼改進。
大佬思路:反向操作x神仙。

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef long long ll;
using namespace std;
set<ll>s;
set<ll>::iterator it;
const ll maxn=1e6+10;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
ll n,op[maxn],x[maxn];
int main()
{
    n=read();
    for(int i=1;i<=n;i++){
        op[i]=read();x[i]=read();
        s.insert(x[i]);
        s.insert(x[i]+1);
    }
    for(int i=1;i<=n;i++){
        if(op[i]==1)
            s.erase(x[i]);
        if(op[i]==2)
            s.insert(x[i]);
        if(op[i]==3)
            it=s.lower_bound(x[i]),write(*it),putchar(10);//換行
    }
    return 0;
}

菜雞思路:雙集合。

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef long long ll;
using namespace std;
set<ll>s,a;//s爲模版集合,a爲不含x的集合(即s的逆操作)
set<ll>::iterator it;
const ll maxn=1e6+10;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
ll n,op,x;
int main()
{
    n=read();
    while(n--){
        op=read();x=read();
        if(op==1){
            s.insert(x);
            if(s.count(x+1)==0)//必不可少!!如果x+1在s裏有,就不加a裏
            a.insert(x+1);
            if(a.count(x)!=0)//像這種的判斷條件,雖然沒啥用,但很節省時間
            a.erase(x);
        }
        if(op==2){
            if(s.count(x)!=0)
            s.erase(x);
            a.insert(x);
        }
        if(op==3){
            ll ans=s.count(x)==0?x:*a.lower_bound(x);
            write(ans);
            putchar(10);
        }
    }
    return 0;
}

G 數位操作

注意兩個點:
1.從9到2找因子。
2.tmp<=9時結果前面加1.

while True:
    try:
        s=int(input());a=0;b="";tmp=s
        for i in range(9,1,-1):
            while s%i==0:
                b+=str(i)
                s=s//i
                a=1
        if tmp<=9:
            print("1"+str(tmp))
        elif a==1 and int(s)==1:
            print(b[::-1])
        else:
            print("-1")
    except:
        break

K 籤個到

注意兩個點:
1.m要麼全給最小值,要麼全給最大值,最後比的是帶m與不帶m的差。
2.n=1時直接扣0.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll maxn=-0x3f3f3f3f,minn=0x3f3f3f3f,maxm=-0x3f3f3f3f,minm=0x3f3f3f3f;
ll n,m,x;
int main()
{
    cin>>n>>m;
    for(ll i=1;i<=n;i++){
        cin>>x;
        maxn=max(x+i*m,maxn);
        minn=min(x-i*m,minn);
        maxm=max(x,maxm);
        minm=min(x,minm);
    }
    if(n==1)
        cout<<"0"<<endl;
    else{
        cout<<max(maxn-minm,maxm-minn)<<endl;
    }
    return 0;
}

什麼?你問菜雞學到了什麼?

快讀和快寫。

inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }

快gcd、快pow、快拓展歐幾里得

inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;   while (b) { if (b & 1)  ans *= a;       b >>= 1;      a *= a; }   return ans; }   ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
ll exgcd(ll a,ll b,ll &x,ll &y){ ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d; }

還有呢?

不用Py,那就一定要開long long。

還有啥呀???

能不模擬就不模擬,仔細想出來的思路纔可能不T。

完結。

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