You are on page 1of 17

Archives of the Thi lp trinh tru+.

c tuye^n PTIT
main problemset

Editors:
Thch code nhu+ng dt

Last updated: 2013-01-18 11:12:42

Preface
This electronic material contains a set of algorithmic problems, forming the archives of the Thi lp trinh tru+.c tuye^n PTIT (http://www.spoj.com/ACMPTIT/), main problemset. The document can be accessed at the following URLs: in PostScript format: http://www.spoj.com/ACMPTIT/problems/main.ps in Portable Document Format: http://www.spoj.com/ACMPTIT/problems/main.pdf These resources are constantly updated to synchronise with the ever-changing hypertext version of the problems, and to include newly added problems. If you have obtained this document from another source, it is strongly recommended that you should download the current version from one of the aforementioned URLs.

Enjoy problem-solving at the Thi lp trinh tru+.c tuye^n PTIT!

Disclaimer from the Editors. Despite our best efforts, it is possible that this document contains errors or that some of the content differs slightly from its original hypertext form. We take no responsibility for any such faults and their consequences. We neither authorise nor approve use of this material for any purpose other than facilitating problem solving at the Sphere Online Judge site; nor do we guarantee its fitness for any purpose whatsoever. The layout of the problems in this document is the copyright of the Editors named on the cover (as determined by the appropriate footers in the problem description). The content is the copyright of the respective Editor unless the copyright holder is otherwise stated in the resource section. The document as a whole is not protected by copyright, and fragments of it are to be regarded independently. No responsibility is taken by the Editors if use or redistribution of this document violates either their or third party copyright laws. When referring to or citing the whole or a fragment of this document, please state clearly the aforementioned URLs at which the document is to be found, as well as the resources from which the problems you are referring to originally came. Remarks concerning this document should be sent to the following e-mail address: contact@spoj.com.

Table of Contents
1. 2. 3. 4. 5. 6. 7. 8. 9. Problem REPSTR (13217. Replacing Digits) Problem FINDCOW (13239. Find the Cow!) Problem STRBALAN (13240. Chui cn bng) Problem GOODFRIE (13265. Good friends) Problem REMOVBIT (13266. Xa bit) Problem SUMSQR1 (13313. Tng binh phu+o+ng) Problem PRISUSTR (13379. Prime Substring) Problem YOUNGCLA (13380. Birthdates) Problem NUMPOLY (13400. Giao ie^?m trong a gic)

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13217. Replacing Digits


Problem code: REPSTR
Cho s nguyen du+o+ng a c N chu+~ s va s day s c M chu+~ s. Chu+~ s o+? vi. tr j (1<=j<=M) cu?a day s c the^? cho.n bt ki vi. tr i (1<=i<=N) trong s a va thay the^ bng s j . Mi chu+~ s cu?a day s chi? u+o+.c thay the^ khng qu mt ln. Nhie^.m vu. cu?a ba.n la hay tim cch thay sao cho s a a.t gi tri. lo+n nht. Ba.n c the^? khng cn su+? du.ng tt ca? cc chu+~ s trong s.

Input
Dong u chu+a s nguyen du+o+ng a c dai N (khng bt u bng chu+~ s 0). Dong 2 chu+a day s c dai M (1<=N,M<=10 5 )

Output
S a lo+n nht c the^? thay the^ u+o+.c.

Example
Input: 1024 010 Output: 1124 Input: 987 1234567 Output: 987

Added by: Thch code nhu+ng dt Date: 2013-01-10 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13239. Find the Cow!


Problem code: FINDCOW
C bo Bessie a trn thot va ang trn o+? mt i ni vo+i nhu+~ng ng co? cao. Nng dn John (FJ), ngu+o+i ang mun tim kie^m Bessie, a quye^t i.nh bo tren ng co? bng tay va du gi e^? tim ra du ve^t cu?a Bessie. Khng may thay, ng ta c vn e^ vo+i vie^.c tim kie^m Bessie tu+ vi. tr thun lo+.i nay. Day co? o+? tru+o+c mt FJ trng nhu+ mt chui ngoc o+n c dai N (1 <= N <= 50,000); v du.: )((()())()) FJ bie^t rng Bessie chn sau cu?a Bessie ging nhu+ mt cp du mo+? ngoc o+n ((, va chn tru+o+c cu?a c ta ging nhu+ mt cp du ng ngoc o+n. Vi. tr cu?a Bessie c the^? u+o+.c die^~n ta? bo+?i mo.t cp x < y , trong (( u+o+.c tim o+? vi. tr x, va )) u+o+.c tim o+? vi. tr y. Hay e^m c bao nhieu vi. tr ma Bessie c the^? ang u+ng.

Input
Dong 1: Mt chui ngoc o+n c dai la N (1 <= N <= 50,000).

Output
Dong 1: S vi. tr ma Bessie c the^? u+ng - C nghia la s cp (x,y) khc nhau vo+i x < y sao cho (( u+o+.c tim thy o+? x va )) u+o+.c tim thy o+? y.

Example
Input: )((()())()) Output: 4

C 4 vi. tr c the^? cu?a Bessie u+o+.c the^? hie^.n o+? du+o+i1. )((()())())^^ ^^2. )((()())()^^^^3. )((()())()^^ ^^4. )((()()) ())^^^^

