HUD 5925-Coconuts(搜索+二维离散化)

Coconuts

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

 

 

 

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(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,C≤109 the second line contains an integer n, the number of bad coconuts, 0≤n≤200 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

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 

题目大意:给你一个R*C的图,图中有n个坏水果,其余全是好水果,相邻(上下左右)的好水果可以一起吃掉,吃掉一堆好水果花费一秒,问你吃完所有的好水果花费的总时间,并从小到大输出每一秒所吃的水果数。

解题思路:首先能想到搜索来解决,但是图太大,直接暴搜肯定不行,那么再读题会发现坏水果只有200个,因为我们只需要求出被坏水果包围的好水果,所以我们对图进行离散化压缩,这样最后的图形就变成了一个非常小的图,再进行暴搜就可以了,因为题目保证坏水果不会从上一直连续到下,也不会从左一直到右,所以在外面的水果一定能够连续到四条边界,通过这个就可以判断那些水果是外面的了,在外面的好水果数就是R*C - K - 被包围的。最后注意开 long long

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<deque>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define FIN freopen("C://Users//Ёбнд©║//Desktop//in.txt","r",stdin);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const int mod = 1e9+7;
const LL INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;

LL n, m, k;
int mp[510][510];

struct node
{
    int x; //原坐标
    int y;
    int xx; //离散坐标
    int yy;
}e[510];

bool cmp1(node a, node b)
{
    return a.x < b.x;
}

bool cmp2(node a, node b)
{
    return a.y < b.y;
}

int l, r, cnt, ans;
int flag1, flag2, flag3, flag4;
int dx[8] = {0, 1, 0, -1};
int dy[8] = {1, 0, -1, 0};
int vis[510][510], vis1[510][510];
vector<LL> v;

bool check(int x, int y)
{
    if(x <= 0 || y <= 0 || x > l || y > r || mp[x][y]) return false;
    if(vis[x][y]) return false;
    return true;
}

void dfs(int x, int y) //搜索每一块
{
    ans ++;
    vis[x][y] = cnt;
    for(int i = 0; i < 4; i ++) {
        int ix = x + dx[i];
        int iy = y + dy[i];
        if(check(ix, iy)) {
            dfs(ix, iy);
        }
    }
}

bool check2(int x, int y)
{
    if(x <= 0 || y <= 0 || x > l || y > r || mp[x][y]) return false;
    if(vis1[x][y]) return false;
    return true;
}

void good(int x, int y) //判断是否被包围
{
    if(x == 1) flag1 = 1;
    if(x == l) flag2 = 1;
    if(y == 1) flag3 = 1;
    if(y == r) flag4 = 1;
    vis1[x][y] = 1;
    for(int i = 0; i < 4; i ++) {
        int ix = x + dx[i];
        int iy = y + dy[i];
        if((vis[x][y] == vis[ix][iy]) && check2(ix, iy)) {
            good(ix, iy);
        }
    }
}

void init()
{
    e[0].x = 0, e[0].y = 0; //初始化边界
    e[0].xx = 0,e[0].yy = 0;
    mem0(mp);
    mem0(vis);
    mem0(vis1);
    v.clear();
    cnt = 1; //水果种类
}

int main()
{
    //FIN;
    int T, t = 1, x, y;
    scanf("%d", &T);
    while(T --) {
        scanf("%lld%lld", &n, &m);
        scanf("%lld", &k);
        init();
        for(int i = 1; i <= k; i ++) {
            scanf("%d%d", &e[i].x, &e[i].y);
        }
        sort(e+1, e+k+1, cmp1);
        for(int i = 1; i <= k; i ++) { //对列离散化
            if(e[i].x - e[i-1].x > 1) {
                e[i].xx = e[i-1].xx + 2;
            }else if(e[i].x - e[i-1].x == 1) e[i].xx = e[i-1].xx + 1;
            else e[i].xx = e[i-1].xx;
        }
        l = e[k].xx;
        if(e[k].x < n) l ++;
        sort(e+1, e+k+1, cmp2);
        for(int i = 1; i <= k; i ++) { //对行离散化
            if(e[i].y - e[i-1].y > 1) {
                e[i].yy = e[i-1].yy + 2;
            }else if(e[i].y - e[i-1].y == 1) e[i].yy = e[i-1].yy + 1;
            else e[i].yy = e[i-1].yy;
        }
        r = e[k].yy;
        if(e[k].y < m) r ++;
        for(int i = 1; i <= k; i ++) {
            mp[e[i].xx][e[i].yy] = 1;
        }
        int sum = 0;
        for(int i = 1; i <= l; i ++) {
            for(int j = 1; j <= r; j ++) {
                if(check(i, j)) {
                    flag1 = flag2 = flag3 = flag4 = 0;
                    ans = 0;
                    dfs(i, j);
                    good(i, j);
                    if(!(flag1 && flag2 && flag3 && flag4)) { //是被包围的
                        v.push_back(ans);
                        sum += ans;
                    }
                    cnt ++;
                }
            }
        }
        v.push_back(n*m-k-(LL)sum);
        sort(v.begin(), v.end());
        printf("Case #%d:\n", t ++);
        printf("%d\n", v.size());
        for(int i = 0; i < v.size()-1; i ++) {
            printf("%lld ", v[i]);
        }
        printf("%lld\n", v[v.size()-1]);
    }
    return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值