成功掉到灰,真的心太累了,orz!!!!,不是很懂那些国外大佬为什么每次都是20多分钟AK的,QAQ
1 second
256 megabytes
standard input
standard output
Petya has an array aa consisting of nn integers. He wants to remove duplicate (equal) elements.
Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed.
The first line contains a single integer nn (1≤n≤501≤n≤50) — the number of elements in Petya's array.
The following line contains a sequence a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — the Petya's array.
In the first line print integer xx — the number of elements which will be left in Petya's array after he removed the duplicates.
In the second line print xx integers separated with a space — Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left.
6
1 5 5 1 6 1
3
5 6 1
5
2 4 2 4 4
2
2 4
5
6 6 6 6 6
1
6
In the first example you should remove two integers 11, which are in the positions 11 and 44. Also you should remove the integer 55, which is in the position 22.
In the second example you should remove integer 22, which is in the position 11, and two integers 44, which are in the positions 22 and 44.
In the third example you should remove four integers 66, which are in the positions 11, 22, 33 and 44.
分析:从右向左提取元素,不能重复,set保留元素个数,哈希映射数组b[]保留元素
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB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gGDGD
7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FF(i, a, b) for(int i = a; i < b; i++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 1001
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie();
int i, a[MAX], n, b[MAX], len, ascll[MAX];set<int> s; cin>>n;ME(ascll, );len = ;
for(i = ; i < n; i++){
cin>>a[i];
ascll[a[i]] = ;
s.insert(a[i]);
}
cout<<s.size()<<endl;
for(i = n - ; i >= ; i--){
if(ascll[a[i]]){
ascll[a[i]] = ;
b[len++] = a[i];
}
}
for(i = len - ; i >= ; i--) printf("%d ", b[i]);
puts("");
return EXIT_SUCCESS;
}
1 second
256 megabytes
standard input
standard output
You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.
Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx".
You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by 11. For example, if you delete the character in the position 22 from the string "exxxii", then the resulting string is "exxii".
The first line contains integer nn (3≤n≤100)(3≤n≤100) — the length of the file name.
The second line contains a string of length nn consisting of lowercase Latin letters only — the file name.
Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0.
6
xxxiii
1
5
xxoxx
0
10
xxxxxxxxxx
8
分析:这题就是找到字符串中有多少个‘xxx’,比赛的时候用错string.find()函数了,这里补起来 m = str.find(str1, 0);
m = str.find(str1, m + 1);
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;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SJDQ7 .iEBL:. . .,:,:;SRQGZXPXXSHaS2UUSUSswQB6OZgG6pZZGODEOGDE
JgUri1aGEpEpXSS5LBQ, .rLap2Jr ;712OO6ZggRDZXHXpKK2211LLLc7wgBa2PODRGE6OZgDgGDGD
7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FF(i, a, b) for(int i = a; i < b; i++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 1001
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie();
int i, n, m, ans;string str, str1 = "xxx"; cin>>n>>str;ans = ;m = str.find(str1, );
if(m == string::npos) cout<<<<endl;
else{
ans++;
m = str.find(str1, m + );
while(m!= string::npos){
ans++;
m = str.find(str1, m + );
}
cout<<ans<<endl;
} return EXIT_SUCCESS;
}
4 seconds
256 megabytes
standard input
standard output
There are nn dormitories in Berland State University, they are numbered with integers from 11 to nn. Each dormitory consists of rooms, there are aiai rooms in ii-th dormitory. The rooms in ii-th dormitory are numbered from 11 to aiai.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all nn dormitories is written on an envelope. In this case, assume that all the rooms are numbered from 11 to a1+a2+⋯+ana1+a2+⋯+an and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case n=2n=2, a1=3a1=3 and a2=5a2=5 an envelope can have any integer from 11 to 88 written on it. If the number 77 is written on an envelope, it means that the letter should be delivered to the room number 44 of the second dormitory.
For each of mm letters by the room number among all nn dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
The first line contains two integers nn and mm (1≤n,m≤2⋅105)(1≤n,m≤2⋅105) — the number of dormitories and the number of letters.
The second line contains a sequence a1,a2,…,ana1,a2,…,an (1≤ai≤1010)(1≤ai≤1010), where aiai equals to the number of rooms in the ii-th dormitory. The third line contains a sequence b1,b2,…,bmb1,b2,…,bm (1≤bj≤a1+a2+⋯+an)(1≤bj≤a1+a2+⋯+an), where bjbj equals to the room number (among all rooms of all dormitories) for the jj-th letter. All bjbj are given in increasing order.
Print mm lines. For each letter print two integers ff and kk — the dormitory number ff (1≤f≤n)(1≤f≤n) and the room number kk in this dormitory (1≤k≤af)(1≤k≤af) to deliver the letter.
3 6
10 15 12
1 9 12 23 26 37
1 1
1 9
2 2
2 13
3 1
3 12
2 3
5 10000000000
5 6 9999999999
1 5
2 1
2 9999999994
In the first example letters should be delivered in the following order:
- the first letter in room 11 of the first dormitory
- the second letter in room 99 of the first dormitory
- the third letter in room 22 of the second dormitory
- the fourth letter in room 1313 of the second dormitory
- the fifth letter in room 11 of the third dormitory
- the sixth letter in room 1212 of the third dormitory
分析:CF理解题意是真的难,QAQ,第3行数组在第一行中能排第几个(就是比前面所有的和要大),一开始直接for循环,wa了,又用二分试了下,
但是二分写错了,QAQ,结果没搞出来,然后又复习了下lower_bound函数.
正常人的做法(多种方法):
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO
a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP
1i :SZJrLXpBBMRB: .v Ba,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OsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FF(i, a, b) for(int i = a; i < b; i++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 200005
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int main(void){
// #ifdef LOCAL
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
// ios::sync_with_stdio(false); cin.tie(0);
LL i, j, n, m, a, b, f[MAX], left, right, mid, ans; cin>>n>>m;ME(f, );
for(i = ; i <= n; i++) {cin>>a;f[i] = f[i - ] + a;}
for(i = ; i <= m; i++){
cin>>b;
left = ;right = n;
while(left < right){
mid = (left + right) >> ;
if(b > f[mid]) left = mid + ;
elseright = mid;
}
cout<<left<<" "<<b - f[left - ]<<endl;
}
return EXIT_SUCCESS;
}
大佬的做法(QAQ):
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .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:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO
a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP
1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6
:K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ
w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,. .;;:;7: . . ,... rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD
H: ;XJ;77rv7rJUaQ;rgaPgXXHBB; . . .ri:;; ,: . :6QZEKHXpXXL:,::r1PKXaa2XSS5a5S5UJ22aOBgEpOppZOOgG
iwsJsr7rsLrrLcJ2gQGXHwa1SUHBB: . ... . .., : 1BRKpOEGp2Jr;::rsXP1Uaa2U5a5aUa5SsSpgQRZGZEEZZOOgDg
,cs5aXaP552ssLwRSHXaSS5asXBB. . . . LBBgGgZHsr;vrr;rcXpP5UUS5X5S2XS5SS1URBggpEZgDDGOGggRD
,;irrs2KgQRPJJJSUXKXSHJpBM . . . ZBBOUsr;:. ,,,rHgZPUaSaUSUSSXaUPXsSBB6ZZp6EEGOOGDEggO
;BBQQZaSJ5J15SJDQ7 .iEBL:. . .,:,:;SRQGZXPXXSHaS2UUSUSswQB6OZgG6pZZGODEOGDE
JgUri1aGEpEpXSS5LBQ, .rLap2Jr ;712OO6ZggRDZXHXpKK2211LLLc7wgBa2PODRGE6OZgDgGDGD
7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FF(i, a, b) for(int i = a; i < b; i++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 200005
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int main(void){
// #ifdef LOCAL
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
// ios::sync_with_stdio(false); cin.tie(0);
LL i, j, n, m, a, f[MAX], b, ans, index; cin>>n>>m;ME(f, );
for(i = ; i <= n; i++){cin>>a;f[i] = a + f[i - ];}
for(i = ; i <= m; i++){
cin>>b;
index = lower_bound(f, f + n + , b) - f;
cout<<index<<" "<<b - f[index - ]<<endl;
}
return EXIT_SUCCESS;
}
1 second
256 megabytes
standard input
standard output
Polycarp likes arithmetic progressions. A sequence [a1,a2,…,an][a1,a2,…,an] is called an arithmetic progression if for each ii (1≤i<n1≤i<n) the value ai+1−aiai+1−ai is the same. For example, the sequences [42][42], [5,5,5][5,5,5], [2,11,20,29][2,11,20,29] and [3,2,1,0][3,2,1,0] are arithmetic progressions, but [1,0,1][1,0,1], [1,3,9][1,3,9] and [2,3,1][2,3,1] are not.
It follows from the definition that any sequence of length one or two is an arithmetic progression.
Polycarp found some sequence of positive integers [b1,b2,…,bn][b1,b2,…,bn]. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by 11, an element can be increased by 11, an element can be left unchanged.
Determine a minimum possible number of elements in bb which can be changed (by exactly one), so that the sequence bbbecomes an arithmetic progression, or report that it is impossible.
It is possible that the resulting sequence contains element equals 00.
The first line contains a single integer nn (1≤n≤100000)(1≤n≤100000) — the number of elements in bb.
The second line contains a sequence b1,b2,…,bnb1,b2,…,bn (1≤bi≤109)(1≤bi≤109).
If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).
4
24 21 14 10
3
2
500 500
0
3
14 5 1
-1
5
1 3 6 9 12
1
In the first example Polycarp should increase the first number on 11, decrease the second number on 11, increase the third number on 11, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to [25,20,15,10][25,20,15,10], which is an arithmetic progression.
In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.
In the third example it is impossible to make an arithmetic progression.
In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like [0,3,6,9,12][0,3,6,9,12], which is an arithmetic progression.
分析:这题天秀,就是对一个数组内所有元素+1或-1操作(每个元素只进行一次),最后能不能形成等差数列,二维i,j,在-1,0,1内循环然后找每一节的差值。
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZ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JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FFI(a, b) for(int i = a; i < b; i++)
#define FFJ(a, b) for(int j = a; j < b; j++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 100005
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int n, a[MAX]; int Solve(int a0, int a1){
int b = a1 - a0, res = ;
for(int i = , ai = a0; i < n; i++, ai += b){
int add = abs(a[i] - ai);
if(add == ) res++;
else if(add > ) return n + ;
}
return res;
} int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie();
int i, j, ans;cin>>n; for(i = ; i < n; i++) cin>>a[i];
ans = n + ;
FFI(-, ) FFJ(-, ) ans = min(ans, Solve(a[] + i, a[] + j));
if(ans == n + )cout<<-; //变不过去
else cout<<ans; return EXIT_SUCCESS;
}
1 second
256 megabytes
standard input
standard output
The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.
If xx is the number of passengers in a bus just before the current bus stop and yy is the number of passengers in the bus just after current bus stop, the system records the number y−xy−x. So the system records show how number of passengers changed.
The test run was made for single bus and nn bus stops. Thus, the system recorded the sequence of integers a1,a2,…,ana1,a2,…,an(exactly one number for each bus stop), where aiai is the record for the bus stop ii. The bus stops are numbered from 11 to nn in chronological order.
Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww (that is, at any time in the bus there should be from 00 to ww passengers inclusive).
The first line contains two integers nn and ww (1≤n≤1000,1≤w≤109)(1≤n≤1000,1≤w≤109) — the number of bus stops and the capacity of the bus.
The second line contains a sequence a1,a2,…,ana1,a2,…,an (−106≤ai≤106)(−106≤ai≤106), where aiai equals to the number, which has been recorded by the video system after the ii-th bus stop.
Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.
3 5
2 1 -3
3
2 4
-1 1
4
4 10
2 4 1 2
2
In the first example initially in the bus could be 00, 11 or 22 passengers.
In the second example initially in the bus could be 11, 22, 33 or 44 passengers.
In the third example initially in the bus could be 00 or 11 passenger.
分析:一辆车能容量ww个人,开始可能有i个人(你要求有几种情况),然后每次给的值就是到站前后的人数的差值,板子题
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO
a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP
1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6
:K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ
w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,. .;;:;7: . . ,... rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD
H: ;XJ;77rv7rJUaQ;rgaPgXXHBB; . . .ri:;; ,: . :6QZEKHXpXXL:,::r1PKXaa2XSS5a5S5UJ22aOBgEpOppZOOgG
iwsJsr7rsLrrLcJ2gQGXHwa1SUHBB: . ... . .., : 1BRKpOEGp2Jr;::rsXP1Uaa2U5a5aUa5SsSpgQRZGZEEZZOOgDg
,cs5aXaP552ssLwRSHXaSS5asXBB. . . . LBBgGgZHsr;vrr;rcXpP5UUS5X5S2XS5SS1URBggpEZgDDGOGggRD
,;irrs2KgQRPJJJSUXKXSHJpBM . . . ZBBOUsr;:. ,,,rHgZPUaSaUSUSSXaUPXsSBB6ZZp6EEGOOGDEggO
;BBQQZaSJ5J15SJDQ7 .iEBL:. . .,:,:;SRQGZXPXXSHaS2UUSUSswQB6OZgG6pZZGODEOGDE
JgUri1aGEpEpXSS5LBQ, .rLap2Jr ;712OO6ZggRDZXHXpKK2211LLLc7wgBa2PODRGE6OZgDgGDGD
7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FFI(a, b) for(int i = a; i < b; i++)
#define FFJ(a, b) for(int j = a; j < b; j++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 1001
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie();
int n, m;LL a, l, r, ma, mi, sum;
cin>>n>>m;ma = -INF, mi = INF;sum = l = ;r = m;
FFI(, n + ){
cin>>a;
sum += a;
ma = max(ma, sum);
mi = min(mi, sum);
}
if(mi <= ) l -= mi;
if(ma >= ) r -= ma;
if(r - l < ) sum = ;
else sum = r - l + ;
cout<<sum<<endl; return EXIT_SUCCESS;
}