POJ 1944 并查集(模拟)
思路:
肯定是要枚举断点的。。就看枚举完断点以后怎么处理了……
1.用类似并查集的思想… f[x]=max(f[x],y)表示x和y相连(一定要注意取max,,,血的教训) 复杂度O(np)
2.猥琐思路 每回枚举完断点以后sort一遍 用左右指针扫一遍就OK..
需要高超的卡时技巧就能过 复杂度:O(nplogp)
// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,p,ans=0x3fffffff,f[2005];
struct Node{int x,y;}node[10005];
int main(){
scanf("%d%d",&n,&p);
for(int i=1;i<=p;i++){
scanf("%d%d",&node[i].x,&node[i].y);
if(node[i].x>node[i].y)swap(node[i].x,node[i].y);
}
for(int i=1;i<n;i++){
memset(f,0,sizeof(f));
int cnt=0,Left=0,Right=0;
for(int j=1;j<=p;j++)
f[node[j].x]=max(node[j].y,f[node[j].x]);
for(int j=i;j<=i+n;j++)
if(j>Right&&f[j])
cnt+=Right-Left,Left=j,Right=f[j];
else
Right=max(Right,f[j]);
ans=min(ans,cnt+Right-Left);
for(int j=1;j<=p;j++)
if(node[j].x==i)
swap(node[j].x,node[j].y),node[j].y+=n;
}
printf("%d\n",ans);
}
// by SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
int n,p,tot=0,ans=0x3fffffff;
struct Node
{
int x,y;
}node[30005];
bool cmp(Node x,Node y)
{
return x.x<y.x;
}
int read()
{
int x=0;char p=getchar();
while(p>'9'||p<'0')p=getchar();
while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();
return x;
}
int main()
{
n=read();p=read();
for(register int i=1;i<=p;i++)
{
node[i].x=read();node[i].y=read();
if(node[i].x>node[i].y)
{
register int temp=node[i].y;
node[i].y=node[i].x;
node[i].x=temp;
}
}
for(register int i=1;i<n;i++)
{
sort(node+1+tot,node+1+tot+p,cmp);
int Left=0,Right=0,cnt=0;
for(int j=1;j<=p;j++)
{
if(node[j+tot].x<=Right)
{
Right=Right>node[j+tot].y?Right:node[j+tot].y;
}
else
{
cnt+=Right-Left;
Left=node[j+tot].x;
Right=node[j+tot].y;
}
}
cnt+=Right-Left;
ans=ans<cnt?ans:cnt;
while(node[tot+1].x==i)
{
node[tot+1+p].x=node[tot+1].y;
node[tot+1+p].y=node[tot+1].x+n;
tot++;
}
}
printf("%d\n",ans);
}
POJ 1944 并查集(模拟)的更多相关文章
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...
- POJ 3657 并查集
题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- python matplot 绘图
import numpy as np import matplotlib.pyplot as plt plt.figure(1) # 创建图表1 plt.figure(2) # 创建图表2 ax1 = ...
- js 智能识别收获地址
项目地址https://github.com/wzc570738205/smart_parse 支持以下数据格式 马云,1351111111,北京市朝阳区姚家园3楼 马云1351111111北京市朝阳 ...
- 数据库 The Network Adapter could not establish the connection解决方案
连接数据库 注意 url ip地址换的时候 oracle 里的listener.ora thnsnames.ora也要随之变化 重启数据库 不然可能会报出 java.sql.SQLException: ...
- WPF向系统发送消息 并传递结构体
场景 :需要开发一个通讯组件 流程为:界面-开启接收服务-通过发送组件发送信息到 其他客户端和服务端 接受服务接收其他客户端发送的消息 需要传递给对应组件或者界面 因此会出现类库重复引用问题.因为采用 ...
- call使用场景
在javascript OOP中,定义: function cat(){ } cat.prototype={ food:"fish", say: funct ...
- Codeforces Round #283 (Div. 2) A
解题思路:给出一个递增数列,a1,a2,a3,-----,an.问任意去掉a2到a3之间任意一个数之后, 因为注意到该数列是单调递增的,所以可以先求出原数列相邻两项的差值的最大值max, 得到新的一个 ...
- GCD与dispatch_group总结
GCD 可以看作是集合类的操作: 集合是queue,元素是block: 集合只能添加对象,删除对象由系统完成. 同理,NSOperationQueue也可以看作是一个集合管理器. dispatch_g ...
- getattibute 与 getparameter区别
1.getAttribute是取得jsp中 用setAttribute设定的attribute 2.parameter得到的是string:attribute得到的是object 3.request ...
- Eclipse中切换GIT分支
切换GIT分支: 右击项目——Team——Switch To——选择你要切换的分支.
- k8s集群启动了上万个容器(一个pod里放上百个容器,起百个pod就模拟出上万个容器)服务器超时,无法操作的解决办法
问题说明: 一个POD里放了百个容器,然后让K8S集群部署上百个POD,得到可运行上万个容器的实验目的. 实验环境:3台DELL裸机服务器,16核+64G,硬盘容量忽略吧,上T了,肯定够. 1.一开始 ...