C 4 vi. tr c the^? cu?a Bessie u+o+.c the^? hie^.n o+? du+o+i 1. )((()())()) ^^^^ 2. )((()())()) ^^^^ 3. )((()())()) ^^^^ 4. )((()())()) ^^^ ^

Added by: Thch code nhu+ng dt Date: 2013-01-11 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13240. Chui cn bng


Problem code: STRBALAN
Bessie vu+a mo+i mua mt my tnh xch tay mo+i, nhu+ng c ta la.i khng go ban phm u+o+.c cho tt lm vi mng chn lo+n cu?a c ta so vo+i kch thu+o+c nho? cu?a ban phm. Bessie vu+a nhp vao ban phm mt day k tu+. u+a thch cu?a c ta - mt chui ngoc cn bng. Tuy nhien, c ta nhn ra rng c ta c le~ a go nhm mt k tu+. mt cch v tinh, tu+ du mo+? ngoc o+n thanh du ng ngoc o+n, hoc ngu+o+.c la.i. Hay gip Bessie e^m s vi. tr trong chui e^? ne^u ta i ngu+o+.c du o+? vi. tr thi chui ban u se~ thanh chui cn bng. C nhie^u cch e^? i.nh nghia mt chui ngoc la "cn bng." Cch de^~ nht la s lu+o+.ng du mo+? ngoc ( bng s lu+o+.ng du ng ngoc ), va vo+i bt ki chui tie^n t nao, s lu+o+.ng du mo+? ngoc o+n ( pha?i lo+n ho+n hoc bng s lu+o+.ng du ng ngoc o+n ). Trong nhu+~ng v du. sau y, nhu+~ng chui o+? ben du+o+i la chui cn bng: () (()) ()(()()) Nhu+~ng chui sau y la chui khng cn bng: )( ()) ( ((()) ))

Bessie vu+a mo+i mua mt my tnh xch tay mo+i, nhu+ng c ta la.i khng go ban phm u+o+.c cho tt lm vi mng chn lo+n cu?a c ta so vo+i kch thu+o+c nho? cu?a ban phm. Bessie vu+a nhp vao ban phm mt day k tu+. u+a thch cu?a c ta - mt chui ngoc cn bng. Tuy nhien, c ta nhn ra rng c ta c le~ a go nhm mt k tu+. mt cch v tinh, tu+ du mo+? ngoc o+n thanh du ng ngoc o+n, hoc ngu+o+.c la.i. Hay gip Bessie e^m s vi. tr trong chui e^? ne^u ta i ngu+o+.c du o+? vi. tr thi chui ban u se~ thanh chui cn bng. C nhie^u cch e^? i.nh nghia mt chui ngoc la "cn bng." Cch de^~ nht la s lu+o+.ng du mo+? ngoc ( bng s lu+o+.ng du ng ngoc ), va vo+i bt ki chui tie^n t nao, s lu+o+.ng du mo+? ngoc o+n ( pha?i lo+n ho+n hoc bng s lu+o+.ng du ng ngoc o+n ). Trong nhu+~ng v du. sau y, nhu+~ng chui o+? ben du+o+i la chui cn bng: () (())

()(()()) Nhu+~ng chui sau y la chui khng cn bng: )( ()) ( ((()) ))

Input
Dong 1: Mt chui ngoc o+n c dai la N (1 <= N <= 100,000).

Output
Dong 1: S vi. tr trong chui ban u ma ne^u ta i ngu+o+.c du o+? vi. tr thi chui se~ thanh chui cn bng.

Example
Input: ()(()))) Output: 4
Ne^u ta nhin cu. the^? vao chui ban u: 12345678 ()(()))) Ne^u ta i ngu+o+.c du cu?a k tu+. thu+ 2 thi chui se~ thanh chui cn bng: 12345678 (((()))) Tu+o+ng tu+. ne^u ta i o+? vi. tr thu+ 5,6, va 7, ta e^u c ke^t qua? la chui cn bng.

Added by: Thch code nhu+ng dt Date: 2013-01-11 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13265. Good friends


