您的位置:首页 > 其它

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
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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: