其他分享
首页 > 其他分享> > 【解题报告】ZJNU 个人赛一 (2021)

【解题报告】ZJNU 个人赛一 (2021)

作者:互联网

【解题报告】ZJNU 个人赛一 (2021暑假)

A

题意

思路

代码

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long ll;
void show(){std::cerr << endl;}template<typename T,typename... Args>void show(T x,Args... args){std::cerr << "[ " << x <<  " ] , ";show(args...);}

const int MAX = 60;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-5;

int b2[MAX],b3[MAX];

int main()
{
    b2[0] = 1;b3[0] = 1;

    string ta,tb;
    int la,lb;
    cin >> ta >> tb;
    la = ta.size();lb=tb.size();

    int n1 = 0,n2 = 0;
    int st = 0;
    for(int i = la-1;i>=0;--i){
        n1 = n1 + b2[st] * (ta[i] - '0');
        st++;
        b2[st] = b2[st-1] * 2;
    }
    st = 0;
    for(int i = lb-1;i>=0;--i){
        n2 = n2 + b3[st] * (tb[i] - '0');
        st++;
        b3[st] = b3[st-1] * 3;
    }
//    show(n1,n2);
    int nn1=0,nn2=0;

    for(int i = la-1;i>=0;--i){
        nn1=n1;

        if(ta[i] == '1'){
            nn1 -= b2[la-1-i];
        }else{
            nn1 += b2[la-1-i];
        }

        for(int j = lb-1;j>=0;--j){
            for(int k = 0;k <= 2;++k){
                if(k != (tb[j]-'0')){
                    nn2 = n2-(tb[j]-'0')*b3[lb-1-j] + k*b3[lb-1-j];

                    if(nn1 == nn2){
                        cout << nn1;
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}

B

题意

思路

代码

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long ll;
void show(){std::cerr << endl;}template<typename T,typename... Args>void show(T x,Args... args){std::cerr << "[ " << x <<  " ] , ";show(args...);}

const int MAX = 1e5+50;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-5;

#define ls (p<<1)
#define rs (p<<1|1)
#define md ((l+r)>>1)

const int YOU = 2e5+2;
int aa[MAX];

int tree[YOU*4];
void push_up(int p){
    tree[p] = tree[ls] + tree[rs];
}
void build(int p,int l,int r){
    if(l == r){
        tree[p] = 0;
        return;
    }
    build(p,l,md);
    build(p,md+1,r);
    push_up(p);
}
void update(int p,int l,int r,int u,ll k){
    if(l == r){
        tree[p] += k;
        return;
    }
    if(md >= u)update(ls,l,md,u,k);
    else update(rs,md+1,r,u,k);
    push_up(p);
}
ll query(int p,int l,int r,int qx,int qy){
    if(qx <= l && qy >= r){
        return tree[p];
    }
    ll res = 0;
    if(qx <= md)res += query(ls,l,md,qx,qy);
    if(qy >  md)res += query(rs,md+1,r,qx,qy);
    return res;
}


int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    for(int i = 1;i <= n;++i){
        int t;scanf("%d",&t);
        if(t >= x)aa[i] = 1;
        else aa[i] = -1;
    }
    build(1,1,YOU);
    int ori = 1e5+1;
    ll ans = 0;
    for(int i = 1;i <= n;++i){
        if(aa[i] == 1){
            ori--;
            update(1,1,YOU,ori+1,1);
            ans += query(1,1,YOU,ori,YOU);
        }else{
            ori++;
            update(1,1,YOU,ori-1,1);
            ans += query(1,1,YOU,ori,YOU);
        }
        //show(ans);
    }
    printf("%lld",ans);
    return 0;
}
/**
4 6
0 0 0 0
*/

C

D

题意

思路

代码

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long ll;
void show(){std::cerr << endl;}template<typename T,typename... Args>void show(T x,Args... args){std::cerr << "[ " << x <<  " ] , ";show(args...);}

const int MAX = 5e4+50;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-5;

struct node{
    int p;
    int id;
    bool operator <(const node &ND)const{
        return p < ND.p;
    }
}aa[MAX];
map<int,int>M;
int main()
{
    int n,cnt = 0;
    scanf("%d",&n);
    for(int i = 1;i <= n;++i){
        scanf("%d%d",&aa[i].p,&aa[i].id);
        M[aa[i].id]++;
        if(M[aa[i].id] == 1)cnt++;
    }
    sort(aa+1,aa+1+n);
//    for(int i = 1;i <= n;++i){
//        printf("%d %d\n",aa[i].p,aa[i].id);
//    }
    M.clear();
    int L = 1,R = 0;
    int ans = INF;
    int now = 0;
    while(1){
        while(R < n && now < cnt){
            R++;
            M[aa[R].id]++;
            if(M[aa[R].id] == 1)now++;
        }
        if(R == n){
            if(now == cnt)ans = min(ans,aa[R].p - aa[L].p);
            else break;
        }
        while(L <= R && now == cnt){
            //show(L,R);
            ans = min(ans,aa[R].p - aa[L].p);
            M[aa[L].id]--;
            if(M[aa[L].id] == 0)now--;
            L++;
        }
    }

    printf("%d",ans);
    return 0;
}
/**
5
1 1
2 1
3 1
4 1
5 2
*/

E

题意

思路

F

int main()
{
    int d,h,m;
    scanf("%d%d%d",&d,&h,&m);
    if(d == 11 && h == 11 && m < 11){
        puts("-1");
    }else if(d == 11 && h < 11){
        puts("-1");
    }else{
        int ans = 0;
        int dd,hh,mm;
        for(dd = 11;dd <= d;++dd){
            if(dd == 11)hh = 11;
            else hh = 0;

            for(;hh <= 23;++hh){

                if(dd == 11 && hh == 11)mm = 11;
                else mm = 0;

                for(;mm <= 59;++mm){
                    ans++;
                    if(dd == d && hh == h && mm == m){
                        printf("%d",ans-1);
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}

H

思路

3 5
X...X
.....
..X..
我们原来跑出来的:6
最优的策略:4
XXXXX
..X..
..X..

代码

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long ll;
void show(){std::cerr << endl;}template<typename T,typename... Args>void show(T x,Args... args){std::cerr << "[ " << x <<  " ] , ";show(args...);}

const int MAX = 52;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-5;

int n,m;
char aa[MAX][MAX];
char bb[MAX][MAX];
int dis[MAX][MAX][4];
int dis2[MAX][MAX];
int col[MAX][MAX];
set<pair<int,int> >V[4];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
void init(){
    for(int i = 1;i <= n;++i)
    for(int j = 1;j <= m;++j)
        aa[i][j] = bb[i][j];
}
void gei(int c){
    for(int i = 1;i <= n;++i)
    for(int j = 1;j <= m;++j)
        dis2[i][j] = dis[i][j][c];
}
void dfs(int x,int y,int c){
    dis[x][y][c] = 0;
    col[x][y] = c;
    V[c].insert(make_pair(x,y));
    for(int i = 0;i < 4;++i){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx < 1 || ty < 1 || tx > n || ty > m)continue;
        if(bb[tx][ty] == '.')continue;
        if(dis[tx][ty][c] == INF)dfs(tx,ty,c);
    }
}
void pre(int c){
    bool fd = false;
    for(int i = 1;i <= n;++i){
        for(int j = 1;j <= m;++j){
            dis[i][j][c] = INF;
        }
    }
    for(int i = 1;!fd && i <= n;++i){
        for(int j = 1;!fd && j <= m;++j){
            if(bb[i][j] == 'X' && col[i][j] == 0){
                fd = true;
                dfs(i,j,c);
            }
        }
    }
}
queue<pair<int,int> >Q;
int bfs(int c){
    int res = INF;
    for(auto it : V[c]){
        Q.push(it);
    }

    while(!Q.empty()){
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        if(dis2[x][y] != 0 && aa[x][y] == 'X'){
            res = min(res,dis2[x][y]-1);
            continue;
        }

        for(int i = 0;i < 4;++i){
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(tx < 1 || ty < 1 || tx > n || ty > m)continue;
            if(dis2[tx][ty] > dis2[x][y] + 1){
                dis2[tx][ty] = dis2[x][y] + 1;
                Q.push(make_pair(tx,ty));
            }
        }
    }
    return res;
}
int bfs(int c1,int c2){
    int res = INF;
    for(auto it : V[c1]){
        Q.push(it);
    }

    while(!Q.empty()){
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        if(col[x][y] == c2){
            res = min(res,dis2[x][y]-1);
            continue;
        }

        for(int i = 0;i < 4;++i){
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(tx < 1 || ty < 1 || tx > n || ty > m)continue;
            if(dis2[tx][ty] > dis2[x][y] + 1){
                dis2[tx][ty] = dis2[x][y] + 1;
                Q.push(make_pair(tx,ty));
            }
        }
    }
    map<int,map<int,int> >VIS;		// 注意这个加上,否则就 MLE了,因为是倒着把图变成 X 的
    for(auto it : V[c2]){
        if(dis2[it.first][it.second] == res + 1)Q.push(it),VIS[it.first][it.second] = true;
    }
    while(!Q.empty()){
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        aa[x][y] = 'X';
        for(int i = 0;i < 4;++i){
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(tx < 1 || ty < 1 || tx > n || ty > m)continue;
            if(dis2[tx][ty] == dis2[x][y] - 1 && !VIS[tx][ty]){
                VIS[tx][ty] = 1;
                Q.push(make_pair(tx,ty));
            }
        }
    }
    return res;
}
int ans = INF;
void dw(){
    puts("---");
    for(int i = 1;i <= n;++i){
        for(int j = 1;j <= m;++j){
            cout << aa[i][j];
        }
        puts("");
    }
    puts("---");
}
void work(int c){
    int ans1,ans2;
    init();
    if(c == 1){
        gei(2);
//        dw();
        ans1 = bfs(2,3);
//        dw();
        gei(1);
        ans2 = bfs(1);
    }else if(c == 2){
        gei(1);
        ans1 = bfs(1,3);
        gei(2);
        ans2 = bfs(2);
    }else{
        gei(1);
        ans1 = bfs(1,2);
        gei(3);
        ans2 = bfs(3);
    }
    ans = min(ans,ans1+ans2);
//    show(ans1,ans2);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;++i){
        scanf("%s",bb[i]+1);
    }

    for(int i = 1;i <= 3;++i){
        pre(i);
    }
    for(int i = 1;i <= 3;++i){
        work(i);
    }
    printf("%d",ans);
    return 0;
}
/**
3 5
X...X
.....
..X..

5 1
X
.
X
.
X
*/

I

题意

思路

代码

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long ll;
void show(){std::cerr << endl;}template<typename T,typename... Args>void show(T x,Args... args){std::cerr << "[ " << x <<  " ] , ";show(args...);}

const int MAX = 300;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-5;

struct node{
    int sx,sy;
    int ex,ey;
    int st;
}aa[MAX];

bool M[MAX][MAX];
bool vis[MAX];
int match[MAX];
int sum = 0;
int n;
bool dfs(int u){
    for(int v=1;v<=n;++v){
        if(M[u][v] && !vis[v]){
            vis[v] = true;
            if(match[v] == -1 || dfs(match[v])){
                match[v] = u;
                return true;
            }
        }
    }
    return false;
}

void check(int i,int j){
    if(aa[i].st == aa[j].st)return;
    if(aa[i].st == 1)swap(i,j);
    if(aa[i].sx >= aa[j].sx && aa[i].sx <= aa[j].ex){
        if(aa[i].sy <= aa[j].sy && aa[i].ey >= aa[j].sy){
            M[i][j] = true;
            //show(i,j);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;++i){
        scanf("%d%d%d%d",&aa[i].sx,&aa[i].sy,&aa[i].ex,&aa[i].ey);

        if(aa[i].sx == aa[i].ex){
            aa[i].st = 0;
            if(aa[i].sy > aa[i].ey)swap(aa[i].sy,aa[i].ey);
        }else {
            aa[i].st = 1;
            if(aa[i].sx > aa[i].ex)swap(aa[i].sx,aa[i].ex);
        }
    }

    for(int i = 1;i <= n;++i){
        for(int j = i+1;j <= n;++j){
            check(i,j);
            //if(M[i][j])show(i,j);
        }
    }

    memset(match,-1,sizeof(match));
    for(int i = 1;i <= n;++i){
        memset(vis,0,sizeof(vis));
        if(dfs(i))sum++;
    }

    //show(sum);

//    for(int i = 1;i <= n;++i){
//        bool dl = true;
//        for(int j = 1;j <= n;++j){
//            if(M[i][j] || M[j][i]){
//                dl = false;
//                break;
//            }
//        }
//        if(dl){
//            sum++;
//        }
//    }

    printf("%d",n-sum);
    return 0;
}
/**
1
0 0 1 1

4
0 0 5 0
2 -1 2 1

10 0 15 0
13 -1 13 1

2
0 0 5 0
2 -1 2 1
*/

K

题意

思路

代码

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long ll;
void show(){std::cerr << endl;}template<typename T,typename... Args>void show(T x,Args... args){std::cerr << "[ " << x <<  " ] , ";show(args...);}

const int MAX = 30;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-5;

int aa[MAX];
int n,m;
ll dp[11][10050];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;++i)scanf("%d",&aa[i]);

    for(int i = 0;i <= n;++i)
    for(int j = 0;j <= m;++j)
        dp[i][j] = LINF;
    dp[0][0] = 0;
    for(int i = 1;i <= n;++i){
        for(int j = 1;j <= m;++j){
            for(ll k = 1;k*k <= m && j-k*k>=0;++k){
                dp[i][j] = min(dp[i][j],dp[i-1][j-k*k] + (aa[i] - k) * (aa[i] - k));
            }
        }
    }
    if(dp[n][m] >= LINF){
        puts("-1");
        return 0;
    }
    printf("%lld",dp[n][m]);
    return 0;
}
/**
1 2
1
*/

标签:aa,shu,tx,ty,int,void,个人赛,2021,ZJNU
来源: https://blog.csdn.net/weixin_45775438/article/details/118680940