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. Java IO流学习总结(1)

    Java IO流学习总结 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本 ...

  2. data.table包简介

    data.table包主要特色是:设置keys.快速分组和滚得时序的快速合并.data.table主要通过二元检索法大大提高数据操作的效率,同时它也兼容适用于data.frame的向量检索法. req ...

  3. ProxySQL的相关维护说明

    背景: 前面的2篇文章MySQL ProxySQL读写分离使用初探和MySQL ProxySQL读写分离实践大致介绍了ProxySQL的使用说明,从文章的测试的例子中看到ProxySQL使用SQLIT ...

  4. javascript基础-表单

    图解:   表单只需要知道常用的,其他的了解原理就行.在实际项目中,序列化一般用库(jquery)带的方法.富文本引用组件.

  5. 将数据的初始化放到docker中的整个工作过程(问题记录)

    将数据的初始化放到docker中的整个工作过程 由于是打算作为个人博客,所以对于install这个步骤,我从一开始就打算删掉的,前面一个多星期一直在修bug,到前天才开始做这个事情. 过程中也是碰到了 ...

  6. (原创) Maven查看JAR包的依赖关系

    如果是用命令行,可进入项目所在目录,然后输入: mvn dependency:tree ,来查看jar包依赖关系. 另外还可以在eclipse操作,如下图所示: 点击run后,开始输出JAR包依赖树. ...

  7. Delphi使用Zint生成QR二维条码(zint.dll)

    Delphi使用Zint生成QRCODE 本文使用的Zint Barcode Library(zint.dll)版本为2.6.0,和之前使用的2.4.3版本在zint_symbol这个结构体上会有差异 ...

  8. 用css控制字数,多余的用省略号代替

    选择器 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100px; } white-space 属性 ...

  9. Bash变量扩展修改符

    1.未设置就临时替换(:-) 冒号:用来检验变量是否设置过,如果没有冒号,则认为设置过,不替换$fruit=peach$echo ${fruit:-plum}peach $fruit=$echo ${ ...

  10. .NET ORM框架 SqlSuagr4.0 功能详解与实践【开源】

    SqlSugar 4.0 ORM框架的优势 为了未来能够更好的支持多库分布式的存储,并行计算等功能,将SqlSugar3.x全部重写,现有的架构可以轻松扩展多库. 源码下载: https://gith ...