bzoj1782[usaco2010feb]slowdown慢慢游*

YuanZiming YuanZiming     2022-08-02     427

关键词:

bzoj1782[Usaco2010 Feb]slowdown 慢慢游

题意:

n只奶牛各有一个目的地。它们按顺序从根节点到达自己的目的地,如果当前奶牛经过了其它已经到达的奶牛的目的地,就要放慢一次脚步。求每只奶牛要放慢多少次脚步。n≤100000。

题解:

对树dfs,求每个节点的进栈时间和出栈时间,然后当每只奶牛到目的地时,将目的地的进栈时间对应数组元素+1,出栈时间对应数组元素-1。每只奶牛的放慢脚步次数就是它出发前它的目的地进栈时间对应数组元素的前缀和。这一过程用树状数组维护。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 100010
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define lb(x) x&-x
 7 using namespace std;
 8 
 9 inline int read(){
10     char ch=getchar(); int f=1,x=0;
11     while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
12     while(ch>=0&&ch<=9)x=x*10+ch-0,ch=getchar();
13     return f*x;
14 }
15 int l[maxn],r[maxn],ho[maxn],sm[maxn*2],n,tot;
16 struct e{int t,n;}; e es[maxn*2]; int ess,g[maxn];
17 void pe(int f,int t){es[++ess]=(e){t,g[f]}; g[f]=ess; es[++ess]=(e){f,g[t]}; g[t]=ess;}
18 void dfs(int x,int fa){l[x]=++tot; for(int i=g[x];i;i=es[i].n)if(es[i].t!=fa)dfs(es[i].t,x); r[x]=++tot;}
19 int query(int x){int q=0; while(x)q+=sm[x],x-=lb(x); return q;}
20 void add(int x,int v){while(x<=tot)sm[x]+=v,x+=lb(x);}
21 int main(){
22     n=read(); inc(i,1,n-1)pe(read(),read()); inc(i,1,n)ho[i]=read(); dfs(1,0);
23     inc(i,1,n){printf("%d
",query(l[ho[i]])); add(l[ho[i]],1); add(r[ho[i]],-1);} return 0;
24 }

 

20160902

bzoj1782[usaco2010feb]slowdown慢慢游

题目描述每天FarmerJohn的N头奶牛(1<=N<=100000,编号1…N)从粮仓走向他的自己的牧场。牧场构成了一棵树,粮仓在1号牧场。恰好有N-1条道路直接连接着牧场,使得牧场之间都恰好有一条路径相连。第i条路连接着A_i,B_i,(1<=A_i... 查看详情

bzoj1697:[usaco2007feb]cowsorting牛排序&bzoj1119:[poi2009]slo

思路:以bzoj1119为例,题目已经给出了置换,而每一次交换的代价是交换二者的权值之和,而置换一定是会产生一些环的,这样就可以只用环内某一个元素去置换而使得其余所有元素均在正确的位置上,显然要选择环内最小的数... 查看详情

bzoj1119[poi2009]slo&&bzoj1697[usaco2007feb]cowsorting牛排序——思路(置换)(代码片段)

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119   https://www.lydsy.com/JudgeOnline/problem.php?id=1697先找到置换的循环节。发现对于同一个循环节里的元素,可以找一个代价最小的元素,用它把所有元素换到位置上。每次交换一定有... 查看详情

bzoj2015:[usaco2010feb]chocolategivingspfa

因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可#include<iostream>#include<cstdio>#include<queue>usingnamespacestd;constintN=50005,inf=1e9;intn,m,b,h[N],cnt,dis[N];boolv[N];structqweintne,to, 查看详情

bzoj1631usaco2007feb.cowparty

【题解】  最短路裸题。。  本题要求出每个点到终点走最短路来回的距离,因此我们先跑一遍最短路得出每个点到终点的最短距离,然后把边反向再跑一遍最短路,两次结果之和即是答案。  #include<cstdio>#include<al... 查看详情

[bzoj2591][usaco2012feb]nearbycows

2591:[Usaco2012Feb]NearbyCowsTimeLimit:4Sec  MemoryLimit:128MBSubmit:149  Solved:89[Submit][Status][Discuss]Description FarmerJohnhasnoticedthathiscowsoftenmovebetweennearbyfi 查看详情

bzoj3942:[usaco2015feb]censoring

3942:[Usaco2015Feb]CensoringTimeLimit: 10Sec  MemoryLimit: 128MBSubmit: 404  Solved: 221[Submit][Status][Discuss]DescriptionFarmerJohnhaspurchasedasubscriptiont 查看详情

bzoj3940[usaco2015feb]censoring

DescriptionFarmerJohnhaspurchasedasubscriptiontoGoodHooveskeepingmagazineforhiscows,sotheyhaveplentyofmaterialtoreadwhilewaitingaroundinthebarnduringmilkingsessions.Unfortunately,thelatestissuecontain 查看详情

bzoj1651:[usaco2006feb]stallreservations专用牛棚

二次联通门: BZOJ1651:[Usaco2006Feb]StallReservations专用牛棚   权限题放题面DescriptionOhthosepickyN(1<=N<=50,000)cows!TheyaresopickythateachonewillonlybemilkedoversomeprecisetimeintervalA.. 查看详情

[bzoj]1631:[usaco2007feb]cowparty

1631:[Usaco2007Feb]CowPartyTimeLimit:5Sec  MemoryLimit:64MBSubmit:866  Solved:624[Submit][Status][Discuss]Description    农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X 查看详情

bzoj4412[usaco2016feb]circularbarn

先看成一条链for一遍找位置在for一遍算答案#include<algorithm>#include<iostream>#include<cstring>#include<string>#include<cstdio>#include<cmath>usingnamespacestd;typedeflonglongLL;#def 查看详情

[bzoj]1651:[usaco2006feb]stallreservations专用牛棚

1651:[Usaco2006Feb]StallReservations专用牛棚TimeLimit:10Sec  MemoryLimit:64MBSubmit:987  Solved:566[Submit][Status][Discuss]DescriptionOhthosepickyN(1<=N<=50,000)cows!Theyaresopi 查看详情

[bzoj1651][usaco2006feb]stallreservations专用牛棚

1651:[Usaco2006Feb]StallReservations专用牛棚TimeLimit:10Sec  MemoryLimit:64MBSubmit:990  Solved:568[Submit][Status][Discuss]DescriptionOhthosepickyN(1<=N<=50,000)cows!Theyaresopi 查看详情

[bzoj1652][usaco06feb]treatsforthecows题解(区间dp)(代码片段)

[BZOJ1652][USACO06FEB]TreatsfortheCowsDescriptionFJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyforgivingvastamountsofmilk.FJsellsonetreatperdayandwantstomaximizethemoneyhereceivesov 查看详情

bzoj1651:[usaco2006feb]stallreservations专用牛棚

1651:[Usaco2006Feb]StallReservations专用牛棚TimeLimit: 10Sec  MemoryLimit: 64MBSubmit: 942  Solved: 544[Submit][Status][Discuss]DescriptionOhthosepickyN(1<=N< 查看详情

[bzoj]1611:[usaco2008feb]meteorshower流星雨

1611:[Usaco2008Feb]MeteorShower流星雨TimeLimit:5Sec  MemoryLimit:64MBSubmit:1654  Solved:707[Submit][Status][Discuss]Description去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息:一场流星雨即将袭击整个霸... 查看详情

bzoj1734:[usaco2005feb]aggressivecows愤怒的牛

1734:[Usaco2005feb]Aggressivecows愤怒的牛DescriptionFarmerJohnhasbuiltanewlongbarn,withN(2<=N<=100,000)stalls.Thestallsarelocatedalongastraightlineatpositionsx1,...,xN(0<=xi<=1,000,000,000).Hi 查看详情

bzoj3940[usaco2015feb]censoring*

bzoj3940[Usaco2015Feb]Censoring题意:有一个S串和一大堆T串,不断地在S串里找最早出现的T串,然后将其删除。S串≤100000,T串总长度≤100000。题解:对所有T串建AC自动机,然后同bzoj3942。注意,本题的AC自动机必须利用所有fail函数建成... 查看详情