D - Coconuts HDU - 5925(二维离散化压缩)

题意:就是给你一个图有多少连通块,按增序输出,但是图比较大需要离散化一下。

思路:把不存在障碍物的行与列压缩为一行或者一列,然后存在的就不压缩原样记录,压缩后的就记录原来的行列大小,这样行列最大就300了就可以了正常dfs了

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
bool st[M][M];
int n, m, k;
PII p[N];
vector<int> lenx, leny;
map<int, int> cntx, cnty;
int x[N], y[N];
int cnt1, cnt2;
vector<int> ans;
int sum;
void dfs(int x, int y)
{
    st[x][y] = 1;
    sum += lenx[x] * leny[y]; // 这样好点都变成矩形了
    for (int i = 0; i < 4; i++)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx < 0 || xx >= lenx.size() || yy < 0 || yy >= leny.size() || st[xx][yy])
            continue;
        dfs(xx, yy);
    }
}
void solve()
{
    cin >> n >> m;
    ans.clear();
    lenx.clear();
    leny.clear();
    cntx.clear();
    cnty.clear();
    cnt1 = 0, cnt2 = 0;
    memset(st, 0, sizeof st);
    x[cnt1++] = y[cnt2++] = 0; // 设置两个边界
    x[cnt1++] = n;
    y[cnt2++] = m;
    cin >> k;
    for (int i = 1; i <= k; i++)
    {
        cin >> p[i].xx >> p[i].yy;
        x[cnt1++] = p[i].xx;
        y[cnt2++] = p[i].yy;
    }
    sort(x, x + cnt1); // 离散化坐标
    sort(y, y + cnt2);
    cnt1 = unique(x, x + cnt1) - x;
    cnt2 = unique(y, y + cnt2) - y;

    for (int i = 1; i < cnt1; i++)
    {
        int len = x[i] - x[i - 1];
        if (len > 1)
            lenx.pb(len - 1);         // 可以压缩的行
        lenx.pb(1);                   // 坏点不可压缩,记录这一行原本的宽度
        cntx[x[i]] = lenx.size() - 1; // 记录这个点离散化的坐标,下标从0开始
    }
    for (int i = 1; i < cnt2; i++)
    {
        int len = y[i] - y[i - 1];
        if (len > 1)
            leny.pb(len - 1);         // 可以压缩的列
        leny.pb(1);                   // 坏点不能压缩,记录这一列原本的宽度
        cnty[y[i]] = leny.size() - 1; // 记录这个点离散化的坐标,下标从0开始
    }
    for (int i = 1; i <= k; i++)
    {
        st[cntx[p[i].xx]][cnty[p[i].yy]] = 1; // 坏点标记为1不再遍历
    }
    for (int i = 0; i < lenx.size(); i++)
    {
        for (int j = 0; j < leny.size(); j++)
        {
            if (!st[i][j]) // 遍历一次一个块
            {
                sum = 0;
                dfs(i, j);
                ans.pb(sum);
            }
        }
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    for (auto ed : ans)
        cout << ed << ' ';
    cout << endl;
}

参考题解链接

  • 2
    点赞
  • 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、付费专栏及课程。

余额充值