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;
}

usaco 2002 月赛 Fiber Communications 题解的更多相关文章

  1. usaco 2002 月赛 Chores 题解

    Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...

  2. usaco 2008 月赛 lites 开关灯 题解

      题目:     Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是 牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每一 ...

  3. POJ1944 Fiber Communications (USACO 2002 February)

    Fiber Communications 总时间限制:  1000ms 内存限制:  65536kB 描述 Farmer John wants to connect his N (1 <= N ...

  4. TOJ1550: Fiber Communications

    1550: Fiber Communications  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal ...

  5. [USACO2002][poj1944]Fiber Communications(枚举)

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3804   Accepted: 1 ...

  6. POJ 1944:Fiber Communications

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4236   Accepted: 1 ...

  7. USACO全部月赛及GateWay数据

    月赛: 以07年open为例,网站如下 http://contest.usaco.org/OPEN07 其他的格式是http://contest.usaco.org/月份(月份的英文前三位,比如1月是 ...

  8. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  9. ZOJ Monthly, June 2014 月赛BCDEFGH题题解

    比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...

随机推荐

  1. SCAU Individual Contest #1

    总结一下就是自己太弱.每次打比赛遇到比较难题就喜欢瞎开题,结果都是每题想一下,然后就是结束了. A:题意让你用小写字母构造一个总共有K个的回文串,比如aba的话就是{a}{b}{a}{aba}四个,比 ...

  2. Http学习之使用HttpURLConnection发送post和get请求(2)

    接上节Http学习之使用HttpURLConnection发送post和get请求 本节深入学习post请求. 上 节说道,post请求的OutputStream实际上不是网络流,而是写入内存,在ge ...

  3. [转载]OpenStack OVS GRE/VXLAN网络

      学习或者使用OpenStack普遍有这样的现象:50%的时间花费在了网络部分:30%的时间花费在了存储方面:20%的时间花费在了计算方面.OpenStack网络是不得不逾越的鸿沟,接下来我们一起尝 ...

  4. 【小练习02】CSS--网易产品

    要求用css和HTML实现下图效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  5. iOS地理围栏技术的应用

    遇到一个需求,要求监测若干区域,设备进入这些区域则要上传数据,且可以后台监测,甚至app被杀死也要监测.发现oc的地理围栏技术完美匹配这个需求,任务做完了,把遇到的坑记录下来,也许能帮到你呢. 要做这 ...

  6. Syntax error on tokens, delete these tokens.问题解决

    问题:Syntax error on tokens, delete these tokens.(在标记语法错误,删除这些标记) 原因:存在中文字符或者符号(包括空格):单引号.双引号不匹配:复制的代码 ...

  7. 每天一个JS 小demo之“随机”抽奖。主要知识点:Math函数,数组方法,递归

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  8. 免费在线生成彩色带logo的个性二维码

          码工具网站提供免费的在线二维码生成服务,可以把网址.文本.电子邮件.短信.电话号码.电子名片.wifi网络等信息生成对应的二维码图片.你可以设置二维码图片的格式(png,jpg,gif). ...

  9. 修改cms版权等等信息

    目的:为DedeCMS换上精美多样的提示信息窗口 用到的开源项目:DedeCMS,artdialog 步骤: 1.下载include.rar文件完成后,解压得到2个php文件和一个使用说明文件,将ph ...

  10. 包装FTPWebRequest类

    上篇文章讨论了C#从基于FTPS的FTP server下载数据 (FtpWebRequest 的使用)SSL 加密.不过细心的朋友应该可以发现FTPWebRequest 每次都是新生成一个reques ...