Description

Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a new fiber-optic network. However, the barns are located in a circle around the edge of a large pond, so he can only connect pairs of adjacent barns. The circular configuration
means that barn N is adjacent to barn 1. 



FJ doesn't need to connect all the barns, though, since only certain pairs of cows wish to communicate with each other. He wants to construct as few 

connections as possible while still enabling all of these pairs to communicate through the network. Given the list of barns that wish to communicate with each other, determine the minimum number of lines that must be laid. To communicate from barn 1 to barn
3, lines must be laid from barn 1 to barn 2 and also from barn 2 to barn 3(or just from barn 3 to 1,if n=3).

Input

* Line 1: Two integers, N and P (the number of communication pairs, 1 <= P <= 10,000) 



* Lines 2..P+1: two integers describing a pair of barns between which communication is desired. No pair is duplicated in the list. 

Output

One line with a single integer which is the minimum number of direct connections FJ needs to make. 

这道题。。。哎,说多了都是泪啊!这道题大意是:有n个谷仓环状连在一起,只有相邻两个之间可以铺一条路。给出p个要求,每个要求x,y表示x和y想联系。当然,因为环形,可以双向铺路。求最少铺多少路。



        一看以为是二分图,然后发现自己不会。。。

一看他人的简要题解,发现思路很简单:首先我们假设只是直线式的,那么像“火烧赤壁”那样的线段树可以轻松解决。然后我们就枚举断点,把效率乘上一个n就行了。

       自己一琢磨,不对啊!因为是枚举断点,所以每个要求可能x和y会变化(倘若我们像破碎的项链把它展开的话)。这样,每次枚举后,就要重新快排,而且之前要p的效率把a拷贝和处理一下。这样,效率是O(n*p*log(p)),看来要爆了。正在吭哧吭哧写代码,晨阳发现一个直接暴力桶排染色的程序,它竟然A了!它的最坏效率应该是O(n^2*p)!!!!因为我那个相对优化的程序比较麻烦,我就毅然决定也写暴力的,关键是常数要压的小一点。

      附注:我们在poj开了个小号:JSBLHYSYC。那里各种的编译器真是令人眼花缭乱。终于,我在C++的编译器中过了!!(以前我都是用G++的)

代码(要求1000ms,压着就969ms过去了)
#include<stdio.h>
#include<cstring>
using namespace std;
struct arr{long x,y;}a[10001];
char f[2001];
long start,end,xx,yy,i,j,k,min,n,p,t;
inline long c(long x)
{
  if (x<start) return x+n;if (x>end) return x-n;
  return x;
}
int main()
{
  scanf("%ld %ld",&n,&p);
  for (i=1;i<=p;i++)
    {
      scanf("%ld %ld",&a[i].x,&a[i].y);
      if (a[i].x>a[i].y) {t=a[i].x;a[i].x=a[i].y;a[i].y=t;}
    }
  long ans=n;
  for (i=1;i<=n;i++)
  {
    start=i;end=start+n-1;min=0;
    memset(f,0,sizeof(f));
    for (j=1;j<=p;j++)
    {
      xx=c(a[j].x);yy=c(a[j].y);
      if (xx>yy) {t=xx;xx=yy;yy=t;}
      for (k=xx;k<yy;k++) f[k]=1;
    }
    for (j=start;j<=end;j++) if (f[j]==1) min++;
    if (min<ans) ans=min;
  }
  printf("%ld",ans);
  //scanf("%ld %ld",&n,&p);
  return 0;
}

最新文章

  1. Attach Volume 操作(Part I) - 每天5分钟玩转 OpenStack(53)
  2. 淘宝弹性布局方案lib-flexible实践
  3. 分享关于Entity Framework 进行CRUD操作实验的结果
  4. 用swift实现自动录音器
  5. hibernate日常BUG总结
  6. Windows C++ 子目录数量
  7. ubuntu14.04上安装Mysql-5.7.11
  8. ruby脚本,随机生成复杂密码
  9. iOS开发之指定UIView的某几个角(小于4)为圆角
  10. javascript为目标标签添加class样式
  11. MCU助推居家移动医疗微型化
  12. hdu3709(数位dp)
  13. Ubuntu通过ADB连接手机
  14. sketch2code 有的叫screenshot to code什么的
  15. 摩羯座Capricornus
  16. 《2018面向对象程序设计(Java)课程学习进度条》
  17. 潭州课堂25班:Ph201805201 django 项目 第十三课 短信验证码后台的实现 (课堂笔记)
  18. js 对象的_proto_属性 和函数的prototype属性分析
  19. Linux常用指令之一
  20. 用 .NET Memory Profiler 跟踪.net 应用内存使用情况--基本应用篇(转)

热门文章

  1. (基础篇 走进javaNIO)第二章-NIO入门
  2. 开涛spring3(7.3) - 对JDBC的支持 之 7.3 关系数据库操作对象化
  3. RavenDB FS 安装使用 介绍
  4. matlab错误:Subscript indices must either be real positive integers or logicals.
  5. Vulkan Tutorial 08 交换链
  6. Java 9 揭秘(2. 模块化系统)
  7. dedecms列表页调用子栏目列表,织梦首页调用栏目的子栏目标签代码
  8. Spring学习(3)---Spring设值注入和构造注入
  9. iframe实现自适应高度
  10. shell脚本调用C语言之字符串切分之strtok函数