codeforces_731D_(前缀和)(树状数组)
2016-11-21 19:08
501 查看
D.80-thLevelArcheology
timelimitpertest
2seconds
memorylimitpertest
256megabytes
input
standardinput
output
standardoutput
ArcheologistshavefoundasecretpassinthedungeonofoneofthepyramidsofCycleland.Toenterthetreasurytheyhavetoopenanunusuallockonthedoor.Thelockconsistsofnwords,eachconsistingofsomehieroglyphs.Thewallnearthelockhasaroundswitch.Eachrotationofthisswitchchangesthehieroglyphsaccordingtosomerules.Theinstructionnearbysaysthatthedoorwillopenonlyifwordswrittenonthelockwouldbesortedinlexicographicalorder(thedefinitionoflexicographicalcomparisoningiveninnotessection).
Therulethatchangeshieroglyphsisthefollowing.Oneclockwiserotationoftheroundswitchreplaceseachhieroglyphwiththenexthieroglyphinalphabet,i.e.hieroglyphx(1 ≤ x ≤ c - 1)isreplacedwithhieroglyph(x + 1),andhieroglyphcisreplacedwithhieroglyph1.
Helparcheologistdetermine,howmanyclockwiserotationstheyshouldperforminordertoopenthedoor,ordeterminethatthisisimpossible,i.e.nocyclicshiftofthealphabetwillmakethesequenceofwordssortedlexicographically.
Input
Thefirstlineoftheinputcontainstwointegersnandc(2 ≤ n ≤ 500 000,1 ≤ c ≤ 106)—thenumberofwords,writtenonthelock,andthenumberofdifferenthieroglyphs.
Eachofthefollowingnlinescontainsthedescriptionofoneword.Thei-thoftheselinesstartswithintegerli(1 ≤ li ≤ 500 000),thatdenotesthelengthofthei-thword,followedbyliintegerswi, 1,wi, 2,...,wi, li(1 ≤ wi, j ≤ c)—theindicesofhieroglyphsthatmakeupthei-thword.Hieroglyphwithindex1isthesmallestinthealphabetandwithindexc—thebiggest.
It'sguaranteed,thatthetotallengthofallwordsdoesn'texceed106.
Output
Ifitispossibletoopenthedoorbyrotatingtheroundswitch,printintegerx(0 ≤ x ≤ c - 1)thatdefinestherequirednumberofclockwiserotations.Ifthereareseveralvalidx,printanyofthem.
Ifitisimpossibletoopenthedoorbythismethod,print - 1.
Examples
input
output
input
output
input
output
树状数组:
timelimitpertest
2seconds
memorylimitpertest
256megabytes
input
standardinput
output
standardoutput
ArcheologistshavefoundasecretpassinthedungeonofoneofthepyramidsofCycleland.Toenterthetreasurytheyhavetoopenanunusuallockonthedoor.Thelockconsistsofnwords,eachconsistingofsomehieroglyphs.Thewallnearthelockhasaroundswitch.Eachrotationofthisswitchchangesthehieroglyphsaccordingtosomerules.Theinstructionnearbysaysthatthedoorwillopenonlyifwordswrittenonthelockwouldbesortedinlexicographicalorder(thedefinitionoflexicographicalcomparisoningiveninnotessection).
Therulethatchangeshieroglyphsisthefollowing.Oneclockwiserotationoftheroundswitchreplaceseachhieroglyphwiththenexthieroglyphinalphabet,i.e.hieroglyphx(1 ≤ x ≤ c - 1)isreplacedwithhieroglyph(x + 1),andhieroglyphcisreplacedwithhieroglyph1.
Helparcheologistdetermine,howmanyclockwiserotationstheyshouldperforminordertoopenthedoor,ordeterminethatthisisimpossible,i.e.nocyclicshiftofthealphabetwillmakethesequenceofwordssortedlexicographically.
Input
Thefirstlineoftheinputcontainstwointegersnandc(2 ≤ n ≤ 500 000,1 ≤ c ≤ 106)—thenumberofwords,writtenonthelock,andthenumberofdifferenthieroglyphs.
Eachofthefollowingnlinescontainsthedescriptionofoneword.Thei-thoftheselinesstartswithintegerli(1 ≤ li ≤ 500 000),thatdenotesthelengthofthei-thword,followedbyliintegerswi, 1,wi, 2,...,wi, li(1 ≤ wi, j ≤ c)—theindicesofhieroglyphsthatmakeupthei-thword.Hieroglyphwithindex1isthesmallestinthealphabetandwithindexc—thebiggest.
It'sguaranteed,thatthetotallengthofallwordsdoesn'texceed106.
Output
Ifitispossibletoopenthedoorbyrotatingtheroundswitch,printintegerx(0 ≤ x ≤ c - 1)thatdefinestherequirednumberofclockwiserotations.Ifthereareseveralvalidx,printanyofthem.
Ifitisimpossibletoopenthedoorbythismethod,print - 1.
Examples
input
43 232 11 3231 42312
output
1
input
25 242 242
output
0
input
44 12 13 14 12
output
-1 题意:n个单词,c个字母,每一次操作都会使所有单词的所有字母变为它字典序的后一个字母。当所有单词按字典序从小到大排列时, 完成任务,问需要多少此操作。 思路:每两个单词成为字典序,都有一个操作次数的区间,一次枚举相邻的两个单词,求得n-1个区间,在这n-1个区间的交集中的数便满足要求。 前缀和:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> usingnamespacestd; #defineN500005 #defineC1000005 vector<int>v ; intseg[C]; intmain() { intn,c; scanf("%d%d",&n,&c); for(inti=0;i<n;i++) { intnn; scanf("%d",&nn); for(intj=0;j<nn;j++) { intnum; scanf("%d",&num); v[i].push_back(num); } } intok=1; for(inti=0;i<n-1;i++) { intp=0; while(1) { if(p==v[i].size()) { seg[0]+=1; seg[c+1]-=1; break; } elseif(p==v[i+1].size()) { ok=0; break; } elseif(v[i+1][p]!=v[i][p]) { if(v[i+1][p]<v[i][p]) { seg[c-v[i+1][p]+1]-=1; seg[c-v[i][p]+1]+=1; } elseif(v[i+1][p]>v[i][p]) { seg[0]+=1; seg[c-v[i+1][p]+1]-=1; seg[c-v[i][p]+1]+=1; seg[c]-=1; } break; } p++; } } if(!ok) printf("-1\n"); else { intpre=0,res=-1; for(inti=0;i<=c;i++) { pre+=seg[i]; //cout<<pre<<endl; if(pre==n-1) { res=i; break; } } printf("%d\n",res); } return0; }
树状数组:
#include<iostream> #include<cstdio> #include<cstring> #include<vector> usingnamespacestd; #defineN500005 #defineC1000005 vector<int>v ; intn,c,s[C]; intlowbit(intx) { returnx&(-x); } voidadd(intk,intx) { for(inti=k;i<=c;i+=lowbit(i)) s[i]+=x; } intSum(intk) { intres=0; for(inti=k;i>0;i-=lowbit(i)) res+=s[i]; returnres; } intmain() { while(scanf("%d%d",&n,&c)!=EOF) { memset(s,0,sizeof(s)); for(inti=0;i<=n;i++) v[i].clear(); for(inti=0;i<n;i++) { intnn; scanf("%d",&nn); for(intj=0;j<nn;j++) { intnum; scanf("%d",&num); v[i].push_back(num); } } intok=1; for(inti=0;i<n-1;i++) { intp=0; while(1) { if(p==v[i].size()) { add(1,1); break; } elseif(p==v[i+1].size()) { ok=-1; break; } elseif(p<v[i+1].size()&&p>=v[i].size()) { add(1,1); break; } elseif(v[i][p]!=v[i+1][p]) { if(v[i][p]>v[i+1][p]) { add(c-v[i][p]+2,1); add(c-v[i+1][p]+2,-1); } elseif(v[i][p]<v[i+1][p]) { add(1,1); add(c-v[i+1][p]+2,-1); add(c-v[i][p]+2,1); add(c+1,-1); } break; } p++; } } if(!ok) printf("-1\n"); else { intres=-1; for(inti=1;i<=c+1;i++) if(Sum(i)==n-1) { res=i-1; break; } printf("%d\n",res); } } return0; }
相关文章推荐
- Codeforces 731D 求所有区间的交(树状数组或前缀和)
- [CodeForces 677D] Vanya and Treasure (二维树状数组+静态前缀最小值)
- CodeForces 873B Balanced Substring(前缀和+思维)
- codeforces 776C - Molly's Chemicals 前缀和
- codeforces 234C C. Weather(枚举+前缀后缀预处理)
- Little Elephant and Array - CodeForces 220 B 树状数组
- [Codeforces 341D] Iahub and Xors (二维树状数组)
- Codeforces 546D Soldier and Number Game【分解素因子+前缀和】
- Codeforces 677E Vanya and Balloons【断点前缀和+暴力维护+数学技巧】
- Hdu 5700 区间交【二分+树状数组+前缀和】
- Codeforces 106 D. Treasure Island(前缀和预处理)
- 【codeforces 731D】【差分+线段扫描 思维题】80-th Level Archeology【给你n个word,现在要使得word按字典序排列,随便操作多少次,每次可以使每个word+1】
- Codeforces 851D - Arpa and a list of numbers 【素数+前缀和】
- Codeforces 853B Round #433 Div2D& Div1B Jury Meeting:差分前缀和+模拟
- CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)
- Codeforces 106D Treasure Island【思维+二维前缀和】
- codeforces 629D-Babaei and Birthday Cake(dp && 线段树或树状数组离散优化)
- Codeforces 703D 树状数组
- Codeforces 165C Another Problem On Strings 尺取 Or 前缀和
- 【Codeforces 776 C Molly's Chemicals】+ 前缀和 + map