hdu5925 Coconuts

比完看acdream说这题是签到题 怎么都不会写
我现在补完也觉得 这不是傻逼题么
我我这个这么快5题的人真的不应该啊

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(X) ((int)X.size())
const int MAXN = 505;
const int INF = 0x3f3f3f3f;

int R,C,n;
int dir[][4] = { {1,0}, {-1,0}, {0,-1}, {0,1} };
int X[MAXN], Y[MAXN];
int has[MAXN]; int pos[MAXN]; int tot;
int v1[MAXN]; int vx;
int v2[MAXN]; int vy;
vector<ll> Ans;
int vis[MAXN][MAXN];

void bfs(int x,int y) {
//  printf("%d %d
",x,y);
    queue<pair<int,int>> Q;
    Q.push({x,y}); vis[x][y] = 1;
    ll ans = 0;

    while(!Q.empty()) {
        int t1 = Q.front().first; int t2 = Q.front().second; Q.pop();
        ans += 1ll*v1[t1]*v2[t2];
        for(int i = 0; i < 4; ++i) {
            int xx = t1+dir[i][0]; int yy = t2+dir[i][1];
            if(xx >= 1 && xx <= vx && yy >= 1 && yy <= vy && !vis[xx][yy]) {
                vis[xx][yy] = 1;
                Q.push({xx,yy});
            }   
        }
    }   
    Ans.push_back(ans);
}
int main(){
    int _; scanf("%d",&_);
    for(int cas=1;cas<=_;cas++) {
        Ans.clear();
        memset(vis,0,sizeof(vis));
        scanf("%d %d %d",&R,&C,&n);

        for(int i = 0; i < n; ++i) {
            scanf("%d %d",&X[i],&Y[i]);
        }

        tot = 0;
        for(int i = 0; i < n; ++i) {
            has[tot++] = X[i];
        }
        sort(has,has+tot);
        tot = unique(has,has+tot) - has;
        int pre = 0; vx = 0;
        for(int i = 0; i < tot; ++i) {
            if(pre+1 < has[i]) v1[++vx] = has[i]-pre-1;
            v1[++vx] = 1; pos[i] = vx;      
            pre = has[i];
        } 
        if(has[tot-1] < R) v1[++vx] = R-has[n-1];
        for(int i = 0; i < n; ++i) {
            int tt = lower_bound(has,has+tot,X[i]) - has;
            X[i] = pos[tt];
        } 

        tot = 0;
        for(int i = 0; i < n; ++i) {
            has[tot++] = Y[i];
        }
        sort(has,has+tot);
        tot = unique(has,has+tot) - has;
        pre = 0; vy = 0;
        for(int i = 0; i < tot; ++i) {
            if(pre+1 < has[i]) v2[++vy] = has[i]-pre-1;
            v2[++vy] = 1; pos[i] = vy;      
            pre = has[i];
        } 
        if(has[tot-1] < C) v2[++vy] = C-has[n-1];
        for(int i = 0; i < n; ++i) {
            int tt = lower_bound(has,has+tot,Y[i]) - has;
            Y[i] = pos[tt];
        }

        for(int i = 0; i < n; ++i) {
            vis[X[i]][Y[i]] = 1;
        }
    //  printf("hh
");
        for(int i = 1; i <= vx; ++i)
            for(int j = 1; j <= vy; ++j) {
                if(!vis[i][j]) {
                    bfs(i,j);
                }
            }

    //  for(int i = 1; i <= vx; ++i) printf("%d ",v1[i]); printf("
");
    //  for(int i = 1; i <= vy; ++i) printf("%d ",v2[i]); printf("
");
    //  for(int i = 0; i < n; ++i) printf("%d %d
",X[i],Y[i]);

        sort(Ans.begin(), Ans.end());
        printf("Case #%d:
%d
",cas,sz(Ans));
        for(int i = 0; i < sz(Ans); ++i) {
            if(i) printf(" ");
            printf("%lld",Ans[i]);
        } printf("
");
    }
    return 0;   
}

原文地址:https://www.cnblogs.com/Basasuya/p/8433737.html