HDU 4354
思路是在看电视时突然想到的。枚举区间,然后按树形DP来选择最大值看是否满足条件。但枚举区间时的方法低效,看了题解,说枚举区间可以设两个指针逐步移动,
开始 l = r = 1, 记录已经出现的国家。
判断是否满足条件。
如果满足,更新答案,更新区间出现的国家,l++, 一直到不满足。
如果不满足,更新区间出现的国家,r++, 一直到满足。
但我写了好久,呃,没正确。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int C,N,K,M,cnt,tot;
struct City{
int x,belong;
};
City cities[5050];
int head[2050];
int dp[2050][2];
bool vis[2050]; struct node{
int u,v;
int next;
}edge[6500]; bool cmp(City a,City b){
if(a.x<b.x) return true;
return false;
}
int belong[2050]; void addedge(int u,int v){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int u,int f){
dp[u][0]=0;dp[u][1]=1;
vis[u]=true;
for(int ei=head[u];ei!=-1;ei=edge[ei].next){
if(f!=edge[ei].v&&belong[edge[ei].v]>0&&!vis[edge[ei].v ]){
dfs(edge[ei].v,u);
dp[u][1]+=dp[edge[ei].v][0];
dp[u][0]+=max(dp[edge[ei].v][0],dp[edge[ei].v][1]);
}
}
} bool judge(){
int ans=0;
memset(vis,false,sizeof(vis));
for(int i=1;i<=N;i++){
if(!vis[i]&&belong[i]>0){
dfs(i,0);
ans+=max(dp[i][0],dp[i][1]);
}
}
if(ans>=K) return true;
else return false;
} int main(){
int T,t=0,u,v,l,r;
scanf("%d",&T);
while(++t<=T){
scanf("%d%d%d%d",&C,&N,&K,&M);
cnt=tot=0;
for(int i=1;i<=C;i++){
scanf("%d%d",&cities[i].x,&cities[i].belong);
}
memset(head,-1,sizeof(head));
sort(cities+1,cities+1+C,cmp);
for(int i=1;i<=M;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
cnt=l=r=1;
int ans=-1;
memset(belong,0,sizeof(belong));
while(r<=C){
if(cnt>=K && judge()){
if(ans==-1||cities[r].x - cities[l].x<ans)
ans=cities[r].x - cities[l].x;
belong[ cities[l].belong ]--;
if( belong[ cities[l].belong ]==0 ) cnt--;
l++;
}
else{
r++;
belong[ cities[r].belong ]++;
if( belong[ cities[r].belong ]==1 ) cnt++;
}
}
printf("Case #%d: %d\n",t,ans);
}
return 0;
}
下面是别人过了的代码。。。T_T不知道错哪里了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int C = 5005;
const int N = 2005;
const int M = 1005; int cnt[N]; //i????????
int vis[C];
int dp[C][2];
int c, n, k, m;
vector<int>v[N];
struct city
{
int x, y;
bool operator<(const city &a) const
{
return x<a.x;
}
}ct[C]; int min(int x, int y)
{
if(x==-1) return y;
return x<y?x:y;
} void dfs(int s)
{
vis[s] = 1;
dp[s][1] = 1;
dp[s][0] = 0;
for(int i=0; i<v[s].size(); i++)
{
int ss = v[s][i];
if(vis[ss]==1 || cnt[ss]==0) continue;
dfs(ss);
dp[s][1] += dp[ss][0];
dp[s][0] += max(dp[ss][1], dp[ss][0]);
}
} bool ok(int l, int r)
{
int res = 0;
memset(vis, 0, sizeof(vis));
for(int i = l; i<=r; i++)
if(vis[ ct[i].y ]==0)
{
dfs( ct[i].y );
res += max( dp[ ct[i].y ][0], dp[ ct[i].y ][1] );
}
if(res>=k) return true;
else return false;
} int solve()
{
int kk, l, r;
kk = l = r = 1;
cnt[ ct[1].y ]++;
int ans = -1;
while(r<=c)
{
if(kk>=k && ok(l, r))
{
ans = min(ans, ct[r].x - ct[l].x);
cnt[ ct[l].y ]--;
if( cnt[ ct[l].y ]==0 ) kk--;
l++;
}
else
{
r++;
cnt[ ct[r].y ]++;
if( cnt[ ct[r].y ]==1 ) kk++;
}
}
return ans;
} int main()
{
int t, tt=0, i, x, y;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d", &c, &n, &k, &m);
for(i=1; i<=n; i++) v[i].clear();
memset(cnt, 0, sizeof(cnt));
memset(vis, 0, sizeof(vis));
for(i=1; i<=c; i++) scanf("%d%d", &ct[i].x, &ct[i].y);
sort(ct+1, ct+1+c);
for(i=1; i<=m; i++)
{
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
printf("Case #%d: %d\n", ++tt, solve());
}
return 0;
}
HDU 4354的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- getLocationInWindow getLocationOnScreen getLeft , getTop, getBottom,getRight
版权声明:本文为博主原创文章,未经博主允许不得转载. 最近做项目时,发现在activity的onCreate()和onResume()方法里调用View.getLocationInWindow() 时 ...
- JQuery常用的api[最好是系统地学习一下《锋利的JQuery》]
text http://api.jquery.com/text/ Get the combined text contents of each element in the set of matche ...
- 【转】git rebase简介(基本篇)
原文网址:http://blog.csdn.net/hudashi/article/details/7664631/ 原文: http://gitbook.liuhui998.com/4_2.html ...
- Juniper交换机维护
Juniper交换机维护操作之一: 1.1 交换机启动和关闭 1.1.1 重新启动 1. 使用具有足够权限的用户名和密码登陆CLI命令行界面. 2. 在提示符下输入下面的命令: use ...
- virtualbox 安装虚拟机(centos7) 并映射本地文件夹至虚拟机(增强工具)
一.安装环境 操作系统:windows10 virtualbox: 5.2.20 (在安装virtualbox 时可能需要 进入BIOS 设置虚拟化系统启动) centos7:http://mirro ...
- (转载) popupWindow 指定位置上的显示
popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报 分 ...
- 揭秘IPHONE X刷脸认证的技术奥秘
苹果最新发布的Iphone X具有一个全新的功能叫做刷脸认证,背后的技术其实是生物密码的更新,通过人脸识别取代了传统的指纹识别,大家肯定对这种新技术非常感兴趣,下面我们通过这篇文章为大家介绍人脸识别的 ...
- FBX骨骼坐标系与模型坐标系的关系
采用assimp加载FBX文件.首先记录下ubuntu下assimp的编译安装. cd assimp_unzip_dir mkdir build cd build && cmake . ...
- Java中数组遍历
就是将数组中的每个元素分别获取出来,就是遍历.遍历也是数组操作中的基石. 数组的索引是 0 到 lenght-1 ,可以作为循环的条件出现 public class ArrayDemo4 { publ ...
- 如何使用Matlab做数字信号处理的仿真1
例如 第三版数字信号处理P51 -1.14习题时域离散信号的相关性研究x(n)=Asin(ωn)+u(n),其中ω=π/16,u(n)是白噪声,现要求 ⑴.产生均值为0,功率P=0.1的均匀分布白噪声 ...