Coconuts----DFS+离散化

Coconuts

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 693    Accepted Submission(s): 199


Problem Description
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 ) 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  the second line contains an integer n, the number of bad coconuts,  0n200  from the third line, there comes n lines, each line contains two integers,  xi  and  yi , which means in cell( 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
 

Source
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5925

这个题我真是。。。因为一个long long 数组,我交了5发wa,我真是!@#¥……&*()

题目的题意是说Mr.Frog想要在一张地图上吃椰子,他只能上下左右移动,现在地图上有一些坏椰子,可看成是障碍,问你这张地图有几个连通块,每个连通块有多少个格子。

如果我们不考虑数据范围,这就是一个很简单的dfs,但是坐标范围为10^9,所以我们离散化两个坐标轴,用代码讲吧。

代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#define LL long long
using namespace std;
struct node{
    LL i,j;
};
LL vis[500][500];
vector<LL>nx,ny;
LL sum;
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};
void dfs(int x,int y){//这个dfs很简单,标记一下直接暴力即可
    if(vis[x][y]||x>=(int )nx.size()||x<0||y>=(int )ny.size()||y<0)
        return;
    sum+=(LL)nx[x]*(LL)ny[y];//这里用相乘计算每个小矩形的格子数
    vis[x][y]=1;
    for(int i=0;i<4;i++){
        dfs(x+dx[i],y+dy[i]);
    }
}
int main(){
    int t;
    scanf("%d",&t);
    int cnt=0;
    while(t--){
        cnt++;
        nx.clear();
        ny.clear();
        LL n,m;
        scanf("%lld%lld",&m,&n);
        int top1=0;
        int top2=0;
        int xx[500],yy[500];
        xx[top1++]=0;
        xx[top1++]=n;
        yy[top2++]=0;
        yy[top2++]=m;
        LL k;
        vector<node> point(500);
        scanf("%lld",&k);
        for(int i=0;i<k;i++){
            scanf("%lld%lld",&point[i].j,&point[i].i);//记录障碍物坐标
        }
        printf("Case #%d:\n",cnt);
        for(LL i=0;i<k;i++){
            xx[top1++]=point[i].i;//把纵坐标和横坐标分开,准备离散化用
            yy[top2++]=point[i].j;
        }
        sort(xx,xx+top1);
        sort(yy,yy+top2);
        LL xxx=unique(xx,xx+top1)-xx;//离散化坐标
        LL yyy=unique(yy,yy+top2)-yy;
        map<LL,LL> x1,y1;
        for(LL i=1;i<xxx;i++){
            if(xx[i]-xx[i-1]>1)
                nx.push_back(xx[i]-xx[i-1]-1);
            nx.push_back(1);
            x1[xx[i]]=(int )nx.size()-1 ;
        }
        for (LL j=1;j<yyy;j++){
            if(yy[j]-yy[j-1]>1)
                ny.push_back(yy[j]-yy[j-1]-1);
            ny.push_back(1);
            y1[yy[j]]=(int )ny.size()-1;
        }
        memset(vis,0,sizeof(vis));
        for(int i=0;i<k;i++){
            vis[x1[point[i].i]][y1[point[i].j]]=1;
        }
        long long xin[500];//这个数组是long long ,当时一激动敲成了int,白白5发罚时,100分钟啊!!
        int top=0;
        for(int i=0;i<(int )nx.size();i++){
            for(int j=0;j<(int )ny.size();j++){
                sum=0;
                if (!vis[i][j])
                    dfs(i,j);
                if(sum)
                    xin[top++]=sum;
            }
        }
        sort(xin,xin+top);
        cout<<top<<endl;
        for (int i=0;i<top;i++){
            printf(i==0?"%lld":" %lld",xin[i]);
        }
        cout<<endl;
    }
}




解决这个问题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、付费专栏及课程。

余额充值