您的位置:首页 > 其它

A. Union of Doubly Linked Lists

2017-09-24 11:40 218 查看
记录进步历程20170924
A. Union of Doubly Linked Liststime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDoublylinked list is one of the fundamental data structures. A doubly linked list isa sequence of elements, each containing information about the previous and thenext elements of the list. In this problem all lists have linear structure.I.e. each element except the first has exactly one previous element, eachelement except the last has exactly one next element. The list is not closed ina cycle.In thisproblem you are given n memorycells forming one or more doubly linked lists. Each cell contains informationabout element from some list. Memory cells are numbered from 1 to n.For eachcell i youare given two values:·        li — cellcontaining previous element for the element in the cell i;·        ri — cellcontaining next element for the element in the cell i.Ifcell i containsinformation about the element which has no previous element then li = 0. Similarly,if cell i containsinformation about the element which has no next element then ri = 0.Three lists are shown on the picture.For example,for the picture above the values of l and r arethe following: l1 = 4, r1 = 7; l2 = 5, r2 = 0; l3 = 0, r3 = 0; l4 = 6, r4 = 1; l5 = 0, r5 = 2; l6 = 0, r6 = 4; l7 = 1, r7 = 0.Your task isto unite all given lists in a single list, joining them to each other in anyorder. In particular, if the input data already contains a single list, thenthere is no need to perform any actions. Print the resulting list in the formof values li, ri.Any otheraction, other than joining the beginning of one list to the end of another, cannot be performed.InputThe firstline contains a single integer n (1 ≤ n ≤ 100) — thenumber of memory cells where the doubly linked lists are located.Each of thefollowing n linescontains two integers li, ri (0 ≤ li, ri ≤ n) — thecells of the previous and the next element of list for cell i.Value li = 0 ifelement in cell i has noprevious element in its list. Value ri = 0 ifelement in cell i has nonext element in its list.It isguaranteed that the input contains the correct description of a single or moredoubly linked lists. All lists have linear structure: each element of listexcept the first has exactly one previous element; each element of list exceptthe last has exactly one next element. Each memory cell contains informationabout one element from some list, each element of each list written in oneof n given cells.OutputPrint n lines,the i-th linemust contain two integers li and ri — thecells of the previous and the next element of list for cell i afterall lists from the input are united in a single list. If there are manysolutions print any of them.Exampleinput74 75 00 06 10 20 41 0output4 75 60 56 13 22 41 0        A. Quasi-palindrometime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet quasi-palindromic numberbe such number that adding some leading zeros (possible none) to it produces apalindromic string.String t iscalled a palindrome, if it reads the same from left to right and from right toleft.For example,numbers 131 and 2010200 are quasi-palindromic,they can be transformed to strings "131" and"002010200", respectively, which arepalindromes.You aregiven some integer number x. Check ifit's a quasi-palindromic number.InputThe firstline contains one integer number x (1 ≤ x ≤ 109). This numberis given without any leading zeroes.OutputPrint "YES" ifnumber x is quasi-palindromic.Otherwise, print "NO" (without quotes).Examplesinput131outputYESinput320outputNOinput2010200outputYES    D. YetAnother Array Queries Problemtime limitper test2 secondsmemory limitper test256megabytesinputstandardinputoutputstandardoutputYou aregiven an array a ofsize n, and q queriesto it. There are queries of two types:·      1 li ri —perform a cyclic shift of the segment [li, ri] to theright. That is, for every x suchthat li ≤ x < ri newvalue of ax + 1 becomesequal to old value of ax, and newvalue of ali becomesequal to old value of ari;·      2 li ri —reverse the segment [li, ri].Thereare m importantindices in the array b1, b2, ..., bm. Foreach i suchthat 1 ≤ i ≤ m youhave to output the number that will have index bi in thearray after all queries are performed.InputThe firstline contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·105, 1 ≤ m ≤ 100).The secondline contains n integernumbers a1, a2, ..., an (1 ≤ ai ≤ 109).Then q linesfollow. i-th of themcontains three integer numbers ti, li, ri,where ti is thetype of i-th query,and [li, ri] is thesegment where this query is performed (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).The lastline contains m integernumbers b1, b2, ..., bm (1 ≤ bi ≤ n) —important indices ofd52ethe array.OutputPrint m numbers, i-th of whichis equal to the number at index bi afterall queries are done.Exampleinput6 3 51 2 3 4 5 62 1 32 3 61 1 62 2 1 5 3output3 3 1 5 2    G. Graphic Settingstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecentlyIvan bought a new computer. Excited, he unpacked it and installed his favouritegame. With his old computer Ivan had to choose the worst possible graphicsettings (because otherwise the framerate would be really low), but now hewants to check, maybe his new computer can perform well even with the bestpossible graphics?Thereare m graphicsparameters in the game. i-thparameter can be set to any positive integer from 1 to ai, andinitially is set to bi(bi ≤ ai). So thereare  different combinations ofparameters. Ivan can increase or decrease any of these parameters by 1; after thatthe game will be restarted with new parameters (and Ivan will have theopportunity to check chosen combination of parameters).Ivan wantsto try all p possiblecombinations. Also he wants to return to the initial settings after trying allcombinations, because he thinks that initial settings can be somehow bestsuited for his hardware. But Ivan doesn't really want to make a lot ofrestarts.So he wantsyou to tell the following:·        If there exists a way to make exactly p changes(each change either decreases or increases some parameter by 1) to try allpossible combinations and return to initial combination, then Ivan wants toknow this way.·        Otherwise, if there exists a way to make exactly p - 1 changesto try all possible combinations (including the initial one), then Ivan wantsto know this way.Help Ivan byshowing him the way to change parameters!InputThe firstline of input contains one integer number m (1 ≤ m ≤ 6).The secondline contains m integernumbers a1, a2, ..., am (2 ≤ ai ≤ 1000). It is guaranteedthat .The thirdline contains m integernumbers b1, b2, ..., bm (1 ≤ bi ≤ ai).OutputIf there isa way to make exactly p changes(each change either decreases or increases some parameter by 1) to try allpossible combinations and return to initial combination, then output Cycle in thefirst line. Then p linesmust follow, each desribing a change. The line must be either inc x (increaseparameter x by 1) or dec x (decreaseit).Otherwise,if there is a way to make exactly p - 1 changesto try all possible combinations (including the initial one), then output Path in thefirst line. Then p - 1 linesmust follow, each describing the change the same way as mentioned above.Otherwise,output No.Examplesinput131outputPathinc 1inc 1input132outputNoinput23 21 1outputCycleinc 1inc 1inc 2dec 1dec 1dec 2  F. AlmostPermutationtime limitper test3 secondsmemory limitper test512megabytesinputstandardinputoutputstandardoutputRecentlyIvan noticed an array a while debugginghis code. Now Ivan can't remember this array, but the bug he was trying to fixdidn't go away, so Ivan thinks that the data from this array might help him toreproduce the bug.Ivan clearlyremembers that there were n elementsin the array, and each element was not less than 1 andnot greater than n. Also heremembers q factsabout the array. There are two types of facts that Ivan remembers:·      1 li ri vi — foreach x suchthat li ≤ x ≤ ri ax ≥ vi;·      2 li ri vi — foreach x suchthat li ≤ x ≤ ri ax ≤ vi.Also Ivanthinks that this array was a permutation, but he is not so sure about it. Hewants to restore some array that corresponds to the qfacts thathe remembers and is very similar to permutation. Formally, Ivan has denotedthe cost ofarray as follows:, where cnt(i) is thenumber of occurences of i in thearray.Help Ivan todetermine minimum possible cost of thearray that corresponds to the facts!InputThe firstline contains two integer numbers n and q (1 ≤ n ≤ 50, 0 ≤ q ≤ 100).Then q lines follow,each representing a fact about the array. i-th linecontains the numbers ti, li, ri and vi for i-th fact (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ vi ≤ n, ti denotesthe type of the fact).OutputIf the factsare controversial and there is no array that corresponds to them, print -1. Otherwise,print minimum possible cost of thearray.Examplesinput3 0output3input3 11 1 3 2output5input3 21 1 3 22 1 3 2output9input3 21 1 3 22 1 3 1output-1 
#include <stdio.h>#include <stdlib.h>int main(){int N;scanf("%d",&N);int L;int R;//输入int zero_L; int zero_R;int num_err;num_err=1;while(num_err!=0){for( int i=1; i<=N; i++)scanf("%d%d",&L[i],&R[i]);zero_L=0; zero_R=0;num_err=0;for(int i=1; i<=N; i++){if(L[i]==0)zero_L++;if(R[i]==0)zero_R++;}if(zero_L!=zero_R||zero_L==0||zero_R==0)num_err=1;for(int i=1; i<=N; i++){if(!(1<=L[i]&&L[i]<=N)){num_err=2; break; }if(!(1<=R[i]&&R[i]<=N)){num_err=2; break; }if(L[i]!=0)if(R[L[i]]!=i){num_err=3; break; }if(R[i]!=0)if(L[R[i]]!=i){num_err=3; break; }}if(num_err==1){printf("err\n%d",N);}else if(num_err==2){}else if(num_err==3){}else{//正常}}//处理int a; int num=0; int head; int j=0;for(int i=1; i<=N; i++){if(L[i]==0){//头 :headnum++;head=i;a[num]=i;if(j!=0){R[j]=head;L[head]=j;}for(int z=1; z<=N; z++){if(R[a[num]]!=0){num++;a[num]=R[a[num-1]];}}j=a[num];//尾 :j}}for(int i=1; i<=N; i++)printf("\n%d %d",L[i],R[i]);//	for(int i=1; i<=N; i++)printf("\n%d",a[i]);scanf("%d",&N);//保留框return 0;}/*74 75 00 06 10 20 41 0*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: