思路:

肯定是要枚举断点的。。就看枚举完断点以后怎么处理了……

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 并查集(模拟)的更多相关文章

  1. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  2. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  3. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  4. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  5. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  6. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  7. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  8. hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...

  9. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

随机推荐

  1. hdoj--1166--敌兵布阵(线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. [MySQL] 查询一段时间记录

    24小时内记录(即86400秒) $sql="SELECT video_id,count(id)as n FROM `rec_down` WHERE UNIX_TIMESTAMP(NOW() ...

  3. Flask-Restful

    定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...

  4. ZYQAssetPickerController的使用,不错的图片选择

    import UIKit class ViewController: UIViewController,ZYQAssetPickerControllerDelegate,UIImagePickerCo ...

  5. MVC的一些常用特性,持续更新中。。。

    1. @MvcHtmlString.Create("<option value='1'>火星</option>")   //渲染Html

  6. ubuntu16.04 安装 docker-compose

    下载安装 docker-composecurl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose ...

  7. firstChild与firstElementChild

    相同点: 都是获取父元素下的第一个节点对象 不同点: firstChild: IE6.7.8 第一个元素节点; 非IE6.7.8:返回第一个元素节点或文本节点 firstElementChild: I ...

  8. SQLAlchemy tutorial

    SQLAlchemy tutorial 重要概念 ORM:数据库对象 <--> class --> 类实例instance 定义数据库连接 session:数据库事务通过sessio ...

  9. 防范CSRF(二)

    在防范CSRF(一)中使用的是微软默认的设置.在信息安全中默认的往往是最危险的.因此可以考虑更改cookie中默认的名字. 更改默认操作在Global.asax中的Application_Start使 ...

  10. 「JavaSE 重新出发」05.01 继承

    继承 一个对象变量可以指示多种实际类型的现象被称为多态(polymorphism). 在运行时能够自动地选择调用哪个方法的现象称为动态绑定(dynamic binding). 如果是private方法 ...