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.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- java.io.IOException: Cannot find any registered HttpDestinationFactory from the Bus.
转自:https://blog.csdn.net/u012849872/article/details/51037374
- centos + nodejs + egg2.x 开发微信分享功能
本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd
- C++ MAP使用
1. map的构造函数map<int,string> maphai;map<char,int> maphai;map<string,char> mapstring; ...
- ffmpeg x264编译与使用介绍
问题1:我用的是最新版本的ffmpeg和x264,刚刚编译出来,编译没有问题,但是在linux 环境使用ffmpeg的库时发现报错error C3861: 'UINT64_C': identifier ...
- 使用 async/ await 进行 异步 编程
一.异步函数 异步函数概念. 通常 是 指用 async 修饰 符 声明 的, 可 包含 await 表达式 的 方法 或 匿名 函数 1. 从 语言 的 视角 来看, 这些 await 表达式 正是 ...
- heavy dark--读《《暗时间》》
本书名为<<暗时间>>,个人觉得是一个非常好的名字:1.迷茫的大学生有多少的业余时间,但又浪费多少的业余时间,浪费的这莫多时间就如同人在黑夜中一样,大脑是在休息的状态.这是第一 ...
- pgpool中的配置参数的定义
/* * configuration parameters */typedef struct { char *listen_addresses; /* hostnames/ ...
- oracle插入或更新某一个指定列来执行触发器
表结构: create table TZ_GXSX ( ID VARCHAR2(15), PROJECT VARCHAR2(50), TXYX NUMBER(22) default '0', CDAT ...
- ucore_lab0
一直想好好学习一下操作系统课程,去一个Mooc网站上找了一门操作系统的课程.这便是里面的配套实验. 实验指导:点这里 lab0主要是准备相关的操作环境.课程推荐使用qemu作为硬件模拟器,推荐运行环境 ...
- STM32 IIC双机通信—— HAL库硬件IIC版
参考传送门 关于IIC的原理这里我就不多说了,网上有很多很好的解析,如果要看我个人对IIC的理解的话,可以点击查看,这里主要讲一下怎样利用STM32CubeMx实现IIC的通讯,经过个人实践,感觉HA ...