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。

完结。

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