usaco 2002 月赛 Fiber Communications 题解
Description
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
* Lines 2..P+1: two integers describing a pair of barns between which communication is desired. No pair is duplicated in the list.
Output
一看以为是二分图,然后发现自己不会。。。
一看他人的简要题解,发现思路很简单:首先我们假设只是直线式的,那么像“火烧赤壁”那样的线段树可以轻松解决。然后我们就枚举断点,把效率乘上一个n就行了。
自己一琢磨,不对啊!因为是枚举断点,所以每个要求可能x和y会变化(倘若我们像破碎的项链把它展开的话)。这样,每次枚举后,就要重新快排,而且之前要p的效率把a拷贝和处理一下。这样,效率是O(n*p*log(p)),看来要爆了。正在吭哧吭哧写代码,晨阳发现一个直接暴力桶排染色的程序,它竟然A了!它的最坏效率应该是O(n^2*p)!!!!因为我那个相对优化的程序比较麻烦,我就毅然决定也写暴力的,关键是常数要压的小一点。
附注:我们在poj开了个小号:JSBLHYSYC。那里各种的编译器真是令人眼花缭乱。终于,我在C++的编译器中过了!!(以前我都是用G++的)
#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 题解的更多相关文章
- usaco 2002 月赛 Chores 题解
Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...
- usaco 2008 月赛 lites 开关灯 题解
题目: Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是 牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每一 ...
- POJ1944 Fiber Communications (USACO 2002 February)
Fiber Communications 总时间限制: 1000ms 内存限制: 65536kB 描述 Farmer John wants to connect his N (1 <= N ...
- TOJ1550: Fiber Communications
1550: Fiber Communications Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal ...
- [USACO2002][poj1944]Fiber Communications(枚举)
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3804 Accepted: 1 ...
- POJ 1944:Fiber Communications
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4236 Accepted: 1 ...
- USACO全部月赛及GateWay数据
月赛: 以07年open为例,网站如下 http://contest.usaco.org/OPEN07 其他的格式是http://contest.usaco.org/月份(月份的英文前三位,比如1月是 ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- ZOJ Monthly, June 2014 月赛BCDEFGH题题解
比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...
随机推荐
- 2017 UESTC Training for Data Structures
http://acm.uestc.edu.cn/#/contest/show/155 对大二来说貌似这套题有点简单了,多是一眼题 发现漏了一题,然而是以前看别人讨论过的:). H:线段树+暴力.大概就 ...
- 开涛spring3(3.2) - DI之循环依赖
3.2.1 什么是循环依赖 循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方,比如CircleA引用CircleB,CircleB引用 CircleC,CircleC引用CircleA ...
- .Net Core应用搭建的分布式邮件系统设计
本篇分享的是由NetCore搭建的分布式邮件系统,主要采用NetCore的Api和控制台应用程序,由于此系统属于公司的所以这里只能分享设计图和一些单纯不设计业务的类或方法: 为什么要在公司中首例采用N ...
- ELK菜鸟手记 (四) - 利用filebeat和不同端口把不同服务器上的log4j日志传输到同一台ELK服务器
1. 问题描述 我们需要将不同服务器(如Web Server)上的log4j日志传输到同一台ELK服务器,介于公司服务器资源紧张(^_^) 2. 我们需要用到filebeat 什么是filebeat ...
- Kotlin入门第三课:数据类型
前文链接: Kotlin学习第一课:从对比Java开始 Kotlin入门第二课:集合操作 初次尝试用Kotlin实现Android项目 Kotlin的数据类型与Java类似,因此这篇文章主要看Kotl ...
- [转]tomcat部署
转载博客原文地址: http://www.cnblogs.com/xing901022/p/4463896.html 阅读目录 介绍 静态部署——在tomcat启动时部署 动态部署——在tomcat ...
- [Leetcode] Binary search--275 H-Index
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- mysql之 mysql 5.6不停机双主一从搭建(活跃双主一从基于日志点复制)
环境说明:版本 version 5.6.25-log 主1库ip: 10.219.24.25主2库ip: 10.219.24.22从1库ip:10.219.24.26os 版本: centos 6.7 ...
- css让文字在一行内显示
1.例如 p元素,里面的文字不换行显示,超出部分不隐藏 p{ width:100px; word-break:keep-all; white-space:nowrap; } 2.例如 p元素,里面的文 ...
- javascript基础数据类型与引用类型
javascript一共有6种数据类型 有5种基本类型:Null,String Number,Boolean,Undefined 和一种引用类型Object 基础类型在内存中存在于栈空间中,例如 va ...