Common BoardInput: 7 1 1 1 1 1 1 1 1 2 1 3 1 4 1 5 2 6 2 7 9 1 1 1 1 1 1 1 1 1 1 1 6 2 2 1 2 2 7 output: 7 8 I used segment tree, but got wa on testcase 6 Yes, the output is correct. This is when there's only one pole. 1 1 2 The answer should be: 1 1 what's is the wrong with this solution ?: i find the shortest path from the first vertx in the chain to the last ( with bfs) an print that path.i read my code , and I think it's correct ,say if I misunderstood the problem You have. I can not understand any thing else that I said , I read the problem description many times !! * the initial and final vertices of the chains p and q coincide; * the edges in the chain q are in the same order as in the chain p; * the chain q has the minimal possible number of edges under the given conditions. means that , it should be a path from first vertex to last ,and should use only the chains edges and should be minimal . so is the shortest path between first and last!! could you pleas give me a test, that my algorithm crashs? Yes after the contest is finished ;-) thnx! ;-) the edges in the chain q are in the same order as in the chain p; BFS breaks this rule Can you share tests now? I also want to know, what was in the 7th test:) something like that 13 1 2 3 4 5 6 7 6 2 6 8 9 7 my answer is 1 2 6 7,I think it's right,but still Wa test 7 Edited by author 11.07.2012 20:55 Edited by author 11.07.2012 20:55 Edited by author 03.04.2013 23:29 Right answer for 15 1 2 3 4 5 6 7 6 2 5 2 8 9 10 7 is 1 2 8 9 10 7 Edited by author 27.01.2016 19:11 Edited by author 27.01.2016 19:11 That is not right. Edges should be in the same order as the initial path. In your answer edges are not in the same order. Here is the right one: [1 2 8 9 10 7] try this test 17 1 2 3 4 5 6 7 2 8 9 5 10 8 9 11 3 9 1 2 6 7 - is not correct answer check yourself 1 2 6 8 9 7 - correct to Piratek-(akaDK) : Please said me why 1 2 8 9 is not correct? thx My program says 1 2 8 9 for 17 1 2 3 4 5 6 7 2 8 9 5 10 8 9 11 3 9 and 1 2 6 8 9 7 for 13 1 2 3 4 5 6 7 6 2 6 8 9 7 both answers seem correct, but i'm still getting WA7. What is the problem with it? This test helped me to pass 7 13 1 2 3 4 5 6 7 3 6 11 12 10 7 (1 2 3 4 5 6 7) However, I got WA 12 :-( Then I invented this one 15 1 2 3 12 4 5 6 7 3 6 11 12 10 7 6 (1 2 3 6) And finally got AC.. 13 1 2 3 4 5 6 7 6 2 6 8 9 7 Why not (1 2 6 7) ?? For input 13 1 2 3 4 5 6 7 6 2 6 8 9 7 the output 1 2 6 7 is wrong, because here edge (6,7) comes after (2,6), but in input edge (6,7) comes before edge (2,6). Hint: Don't use BFS, there is a simple O(n) solution, you don't need to construct graph. thanks But if decided to use BFS? It is important to know: 1. Can vertex be used multiply in BFS search- Yes. 2.Can edge be used multiply in BFS search-Yes. Thus what objects should we use in BFS constructing and how avoid loop in BFS. Good mastering in BFS more important than cleverness by the chance. P.S. Ac with BFS. 18 bad submissions on test 3 were answer: v1- I think that it is stupid trick. Case v1=v2 should be processed as common. BFS: 1)used pair(r1,r2) adjacent edges and r2 after r1 as object of layers of BFS because this pair can be meet in searching uniquely. And must use set<pair> to defining that pair is new. 2) used vector<int> sloi1,sloi2 for layers of BFS and struct pair data[1000000] as store of all items of BFS. Time Ac is bad but approach is in standard layout. After first Ac I had got Tle 17 because of new redjudjement. Wanted to have Ac throught BFS and no DP. I recoded BFS radically: 1) foud all states on reading data stage and stored its in array data[3000000] 2) used char met[3000000] to control uniquness of new states on searchind in BFS These helped to take AC 0.640c. Edited by author 08.11.2008 12:55 Infinity, i've produced correct answer to your test, but WA12 anyway :( give more tests, please I think you have just a bug somewhere.. Because I had. If you've passed 7, the idea seems to be right.. Try to check your code better.. I don't have more good tests I think test 12 contains something like that: 4 1 2 1 3 At least, such test helps in my WA12 :) My program works for all the test cases given in this thread, but it is giving WA#6, can you please give me this test case? Mine was wrong because of the test: 17 1 2 12 3 4 7 4 1 5 4 7 8 4 9 10 11 8 (Correct answer is "1 5 4 7 8") Can anybody give me test #7? Edited by author 18.12.2008 20:57 Please, give me test 7. hello to all i could'nt pass test 7 but i've a good test 9 1 2 3 4 1 3 5 6 4 Try to use this one for test 12: 13 1 4 5 13 8 6 4 8 17 11 12 8 7 Answer: 1 4 8 7 CODE:- import java.util.*; public class titans { public static void main (String[] args) throws java.lang.Exception { int ind = 0; int w = 0; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int largest = a[0] + a[1] + a[2]; if (n%2 != 0 ) { w = (n/2) + 1; } else { w = n/2; } for (int j = 0; j <= w; j++) { int new_long = a[j] + a[j+1] + a[j+2]; if (new_long >= largest) { largest = new_long; ind = j + 2; } } System.out.println(largest + " " + ind); } } ---------------------------------------------------------- Please tell where am I going wrong. The code is working fine on my system. Thanks in advance. ----------------------------------------------------------- Thanks for your time but I have found the solution - thanks again! Edited by author 28.11.2015 19:30 I solved this problem with a non-greedy solution. Basically there are only one of the following 4 cases: 1. total number of '+' is <= 2N, problem solved; 2. There exists one column without any '+', and there also exists one column with more than 2 '+'(i.e. at least three), in this case you can "move" that two '+'s to the empty column (through two permutations); 3. Similar condition for case 2, move the row; 4. None of above is satisfied, using Hungarian Algorithm to find a best traversal. Repeat above process until case 1 is reached. This is not greedy and we can prove that it is correct: Both case 2 and 3 guarantees that 1) the total number of '+'s does not change, and 2) cannot continue infinitely, i.e. it must reach case 4 at some point. Then case 4 guarantees the total number of '+'s will be reduced at least by 1 (due to the fact that 2N + 1 is odd, and neither case 2 and case 3 is satisfied, can be easily proved) Good solution! A great fix to the greedy algorithm Code: const Nmax=155000; var Z,L1,R1:array[1..Nmax] of longint; A:array[1..Nmax] of char; N,M,Middle,L,R,i,j,k:longint; ch:char;
function Min(x,y:longint):longint; begin if(x>y) then x:=y; Min:=x; end;
BEGIN N:=0; read(ch); while(ord(ch)>=ord('a')) do begin inc(N); A[N]:=ch; read(ch); end; readln;
inc(N); A[N]:='#'; M:=N; read(ch); while(ord(ch)>=ord('a')) do begin inc(N); A[N]:=ch; read(ch); end;
Z[1]:=N; R:=0; L:=0;
for i:=2 to N do begin if(i<=r) then Z[i]:=min(R-i+1,Z[i-L+1]) else Z[i]:=0; while(i+z[i]<=N)and(A[i+z[i]]=A[z[i]+1]) do inc(z[i]); if(i+z[i]-1>r) then begin L:=i; R:=i+z[i]-1; end; end;
k:=0; L1[k]:=N+1; R1[k]:=N+1;
i:=N; while(i>=M+1) do begin if(i+z[i]-1>=L1[k]-1) then begin inc(k); L1[k]:=i; R1[k]:=L1[k-1]-1; end; dec(i); end;
if(L1[k]=M+1) then begin writeln('No'); while(k>0) do begin for i:=L1[k] to R1[k] do write(A[i]);write(' '); dec(k); end; end else begin writeln('Yes'); end; END.
In what position brick can fall through the hole? Why program like follows doesn't work properly? var a, b, c, d, e: real; begin readln(a, b, c, d, e); if (a<=d)and(b<=e)or (a<=e)and(b<=d)or (a<=d)and(c<=e)or (a<=e)and(c<=d)or (b<=d)and(c<=e)or (b<=e)and(c<=d) then writeln('YES') else writeln('NO') end. We can rotate bricks! Example 1x10x10 we can push to 9x9 Thanks for Your hint! I've got AC now. Edited by author 28.07.2004 11:31 Thanks for Your hint! I've got AC now. Edited by author 28.07.2004 11:31 Thanks for Your hint! I've got AC now. Edited by author 28.07.2004 11:31 Haha, I wrote the exactly same code... Please, tell me some tests to check my program... I have WA#2 Problem statement is wrong. Two types of walls should be exchanged. I'm change type of walls, bu I still have WA#2. Could you give me some test? Had WA2, because i've read walls, but completely forgot to take them into account. Haha... Give me test#12, please. I tried all tests in the discussions, but still WA12 Edited by author 27.01.2014 14:42 0e2000000000 10 0e4000000000 10 One more test 100e- 1 Edited by author 28.11.2015 11:54 Edited by author 28.11.2015 11:54 Решая задачу обыватель мог столкнуться с проблемой того,как вводить данные. Данные водятся потоком, поэтому мы не знаем какое количество чисел войдут на проверку, для этого нам нужно использовать следующую конструкцию: var r:real; .. Begin while not seekeof do Begin read(r); .. End; .. End. while not seekeof do - пока не конце файла, то делай то то. пропуская пробелы и переходы на новую строчку. Но так как отсутствует файлы, то он ждёт конец потока вводимых с экрана данных. Для решения просто вставляем эту конструкцию, дополняя кодом и всё. http://acm.timus.ru/help.aspx?topic=pascal Edited by author 28.11.2015 11:30here is my code if someone can help me, thanks you and greetings. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; public class P1100 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int [] vec=new int[n*2]; for(int i=0;i<n*2;i++)vec[i]=in.nextInt(); int [] aux=new int[n]; int mayor=0,menor=100; for(int i=1;i<n*2;i=i+2){ if(menor>vec[i]){ menor=vec[i]; } if(mayor<vec[i]){ mayor=vec[i]; } } int sig=-1; for(int i=0;i<n;i++){ for(int j=1;j<n*2;j=j+2){ if(mayor==vec[j] && vec[j]!=-1 ){ System.out.printf(" %d %d",vec[j-1],vec[j]); vec[j]=-1;
} if(sig<mayor && sig < vec[j] && vec[j]!=-1){ sig=vec[j]; } } mayor=sig; sig=-1; } out.flush();
} } Your solution contains 2 nested loops with range [0..n) each. So you have n^2 complexity. Of course you have TLE.
You should better load input into array of structures (with fields Id, Score), sort array by score field in decreasing order and print it. * "Collections.sort" is stable so you can just use it. im trying now with a arrayList of a node(int ele,int pos), but i dont know how no order that, can you help me? Hi. Is O(N^2) solution good for this problem? I got TL on 8th test. Should I reduce constant in O() or should I find N*logN solution? It's strange that 25M operations take over 2 seconds. Ofc no; the max N is 10^5, so N^2 would be 10^10; thats about 50 seconds to do such number of operations on a normal PC Yes. I thought it is 10^4. I was neglectful. N^2 is almost never acceptable if N exceeds ~4000-5000. I attempted to think a bit at this task... as far as i understood, for flowers with a certain dryness X, let us assume that the leftmost flower with such dryness is X1, the rightmost is X2, Kirill's position is Y. Cases like Y...X1...X2 and X1...X2...Y are easy, Kirill needs to go to the rightmost and to the leftmost flower respectively, but in case of X1...Y...X2 it gets a bit harder. At first, i assumed that we should just go first to the side that we're closer to, but then, i found out a counter test for that: 19 2 9 9 9 9 9 9 9 1 9 9 9 9 9 9 2 3 4 5 Initial path to the right 2, through 6 nines, is shorter than to the left 2, through 7 nines, but eventually this turns out to be a bad decision, since we have to go all the way to the right again. So probably, we should somehow take a group of flowers with next closest dryness into consideration. I'll try to think of it some more when i'm not lazy... Hello! I solved this problem O(N*logN). Idea=Sort+Dp[i,x],where x=the most Right and the most Left . Edited by author 05.11.2015 18:28 ... Edited by author 07.11.2015 13:32 19 2 9 9 9 9 9 9 9 1 9 9 9 9 9 9 2 3 4 5 70, btw for java AC long for path[] is enough (BigInteger works slower few milliseconds) I use Python 3.5 Is it right that it's prohibited to import function gcd from library math? But it's allowed to import this function from library fractions Edited by author 27.11.2015 02:04 If there is three equal points than perimeter equals zero? Something strange: if three points with equal coordinates than output zero. -> WA6 if four points with equal coordinates than output zero. -> AC This made me crazy. Can somebody clarify? I have the same trouble. Maybe something wrong with test or checker? I know i am stupid but help me to understand this formula! n*(n+1)/2*(n+2) Edited by author 02.11.2013 03:06 Edited by author 02.11.2013 03:12 This formula is equivalent to the this sum: sum(i=0 -> N) sum(j=i -> N) (i + j) Edited by author 27.11.2015 01:21 // del got AC Edited by author 26.11.2015 22:07 Edited by author 27.03.2016 21:21 here is my code if someone can help me, thanks you and greetings. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; public class P1100 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int [] vec=new int[n*2]; for(int i=0;i<n*2;i++)vec[i]=in.nextInt(); int [] aux=new int[n]; int mayor=0,menor=100; for(int i=1;i<n*2;i=i+2){ if(menor>vec[i]){ menor=vec[i]; } if(mayor<vec[i]){ mayor=vec[i]; } } int sig=-1; for(int i=0;i<n;i++){ for(int j=1;j<n*2;j=j+2){ if(mayor==vec[j] && vec[j]!=-1 ){ System.out.printf(" %d %d",vec[j-1],vec[j]); vec[j]=-1;
} if(sig<mayor && sig < vec[j] && vec[j]!=-1){ sig=vec[j]; } } mayor=sig; sig=-1; } out.flush();
} } import java.io.PrintWriter; import java.util.Scanner; public class P1581 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int [] vec=new int[n]; for(int i=0;i<n;i++)vec[i]=in.nextInt(); int cont=0,num; num=vec[0]; for(int i=0;i<n;i++){ if(vec[i]==num){ cont=cont+1; } if(vec[i]!=num || i+1==n){ if(i+1!=n){ System.out.printf("%d %d ",cont,num); }else{ System.out.printf("%d %d",cont,num); } num=vec[i]; cont=1; }
} out.flush(); } } ---- THANKS YOU, GREETINGS. Edited by author 26.11.2015 05:45 nub qlo int cont=0,num; num=vec[0]; for(int i=0;i<n;i++){ if(vec[i]==num){ cont=cont+1; } if(vec[i]!=num ){ System.out.printf("%d %d ",cont,num); num=vec[i]; cont=1; } } System.out.printf("%d %d",cont,num); |
|