Coconuts HDU - 5925 (离散化+dfs求联通块)

http://acm.hdu.edu.cn/showproblem.php?pid=5925

TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1). 

Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component). 
Input
The first line contains apositiveinteger T( T10 T≤10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C,  0<R,C109 0<R,C≤109 the second line contains an integer n, the number of bad coconuts,  0n200 0≤n≤200 from the third line, there comes n lines, each line contains two integers,  xi xi and  yi yi, which means in cell( xi,yi xi,yi), there is a bad coconut. 

It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time. 
Output
For each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.
Sample Input
2

3 3
2
1 2
2 1

3 3
1
2 2
Sample Output
Case #1:
2
1 6
Case #2:
1
8
题目大意: R行C列的网格(R,C<=109),有N(N<=200)个障碍,求联通块数量和各个大小(从小到大)。

ps:吐槽一下,这个题做了整整一下午了,看了无数的题解可是都没有自己想要的那个思路,最终还是自己根据自己思路把它写出来了,

题解:因为图太大,而障碍点少那么就讲非障碍的地方缩成一个点,一定要对齐,凡是有x,y的就画垂直水平线,然后所得出来的小矩阵成为点,注意:在线上的点特殊考虑,有水平垂直线交点的点也要特殊处理,判断一下是不是障碍点(此处用的离散化后的标记识别),建出新图来以后dfs求联通块就可以了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
long long  x[222],y[222];
long long A[444][444];
int B[444][444];
int cnt;
int R,C;
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
long long sum;
long long d[222222];
map<long long ,long long >mx,my;
struct node
{
    long long x;
    long long y;
} p[222];
int ma[422][422];
void dfs(int x,int y,int k)
{
    if(x<0||y<0||x>=R||y>=C)return ;
    if(B[x][y]>0||A[x][y]==0)return ;
    B[x][y]=k;
    sum+=A[x][y];
    for(int i=0; i<4; i++)
    {
        int nowx=x+dx[i];
        int nowy=y+dy[i];
        dfs(nowx,nowy,k);
    }
    return ;
}
bool cmp(node a,node b)
{
    return a.x<b.x;
}
bool cmp1(node a,node b)
{
    return a.y<b.y;
}
int main()
{
    int t;
    scanf("%d",&t);
    int w=1;
    while(t--)
    {
        ///初始化
        long long r,c;
        int n,m;
        scanf("%lld%lld",&r,&c);
        scanf("%d",&n);
        x[0]=y[0]=0;
        mx.clear();
        my.clear();
        memset(ma,0,sizeof(ma));
        for(int i=1; i<=n; i++)
        {
            scanf("%lld%lld",&p[i].x,&p[i].y);
            x[i]=p[i].x;
            y[i]=p[i].y;
        }
        ///为了标记障碍点进行离散化
        sort(p+1,p+1+n,cmp);
        int pre=1;
        mx[p[1].x]=pre++;
        for(int i=2; i<=n; i++)
        {
            if(p[i].x!=p[i-1].x)
                mx[p[i].x]=pre++;
        }
        sort(p+1,p+1+n,cmp1);
        pre=1;
        my[p[1].y]=pre++;
        ma[mx[p[1].x]][my[p[1].y]]=1;
        for(int i=2; i<=n; i++)
        {
            if(p[i].y!=p[i-1].y)
                my[p[i].y]=pre++;
            ma[mx[p[i].x]][my[p[i].y]]=1;
        }
        ///建新图,缩图
        sort(x,x+n+1);
        sort(y,y+n+1);
        int nn=n;
        n=unique(x,x+nn+1)-x;
        m=unique(y,y+nn+1)-y;
//        for(int i=0; i<n; i++)cout<<x[i]<<" ";
//        cout<<endl<<"*";
//        for(int i=0; i<m; i++)cout<<y[i]<<" ";
//        cout<<endl;
        int sr,sc;
        sr=sc=0;
        for(int i=1; i<n; i++)
        {
            if(x[i]-x[i-1]>1)
            {
                long long xx=x[i]-x[i-1]-1;
                for(int j=1; j<m; j++)
                {
                    if(y[j]-y[j-1]>1)A[sr][sc++]=(y[j]-y[j-1]-1)*xx;
                    A[sr][sc++]=xx;
                }
                if(y[m-1]!=c)A[sr][sc++]=(c-y[m-1])*xx;
                sr++;
            }
            C=sc;
            sc=0;
            for(int j=1; j<m; j++)
            {
                if(y[j]-y[j-1]>1)A[sr][sc++]=y[j]-y[j-1]-1;
                if(ma[mx[x[i]]][my[y[j]]])A[sr][sc++]=0;
                else A[sr][sc++]=1;
            }
            if(y[m-1]!=c)A[sr][sc++]=c-y[m-1];
            sr++;
            C=sc;
            sc=0;
        }
        if(x[n-1]!=r)
        {
            long long xx=r-x[n-1];
            for(int j=1; j<m; j++)
            {
                if(y[j]-y[j-1]>1)A[sr][sc++]=(y[j]-y[j-1]-1)*xx;
                A[sr][sc++]=xx;
            }
            if(y[m-1]!=c)A[sr][sc++]=(c-y[m-1])*xx;
            sr++;
        }
        R=sr;
//        for(int i=0; i<R; i++)
//        {
//            for(int j=0; j<C; j++)
//            {
//                printf("%d ",A[i][j]);
//            }
//            printf("\n");
//        }
///求联通块
        memset(B,0,sizeof(B));
        cnt=0;
        for(int i=0; i<R; i++)
        {
            for(int j=0; j<C; j++)
            {
                if(A[i][j]!=0&&B[i][j]==0)
                {
                    sum=0;///记录联通块的大小
                    dfs(i,j,++cnt);
                    d[cnt]=sum;
                }
            }
        }
        sort(d,d+1+cnt);
        printf("Case #%d:\n",w++);
        if(cnt==0)
            printf("%d\n",cnt);
        else
        {
            printf("%d\n",cnt);
            for(int i=1; i<=cnt; i++)
            {
                if(i==1)printf("%lld",d[i]);
                else printf(" %lld",d[i]);
            }
            printf("\n");
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决这个问题King Julien rules the Madagascar island whose primary crop is coconuts. If the price of coconuts is P , then King Julien’s subjects will demand D(P ) = 1200 − 100P coconuts per week for their own use. The number of coconuts that will be supplied per week by the island’s coconut growers is S(p) = 100P. (a) (2 pts) Calculate the equilibrium price and quantity for coconuts. (b) (2 pts) One day, King Julien decided to tax his subjects in order to collect coconuts for the Royal Larder. The king required that every subject who consumed a coconut would have to pay a coconut to the king as a tax. Thus, if a subject wanted 5 coconuts for himself, he would have to purchase 10 coconuts and give 5 to the king. When the price that is received by the sellers is pS, how much does it cost one of the king’s subjects to get an extra coconut for himself? (c) (3 pts) When the price paid to suppliers is pS, how many coconuts will the king’s subjects demand for their own consumption (as a function of pS)? 2 (d) (2 pts) Under the above coconut tax policy, determine the total number of coconuts demanded per week by King Julien and his subjects as a function of pS. (e) (3 pts) Calculate the equilibrium value of pS, the equilibrium total number of coconuts produced, and the equilibrium total number of coconuts consumed by Julien’s subjects. (f) (5 pts) King Julien’s subjects resented paying the extra coconuts to the king, and whispers of revolution spread through the palace. Worried by the hostile atmosphere, the king changed the coconut tax. Now, the shopkeepers who sold the coconuts would be responsible for paying the tax. For every coconut sold to a consumer, the shopkeeper would have to pay one coconut to the king. For this new policy, calculate the number of coconuts being sold to the consumers, the value per coconuts that the shopkeepers got after paying their tax to the king, and the price payed by the consumers.
最新发布
03-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值