Problem code: GOODFRIE
Trong mt lo+p ho.c, c gio xe^p ha.ng N ho.c sinh theo thu+ tu+. ie^?m s tu+ cao xung thp. Hai ho.c sinh se~ la ba.n ne^u thu+ tu+. cu?a ho. la gn nhau, tu+c la khc bie^.t giu+~a thu+ tu+. khng qu K. V du.: ne^u K = 1, thi chi? c 2 ho.c sinh o+? tru+o+c va sau danh sch la ba.n cu?a 1 ho.c sinh. Them nu+~a, hai ho.c sinh go.i la ba.n tt ne^u ho. la ba.n va ten cu?a ho. c cung dai. Vie^t chu+o+ng trinh tnh s cc cp ba.n tt trong lo+p.

Input
Dong u chu+ N (3<=N<=300 000) va K (1<= K <=N). N dong tie^p theo, mi dong chu+a ten mt ho.c sinh theo danh sch xe^p ha.ng (tu+ 2 e^n 20 chu+~ ci tie^ng Anh in hoa).

Output
S cp ba.n tt.

Example
Input: 4 2IVAIVOATOM4 2 IVA IVO ANA TOM Output: 5 Input: 6 3 CYNTHIA LLOYD STEVIE KEVIN MALCOLM DABNEY Output: 2

Added by: Thch code nhu+ng dt Date: 2013-01-12 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13266. Xa bit
Problem code: REMOVBIT
Cho s a vie^t o+? he^. co+ s 2. Nhie^.m vu. cu?a ba.n la pha?i xa mt chu+~ s e^? s con la.i xa khi xa la lo+n nht !!!

Input
Mt dong chu+a s a vie^t o+? he^. nhi. phn.

Output
S lo+n nht sau khi xa 1 chu+~ s tu+ a.

Example
Input: 101 Output: 11

Added by: Thch code nhu+ng dt Date: 2013-01-12 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13313. Tng binh phu+o+ng


Problem code: SUMSQR1
Cho day a gm c N phn tu+? la hon vi. cc s tu+ 1 e^n N. Tnh tng binh phu+o+ng cc s trong day A.

Input
Gm mt s N duy nht (1<=N<=10 9 )

Output
Tng binh phu+o+ng cc s trong day a.

Example
Input: 2 Output: 5

Added by: Thch code nhu+ng dt Date: 2013-01-14 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13379. Prime Substring


Problem code: PRISUSTR
Cho mt xu cc chu+~ s, nhie^.m vu. cu?a ba.n la tim s nguyen t lo+n nht (gm cc chu+~ s lien tie^p nhau) xut hie^.n trong xu. Cc s nguyen t trong oa.n 2 e^n 100 000.

Input
Mi dong chu+a 1 xu (ti a 255 s). Ke^t thc bo+?i dong chu+a s 0 (khng cn xu+? l)

Output
Vo+i mi xu, in ra tren 1 dong ke^t qua?

Example
Input: 112491321150441226406011245 91321150448 1226406 0 Output: 11 1321 2

Added by: Thch code nhu+ng dt Date: 2013-01-17 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13380. Birthdates
Problem code: YOUNGCLA
Vie^t chu+o+ng trinh tim ngu+o+i tre? nht va gia nht trong lo+p.

Input
Dong 1 chu+a s n (1<=n<=100), s ngu+o+i trong lo+p. N dong sau, mi dong la thng tin 1 ngu+o+i c da.ng: personName dd mm yyyy Trong : personName la ten khng qu 15 chu+~ ci, dd,mm,yyyy ln lu+o+.t la ngay, thng, va nm sinh.

Output
Dong 1: ten ngu+o+i tre? nht Dong 2: ten ngu+o+i gia nht

Example
Input: Garfield Mickey 1 Alice 30 Tom 15 8 Jerry 18 Garfield Output: Tom Jerry 20 9 19905 10 1991 12 1990 1993 9 1990 20 9 1990

Added by: Thch code nhu+ng dt Date: 2013-01-17 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA

(function() { var po = document.createElement(script); po.type = text/javascript; po.async = true; po.src = https://apis.google.com/js/plusone.js; var s = document.getElementsByTagName(script)[0]; s.parentNode.insertBefore(po, s); })();

SPOJ Problem Set (main)

13400. Giao ie^?m trong a gic


Problem code: NUMPOLY
Cho tru+o+c mt a gic li N ca.nh, tho?a man khng c 3 u+o+ng cho nao ng quy. Hay e^m s giao ie^?m ta.o bo+?i cc cp u+o+ng cho trong a gic. Hinh ve~ minh ho.a vo+i lu.c gic:

Input
Chu+a 1 s nguyen N (3 <= N <=100) duy nht la s i?nh cu?a a gic.

Output
S giao ie^?m ta.o bo+?i cc u+o+ng cho nm trong a gic.

Example

Input13outputInput24outputInput36output15

Added by: Thch code nhu+ng dt Date: 2013-01-18 Time limit: 1s Source limit:50000B Cluster: Cube (Intel Pentium G860 3GHz) Languages: C++ 4.3.2 C++ 4.0.0-8 JAVA