思路:

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

1.用类似并查集的思想… f[x]=max(f[x],y)表示x和y相连(一定要注意取max,,,血的教训) 复杂度O(np)

2.猥琐思路 每回枚举完断点以后sort一遍 用左右指针扫一遍就OK..

需要高超的卡时技巧就能过 复杂度:O(nplogp)

  1. // by SiriusRen
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. int n,p,ans=0x3fffffff,f[2005];
  7. struct Node{int x,y;}node[10005];
  8. int main(){
  9. scanf("%d%d",&n,&p);
  10. for(int i=1;i<=p;i++){
  11. scanf("%d%d",&node[i].x,&node[i].y);
  12. if(node[i].x>node[i].y)swap(node[i].x,node[i].y);
  13. }
  14. for(int i=1;i<n;i++){
  15. memset(f,0,sizeof(f));
  16. int cnt=0,Left=0,Right=0;
  17. for(int j=1;j<=p;j++)
  18. f[node[j].x]=max(node[j].y,f[node[j].x]);
  19. for(int j=i;j<=i+n;j++)
  20. if(j>Right&&f[j])
  21. cnt+=Right-Left,Left=j,Right=f[j];
  22. else
  23. Right=max(Right,f[j]);
  24. ans=min(ans,cnt+Right-Left);
  25. for(int j=1;j<=p;j++)
  26. if(node[j].x==i)
  27. swap(node[j].x,node[j].y),node[j].y+=n;
  28. }
  29. printf("%d\n",ans);
  30. }
  1. // by SiriusRen
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int n,p,tot=0,ans=0x3fffffff;
  6. struct Node
  7. {
  8. int x,y;
  9. }node[30005];
  10. bool cmp(Node x,Node y)
  11. {
  12. return x.x<y.x;
  13. }
  14. int read()
  15. {
  16. int x=0;char p=getchar();
  17. while(p>'9'||p<'0')p=getchar();
  18. while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();
  19. return x;
  20. }
  21. int main()
  22. {
  23. n=read();p=read();
  24. for(register int i=1;i<=p;i++)
  25. {
  26. node[i].x=read();node[i].y=read();
  27. if(node[i].x>node[i].y)
  28. {
  29. register int temp=node[i].y;
  30. node[i].y=node[i].x;
  31. node[i].x=temp;
  32. }
  33. }
  34. for(register int i=1;i<n;i++)
  35. {
  36. sort(node+1+tot,node+1+tot+p,cmp);
  37. int Left=0,Right=0,cnt=0;
  38. for(int j=1;j<=p;j++)
  39. {
  40. if(node[j+tot].x<=Right)
  41. {
  42. Right=Right>node[j+tot].y?Right:node[j+tot].y;
  43. }
  44. else
  45. {
  46. cnt+=Right-Left;
  47. Left=node[j+tot].x;
  48. Right=node[j+tot].y;
  49. }
  50. }
  51. cnt+=Right-Left;
  52. ans=ans<cnt?ans:cnt;
  53. while(node[tot+1].x==i)
  54. {
  55. node[tot+1+p].x=node[tot+1].y;
  56. node[tot+1+p].y=node[tot+1].x+n;
  57. tot++;
  58. }
  59. }
  60. printf("%d\n",ans);
  61. }

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. java.io.IOException: Cannot find any registered HttpDestinationFactory from the Bus.

    转自:https://blog.csdn.net/u012849872/article/details/51037374

  2. centos + nodejs + egg2.x 开发微信分享功能

    本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd

  3. C++ MAP使用

    1. map的构造函数map<int,string> maphai;map<char,int> maphai;map<string,char> mapstring; ...

  4. ffmpeg x264编译与使用介绍

    问题1:我用的是最新版本的ffmpeg和x264,刚刚编译出来,编译没有问题,但是在linux 环境使用ffmpeg的库时发现报错error C3861: 'UINT64_C': identifier ...

  5. 使用 async/ await 进行 异步 编程

    一.异步函数 异步函数概念. 通常 是 指用 async 修饰 符 声明 的, 可 包含 await 表达式 的 方法 或 匿名 函数 1. 从 语言 的 视角 来看, 这些 await 表达式 正是 ...

  6. heavy dark--读《《暗时间》》

    本书名为<<暗时间>>,个人觉得是一个非常好的名字:1.迷茫的大学生有多少的业余时间,但又浪费多少的业余时间,浪费的这莫多时间就如同人在黑夜中一样,大脑是在休息的状态.这是第一 ...

  7. pgpool中的配置参数的定义

    /* * configuration parameters */typedef struct {    char *listen_addresses;            /* hostnames/ ...

  8. oracle插入或更新某一个指定列来执行触发器

    表结构: create table TZ_GXSX ( ID VARCHAR2(15), PROJECT VARCHAR2(50), TXYX NUMBER(22) default '0', CDAT ...

  9. ucore_lab0

    一直想好好学习一下操作系统课程,去一个Mooc网站上找了一门操作系统的课程.这便是里面的配套实验. 实验指导:点这里 lab0主要是准备相关的操作环境.课程推荐使用qemu作为硬件模拟器,推荐运行环境 ...

  10. STM32 IIC双机通信—— HAL库硬件IIC版

    参考传送门 关于IIC的原理这里我就不多说了,网上有很多很好的解析,如果要看我个人对IIC的理解的话,可以点击查看,这里主要讲一下怎样利用STM32CubeMx实现IIC的通讯,经过个人实践,感觉HA ...