Coconuts

41 篇文章 0 订阅

这里写图片描述


给出n*m的图,图中有不超过200个障碍点,要求把所有连通块的大小从小到大输出。数据保证第一行和最后一行不同时存在障碍,第一列和最后一列不同时存在障碍。



我的做法就是大暴力+map记忆化。对于每一个障碍,找他周围是否存在好的点,如果存在就对这个点做bfs,因为200个障碍点配合边界最大包围的区域为1+2+…+199,所以如果超过这么多个点那么一定不是一个被包围的区间,还有一个优化就是如果行或列与最初的点超过了200,那么这片区间一定不会被障碍点包围。最后一个优化是:每完成一次bfs,都把所有搜到过的点压进去map中,如果下次搜索中还搜到这些点,那么直接退出就行了,因为能搜到的话一定是重复或者不被障碍物包围的。还有一点是,我是先记录一共有多少个好的点,然后每找到一个不重复的连通块,就从最大的那个分出来。


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

const int maxn = 202;
struct Node {
    int x, y;
    bool operator<(const Node &other) const {
        if (x < other.x) return true;
        if (x > other.x) return false;
        if (y < other.y) return true;
        return false;
    }
}a[maxn];
set<Node> black, certen, seach, found;
vector<Node> q;
int step, max_step, front;
long long ans[maxn], n, m, tot;
bool flag;
Node temp, initial, head;

const int moved[8][2] = {-1,0, 0,-1,  0,1,  1,0, -1,-1, -1,1, 1,-1, 1,1};

bool connect[maxn][maxn];

int main() {
    int T;
    scanf("%d", &T);
    for (int cases = 1; cases <= T; cases++) {
        printf("Case #%d:\n", cases);
        scanf("%lld%lld", &n, &m);
        scanf("%lld", &tot);
        black.clear();
        for (int i = 1; i <= tot; i++) {
            scanf("%d%d", &a[i].x, &a[i].y);
            black.insert(a[i]);
        }
        ans[0] = 1;
        ans[1] = n*m-tot;
        max_step = max(1ll, tot*(tot-1)/2);
        certen.clear();
        found.clear();
        for (int i = 1; i <= tot; i++) {
            for (int k = 0; k < 4; k++) {
                initial.x = a[i].x+moved[k][0];
                initial.y = a[i].y+moved[k][1];
                if (certen.count(initial) || black.count(initial)) continue;
                if (initial.x < 1 || initial.x > n || initial.y < 1 || initial.y > m) continue;

                q.clear();
                seach.clear();
                q.push_back(initial);
                seach.insert(initial);
                flag = true; step = 1; front = 0;
                while (front < step) {
                    head = q[front];
                    for (int kk = 0; kk < 4; kk++) {
                        temp.x = head.x+moved[kk][0];
                        temp.y = head.y+moved[kk][1];
                        if (black.count(temp) || temp.x < 1 || temp.y < 1 || temp.x > n || temp.y > m) continue;
                        if (found.count(temp)) {
                            flag = false; break;
                        }
                        if (seach.count(temp)) continue;
                        step++;
                        q.push_back(temp);
                        seach.insert(temp);
                        if (step > max_step) {
                            flag = false;
                            break;
                        }
                        if (abs(temp.x-initial.x) > 201 || abs(temp.y-initial.y) > 201) {
                            flag = false;
                            break;
                        }
                    }
                    front++;
                    if (!flag) break;
                }
                for (int j = 0; j < step; j++) found.insert(q[j]);
                if (flag && ans[1] > step) {
                    for (int j = 0; j < step; j++) certen.insert(q[j]);
                    ans[1] -= step;
                    ans[++ans[0]] = step;
                }
            }
        }
        sort(ans+1, ans+1+ans[0]);
        printf("%lld\n", ans[0]);
        printf("%lld", ans[1]);
        for (int i = 2; i <= ans[0]; i++) printf(" %lld", ans[i]);
        printf("\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、付费专栏及课程。

余额充值