首页 > 其他分享 >hduCoconuts(离散化)

hduCoconuts(离散化)

时间:2022-11-18 16:35:29浏览次数:34  
标签:vis int 离散 xlen coconuts hduCoconuts include ylen


Coconuts


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


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


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


 题意:
简单的说一下这个题,给一些点,这些点会将矩阵分成许多部分,求分成了几部分,面积从小到达排序。

出现的问题:1、普通矩阵开不了10^9; 2、普通的搜索一定超时。
解决方法:离散化
先将出现的坐标离散化,记录空白处的长和宽,Dfs搜索。
unique()的作用:将相邻的且相同的元素“删除”(也不是,是将那些元素放后边,例如:1 2 2 2 3 5 5 6 处理后是1 2 3 5 6 2 2 5),然后返回它的长度(就是处理后6的为位置)。

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;

const int inf = 0x3f3f3f3f;
const int Max = 500;
typedef long long int LL;

LL c[Max], r[Max], cc[Max], rr[Max];
map<int, int> rf, cf;
bool vis[Max][Max];
vector<LL> xlen, ylen;
LL sum;

void Dfs(int i, int j)
{
if(vis[i][j] || i >= xlen.size() || i < 0 || j < 0 || j >= ylen.size())
{
return ;
}
sum += xlen[i]*ylen[j];
vis[i][j] = 1;
/*四个方向Dfs*/
Dfs(i+1, j);
Dfs(i-1, j);
Dfs(i, j+1);
Dfs(i, j-1);
}

int main()
{
int K, j, i, n;
LL R, C;
int h = 0;
scanf("%d", &K);
while(K--)
{
memset(vis, 0, sizeof vis);
xlen.clear();
ylen.clear();
cf.clear();
rf.clear();
scanf("%lld %lld", &R, &C);
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%lld %lld", &rr[i], &cc[i]);
r[i] = rr[i];
c[i] = cc[i];
}
/*对边进行处理*/
r[0] = 0;
c[0] = 0;
r[n+1] = R;
c[n+1] = C;
/*处理列*/
sort(r, r+n+2);
int numr = unique(r, r+n+2)-r;
for(i = 1; i < numr; i++)//看懂这个处理就明白这个离散化了
{
int x = r[i] - r[i-1];
if(x > 1)//**这一步的存在是**关键**,将多个空列变成一列,并储存这些空白列的长度
{
xlen.push_back(x - 1);
}
xlen.push_back(1);
rf[r[i]] = xlen.size()-1;
}
/*处理行*/
sort(c, c+n+2);
int numc = unique(c, c+n+2)-c;
for(i = 1; i < numc; i++)
{
int y = c[i] - c[i-1];
if(y > 1)
{
ylen.push_back(y-1);
}
ylen.push_back(1);
cf[c[i]] = ylen.size() - 1;
}
/*vis标记输入的点*/
for(i = 1; i <= n; i++)
{
vis[rf[rr[i]]][cf[cc[i]]] = 1;
}
/*搜索*/
vector<LL>ans;
for(i = 0; i < xlen.size(); i++)
{
for(j = 0; j < ylen.size(); j++)
{
sum = 0;
if(vis[i][j]==0)
{
Dfs(i, j);
}
if(sum)
{
ans.push_back(sum);
}
}
}
sort(ans.begin(), ans.end());
printf("Case #%d:\n", ++h);
printf("%d\n", ans.size());
for(i = 0; i < ans.size(); i++)
{
if(i)
printf(" %lld", ans[i]);
else
printf("%lld", ans[i]);
}
printf("\n");
}
return 0;
}




标签:vis,int,离散,xlen,coconuts,hduCoconuts,include,ylen
From: https://blog.51cto.com/u_15879559/5868810

相关文章

  • 算法基础:离散化及模板详解
    ⭐写在前面的话:本系列文章旨在复习算法刷题中常用的基础算法与数据结构,配以详细的图例解释,总结相应的代码模板,同时结合例题以达到最佳的学习效果。本专栏面向算法零基础但有......
  • 离散数学左孝凌版本-集合论一
    集合论集合与关系集合的概念略集合表示法略集合相等定义基本概念子集空集全集幂集集合的运算序偶笛卡尔积总结关系及其表......
  • 离散化
    V<int>dis;//离散化数组V<int>l(n),r(n);for(inti=0;i<n;i++){cin>>l[i]>>r[i];dis.push_back(l[i]),dis.push_back(r[i]);}//离散化......
  • 离散化
    点的值范围很大,但数量有限,所以不能直接开数组,需要将每个点做映射。Java代码importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;import......
  • 离散化
    #include<iostream>#include<algorithm>#include<cstdio>usingnamespacestd;inta[500005],b[500005],n;intmain(){scanf("%d",&n);for(inti=1;i......
  • 【图像加密】基于离散小波变换结合Schur分解的双重加密零水印算法附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进。......
  • 每日一题-离散化
    IntervalSumvector<int>all;vector<pair<int,int>>add,query;constintN=3e5+5;//Attention!Whenworstcasethatl,r,xalldifferent,weneed3e5......
  • 傅里叶变换(FT)/离散傅里叶变换(DFT)
    本文只讨论非周期情况傅里叶变换---FT:傅里叶变换是将时域信号拆解成频域上各个频率分量的过程,每个连续时刻的信号都对应着一个频域上各个频率分量,则一个完整的频谱为......
  • 推荐系统算法:特征工程(归一化、离散化、各类型特征处理) ----- 机器学习
                             ......
  • 离散数学笔记-- 谓词
    基本概念:  看下面的小例子: 特性谓词,拓展定义域的时候: 看看就行: 具体展开:    具体应用3步骤:   一些逻辑变化:  仔细看下面的例......