Show all threads Hide all threads Show all messages Hide all messages | why TLE??? (Dynamic Programming) THANKS | sashimi | 1167. Bicolored Horses | 10 Jun 2006 09:30 | 4 | const maxn=500; var n,k:integer; s:array[1..maxn]of byte; f:array[1..maxn,1..maxn]of integer; procedure init; var i,j:integer; begin { assign(input,'in.in');reset(input);} readln(n,k); for i:=1 to n do readln(s[i]); end; function g(p1,p2:integer):integer; var i,sum:integer; begin sum:=0; for i:=p1 to p2 do if s[i]=1 then inc(sum); g:=(p2-p1+1-sum)*sum; end; procedure dp; var i,j,mik,i2,t:integer; begin for i:=1 to n do begin if i<k then mik:=i else mik:=k; for j:=1 to mik do begin f[i,j]:=maxint; if j=1 then f[i,j]:=g(1,i) else for i2:=1 to i-j+1 do begin t:=f[i-i2,j-1]+g(i-i2+1,i); if t<f[i,j] then f[i,j]:=t; end; end; end; end; {=============main==============} begin init; dp; writeln(f[n,k]); end. How to solve it in O(n) time? How to slove it in (n^2). plz | Hint? | Deliverance | 1167. Bicolored Horses | 16 Dec 2005 16:37 | 1 | Hint? Deliverance 16 Dec 2005 16:37 Can someone give me a hint on solving this problem in O(n) and/or O(n^2) time? please it's for a school project, here is my e-mail: ethereal_2005_0@yahoo.com | DO you have any goodidea?? | Tony | 1167. Bicolored Horses | 30 Sep 2005 13:34 | 4 | | What is in test 1? | Bandera | 1167. Bicolored Horses | 3 May 2005 21:47 | 2 | Test 1 is the same as sample input in a problem? And another question: if there are only black horces or onle white horces then the answer is count of horces or 0? PLESE HELP ME! P.S. I know that i don't know English well... Sorry for a mistakes. surely,the answer is 0 if there's only one kind of horces. | Admins or others cam\n someone please tell me test case 1 | vj | 1167. Bicolored Horses | 15 Jun 2004 23:21 | 2 | Please some one help me by giving test case 1. I am waiting for it. Thank you. | Why Memory Limit Exceeded?????HELPHELPHELPHELPHELP | King Without Kingdom | 1167. Bicolored Horses | 13 Apr 2004 05:32 | 3 | [deleted by moderator] Edited by moderator 13.04.2004 07:47 You may use 'short int' instead of 'int' sizeof(int) == 4, sizeof (short int) == 2, according to the timus's compiler. So unsigned short int is enough. Even if this program fits in the memory limit, it will get TL. You have complexity of O(N^3). Mine was the same, it needs 3-4 secs. for N=500 K=300. I'm now wondering how to solve this problem. you must use longint not just int, but O(N) memory. O(N^3) works, don't know if N^2 possible | Can this problem get ac in a O(n^3) way without TLE?? | Dont be shy | 1167. Bicolored Horses | 29 Aug 2003 02:37 | 2 | Yes Silviu Ganceanu 29 Aug 2003 02:37 I got ACC with an program running in O(N^3) like this: If sol[i][j]=minimum unhappiness that it is obtained with the first i horses using j stables than sol[i][j]=min(sol[k][j-1]+ no[0]*no[1]) where k can take value between j-1 and i-1 and no[a] represent the number of horses coloured with 'a' from the interval [k+1, i]. You can use in this way O(N) memory and you can avoid getting MLE. Good luck! | Is there anybody knows 1167`s O(N^2) Solution?! (-) | Locomotive | 1167. Bicolored Horses | 25 Apr 2003 18:53 | 1 | | Anybody who wants any help on this problem can email to "hyz12345678@163.com". | Huang Yizheng | 1167. Bicolored Horses | 28 Dec 2001 16:07 | 5 | hi could you give me some hints on this ques i've got an inefficient n^3 soln tt always tle thanks > hi > could you give me some hints on this ques > i've got an inefficient n^3 soln tt always tle > > thanks Can anyone give me solution which isn't in n^3 time? Alexandar Can anyone give me solution which isn't in n^3 time? Alexandar | Why WA? | sillyboy | 1167. Bicolored Horses | 21 Dec 2001 13:07 | 4 | I always get wrong answer. Could anybody help me? Here is my programme: var n,k,c,d,f,g,h:word; t:text; a,b:array[0..500] of word; e:array[1..500,1..500] of word; begin assign(t,''); reset(t); readln(t,n,k); a[0]:=0; b[0]:=0; for c:=1 to n do begin readln(t,d); if d=0 then begin a[c]:=a[c-1]+1; b[c]:=b[c-1]; end else begin a[c]:=a[c-1]; b[c]:=b[c-1]+1; end; end; close(t); for c:=1 to n do for d:=1 to k do e[c,d]:=65535; for c:=1 to n do begin e[c,1]:=a[c]*b[c]; for d:=1 to c-1 do begin h:=(a[c]-a[d])*(b[c]-b[d]); if d<k-1 then f:=d else f:=k-1; for g:=1 to f do if e[c,g+1]>e[d,g]+h then e[c,g+1]:=e[d,g]+h; end; end; writeln(e[n,k]); end. This is really very strange, but if you do the following, you'll get AC. In part Var make a new variable i, and change cycle for g:=1 to f do if e[c,g+1]>e[d,g]+h then e[c,g+1]:=e[d,g]+h; to this: for i:=1 to f do begin g:=i; if e[c,g+1]>e[d,g]+h then e[c,g+1]:=e[d,g]+h; end; This should be the same, of course, but this way you'll get AC. Actually, I don't know why:) {Submit this program, you'll get Time Limit Exceeded!} var i,j,k:integer; p:array [1..2,1..1] of integer; begin p[1,1]:=1; p[2,1]:=0; for i:=1 to 1 do begin j:=1; while p[i,j]=0 do inc(j); for j:=2 to 2 do begin for k:=1 to 1 do p[j,k]:=p[i,k]; if p[2,1]=0 then repeat until false; end; end; end. I've got AC. :) Thank you very much indeed. | Why WA? | sillyboy | 1167. Bicolored Horses | 16 Dec 2001 10:43 | 1 | I always get wrong answer. Could you help me? |
|
|