Coconuts HDU - 5925 (bfs + 离散化)

Coconuts HDU - 5925 (bfs + 离散化)

题目链接:https://cn.vjudge.net/contest/221692#problem/D
题目大意:就是有r*c的方阵 上面都是白的 其中有n个黑色的点 ,问你黑色的点把白色的点分成了几个部分,每个部分的点有多少。
input:
2

3 3
2
1 2
2 1

3 3
1
2 2
output:
Case #1:
2
1 6
Case #2:
1
8
题目分析:正常的思路就是搜索,但是图太大 0 < R,C≤ 1e9, 会超时, 可是黑色的点少啊,才200,所以突破点应该就在这里。
最终想出用离散化的方法解决这个问题,;
图很大的时候每个点之间的距离很大,只要把这一部分离散化就好了,离散化后 图最坏也只是400*400,这样的话搜索就不会超时了。
然而这个题补了这么久就是卡在离散化这里了。不会二维的离散化…

#include <bits/stdc++.h>
using namespace std;

const int maxn = 3000;
typedef long long ll;

int x[maxn], y[maxn], cntx, cnty, tx[maxn], ty[maxn];
ll p[maxn][maxn];
vector<int> data1, data2;
vector<ll> ans;
bool vis[maxn][maxn];
int dict[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

bool judge(int x, int y) {
   return x >= 1 && x < cntx && y >= 1 && y < cnty && !vis[x][y];
}

ll bfs(int x, int y) {
    queue<pair<int, int> > q;
    ll num = 0;
    vis[x][y] = true;
    q.push(make_pair(x, y));

    while(!q.empty()) {
        int X = q.front().first;
        int Y = q.front().second;
        q.pop();
        num += p[X][Y];

       for(int i = 0; i < 4; i++) {
        int tx = X + dict[i][0];
        int ty = Y + dict[i][1];
        if(judge(tx, ty)) {
            vis[tx][ty] = true;
            q.push(make_pair(tx, ty));
        }
      }
    }
    return num;
}

int main()
{
    int t, cas = 1;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        data1.clear();
        data2.clear();
        memset(vis, false, sizeof(vis));
        scanf("%d %d", &n, &m);
        int num;
        scanf("%d", &num);
        data1.push_back(0);
        data1.push_back(n);
        data2.push_back(0);
        data2.push_back(m);
        for(int i = 0; i < num; i++)
        {
            scanf("%d %d", &tx[i], &ty[i]);
            for(int d = -1; d <= 1; d++) {
                if(tx[i] + d >= 0 && tx[i] + d <= n) data1.push_back(tx[i] + d);
                if(ty[i] + d >= 0 && ty[i] + d <= m) data2.push_back(ty[i] + d);
            }
        }
        sort(data1.begin(), data1.end());
        sort(data2.begin(), data2.end());
        cntx = unique(data1.begin(), data1.end()) - data1.begin();
        cnty = unique(data2.begin(), data2.end()) - data2.begin();

        for(int i = 1; i < cntx; i++) {
            for(int j = 1; j < cnty; j++) {
                p[i][j] = (ll)(data1[i] - data1[i - 1]) * (data2[j] - data2[j - 1]);
     //               printf("%d %d %lld\n", i, j, p[i][j]);
            }
        }
        //p[i][j]表示(i, j)这个点表示的实际的点的个数

        for(int i = 0; i < num; i++) {
            int u = lower_bound(data1.begin(), data1.begin() + cntx, tx[i]) - data1.begin();
            int v = lower_bound(data2.begin(), data2.begin() + cnty, ty[i]) - data2.begin();
            vis[u][v] = true;
        }


//        for(int i = 1; i < cntx; i++) {
//            for(int j = 1; j < cnty; j++) {
//                printf("%d%c", vis[i][j], j == cnty - 1 ? '\n' : ' ');
//            }
//        }
//        printf("\n");

        ans.clear();
        for(int i = 1; i < cntx; i++) {
            for(int j = 1; j < cnty; j++) {
                if(!vis[i][j]) {
                    ans.push_back(bfs(i, j));
                }
            }
        }

        printf("Case #%d:\n", cas++);
        printf("%d\n", ans.size());
        sort(ans.begin(), ans.end());
        for(int i = 0; i < ans.size(); i++)
            printf("%lld%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');
    }
}


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

余额充值