USACO月赛2017.02 铂金组T1--MINCROSS
来源:互联网 发布:查看80端口的命令 编辑:程序博客网 时间:2024/04/27 20:51
Description
Why did the cow cross the road? We may never know the full reason, but it is certain
that Farmer John’s cows do end up crossing the road quite frequently. In fact, they
end up crossing the road so often that they often bump into each-other when their
paths cross, a situation Farmer John would like to remedy.
Farmer John raises NN breeds of cows (1≤N≤100,000), and each of his fields is
dedicated to grazing for one specific breed; for example, a field dedicated to breed
12 can only be used for cows of breed 12 and not of any other breed. A long road
runs through his farm. There is a sequence of NN fields on one side of the road (one
for each breed), and a sequence of NN fields on the other side of the road (also one
for each breed). When a cow crosses the road, she therefore crosses between the
two fields designated for her specific breed.
Had Farmer John planned more carefully, he would have ordered the fields by breed
the same way on both sides of the road, so the two fields for each breed would be
directly across the road from each-other. This would have allowed cows to cross the
road without any cows from different breeds bumping into one-another. Alas, the
orderings on both sides of the road might be different, so Farmer John observes that
there might be pairs of breeds that cross. A pair of different breeds (a,b)(a,b) is
“crossing” if any path across the road for breed aa must intersect any path across the
road for breed bb.
Farmer John would like to minimize the number of crossing pairs of breeds. For
logistical reasons, he figures he can move cows around on one side of the road so
the fields on that side undergo a “cyclic shift”. That is, for some
cow re-locates to the field kk fields ahead of it, with the cows in the last kk fields
moving so they now populate the first kk fields. For example, if the fields on one side
of the road start out ordered by breed as 3, 7, 1, 2, 5, 4, 6 and undergo a cyclic shift
by k=2k=2, the new order will be 4, 6, 3, 7, 1, 2, 5. Please determine the minimum
possible number of crossing pairs of breeds that can exist after an appropriate cyclic
shift of the fields on one side of the road.
INPUT FORMAT (file mincross.in):
The first line of input contains NN. The next NN lines describe the order, by breed ID,
of fields on one side of the road; each breed ID is an integer in the range 1…N1…N.
The last NN lines describe the order, by breed ID, of the fields on the other side of
the road.
OUTPUT FORMAT (file mincross.out):
Please output the minimum number of crossing pairs of breeds after a cyclic shift of
the fields on one side of the road (either side can be shifted).
题解
额,太水了。先树状数组求出不移动的答案,然后一个一个枚举推一下就好。(看到有人写的主席树?沃不会啊)
代码
#include<cstdio>#include<cstring>#include<algorithm>#define maxn 100006#define lowbit(x) (x&-x)#define LL long longusing namespace std;inline char nc(){ static char buf[100000],*i=buf,*j=buf; return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;}inline int _read(){ char ch=nc();int sum=0; while(!(ch>='0')&&ch<='9')ch=nc(); while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc(); return sum;}int n,a[maxn],b[maxn],p1[maxn],p2[maxn];LL ans,ans0,ans1,f[maxn];void put(int x,int y){for(;x<=n;x+=lowbit(x))f[x]+=y;}int get(int x){ int sum=0; for(;x;x-=lowbit(x))sum+=f[x]; return sum;}int main(){ freopen("mincross.in","r",stdin); freopen("mincross.out","w",stdout); n=_read(); for(int i=1;i<=n;i++)a[i]=_read(),p1[a[i]]=i; for(int i=1;i<=n;i++)b[i]=_read(),p2[b[i]]=i; for(int i=n;i>=1;i--)ans0+=get(p1[b[i]]),put(p1[b[i]],1); ans=ans1=ans0; for(int i=1;i<=n;i++){ ans0=ans0-p1[b[i]]+1+n-p1[b[i]]; ans=min(ans,ans0); } for(int i=1;i<=n;i++){ ans1=ans1-p2[a[i]]+1+n-p2[a[i]]; ans=min(ans,ans1); } printf("%lld",ans); return 0;}
- USACO月赛2017.02 铂金组T1--MINCROSS
- USACO月赛2017.02 铂金组T2--NOCROSS
- USACO月赛2017.02 铂金组T3--FRIENDCROSS【CDQ分治】
- USACO二月月赛 铂金组 friendcross CDQ分治+树状数组
- 2014.3 USACO月赛T1 Watering the Fields
- USACO月赛数据
- usaco月赛geteven
- usaco月赛cowroute
- usaco月赛MOOCRYPT
- usaco月赛palpath
- usaco月赛trapped
- USACO月赛 晚餐
- 【USACO月赛】贪心
- [usaco月赛]梦幻王国
- 2017年8月7日提高组T1 呵呵
- 2017年8月7日提高组T1 呵呵
- 2017年8月7日提高组T1 呵呵
- 2017年8月7日提高组T1 根
- 王爽.汇编.第三版.课程设计2
- 水题 第三站 荷兰国旗问题
- 【Win32多线程】异步I/O技术(Overlapped I/O),避免使用多线程
- 购物商城之产品数据模型设计
- 与老板谈薪酬时,一定不要犯这些错误
- USACO月赛2017.02 铂金组T1--MINCROSS
- MySQL索引原理及慢查询优化
- Android Context使用时候的区别
- 你我都是被时间杀死的人
- 打开word提示:The setup controller has encountered a problem during instll.
- Oracle锁的模式和10704事件跟踪对比
- Web服务描述、是什么
- nodejs express 快速搭建一个项目过程
- 软件测试概述