Общий форумCould you give me any hints of the DP algorithm? Thanks! (mail to: comars@hotmail.com ) If it is NP, the complexity of the algorithm should be O (K*something) right ? Can you give me any hints on your solution ? mail: scythe@toughguy.net thanx a lot Could you give me any hints of the DP algorithm? Thanks! (mail to: cpp_student@163.com ) I also think it's nice!My algorithm use 0.5s.I use some random.It's nearly O(n*m) can you give your solution?? Thx remdy21@live.cn I naively applied suffix arrays with lcp and got AC in ~2.7s. (complexity N*K*log^2(K)) But I see that there are submissions with times <0.1s. What are the more efficient algorithms? Someone mentioned suffix-function - any referece for that? Also it was said that KMP can be used - can anyone explain how exactly it is applicable here? Thanks in advance. can you explain the sample for me Thx program ural; uses math; var s:array['X'..'Z']of integer; c:char; d,n,i:integer; begin readln(n); for i:=1 to n do begin read(c); readln(d); s[c]:=s[c]+d; end; if(s['X']>0)and(s['Z']>0)then begin d:=min(s['X'],s['Z']); inc(s['Y'],d); dec(s['X'],d); dec(s['Z'],d); end else if(s['X']<0)and(s['Z']<0)then begin d:=max(s['X'],s['Z']); inc(s['Y'],d); dec(s['X'],d); dec(s['Z'],d); end; if(s['X']>0)and(s['Y']<0)then begin d:=max(-s['X'],s['Y']); inc(s['Z'],d); inc(s['X'],d); dec(s['Y'],d); end else if(s['X']<0)and(s['Y']>0)then begin d:=min(-s['X'],s['Y']); inc(s['Z'],d); inc(s['X'],d); dec(s['Y'],d); end; if(s['Z']>0)and(s['Y']<0)then begin d:=max(-s['Z'],s['Y']); inc(s['X'],d); inc(s['Z'],d); dec(s['Y'],d); end else if(s['Z']<0)and(s['Y']>0)then begin d:=min(-s['Z'],s['Y']); inc(s['X'],d); inc(s['Z'],d); dec(s['Y'],d); end; writeln(abs(s['X'])+abs(s['Y'])+abs(s['Z'])); if s['X']<>0 then writeln('X ',-s['X']); if s['Y']<>0 then writeln('Y ',-s['Y']); if s['Z']<>0 then writeln('Z ',-s['Z']); end. If you have CRASH, may be, will help for you to make columns of a global variable and to clean from definitions of procedures. I got AC... Maybe you should increase the stack size as well. If you get WA2 try this one: Input: 3 3 2 2 1 1 -1 3 1 2 3 2 1 3 Output: 1 2 1 Also if you get WA12 pay attention for: "All identifiers lie between 0 and 40000." #include <iostream> #include <iomanip> #include<math.h> #include<vector> using namespace std; int main() { cout<<setiosflags(ios::fixed)<<setprecision(4); vector<double>v; double a; for(int i=0;!cin.eof();i++ ) { cin>>a; v.push_back(sqrt(a)); } for(int j=v.size()-2;j>=0;j--) {cout<<v[j]<<"\n";} return 0; } How to control the output precision?What does "to the acuracy of 10-6? Up! Why in task 10^6, but output 10^16 var i,j,k,n,m,tot:longint; ans,x1,y1,x2,y2,d1,d2,d3,x,y,angle1,angle2,t,p1,p2:double; function min(xx,yy:double):double; begin if xx>yy then exit(yy); exit(xx); end; begin {assign(input,'1215.in');reset(input);} read(x,y,n); read(x1,y1); p1:=x1; p2:=y1; d1:=sqrt(sqr(x-x1)+sqr(y-y1)); ans:=999999999; tot:=0; for i:=2 to n do begin read(x2,y2); if (x1<x)and(x2>=x)and((y1<y)or(y2<y)) then inc(tot); d2:=sqrt(sqr(x-x2)+sqr(y-y2)); d3:=sqrt(sqr(x1-x2)+sqr(y1-y2)); angle1:=(d2*d2+d3*d3-d1*d1)/(2*d2*d3); angle2:=(d1*d1+d3*d3-d2*d2)/(2*d1*d3); if (angle1>=0)and(angle2>=0) then begin if x1=x2 then t:=abs(x1-x) else t:=((y1-y2)/(x1-x2)*x-y+y1+(y2-y1)/(x1-x2)*x1)/(sqrt(sqr((y1-y2)/(x1-x2))+1)); t:=abs(t); ans:=min(ans,t); end else begin t:=min(d1,d2); ans:=min(ans,t); end; d1:=d2; x1:=x2; y1:=y2; end; x2:=p1; y2:=p2; if (x1<x)and(x2>=x)and((y1<y)or(y2<y)) then inc(tot); d2:=sqrt(sqr(x-x2)+sqr(y-y2)); d3:=sqrt(sqr(x1-x2)+sqr(y1-y2)); angle1:=(d2*d2+d3*d3-d1*d1)/(2*d2*d3); angle2:=(d1*d1+d3*d3-d2*d2)/(2*d1*d3); if (angle1>=0)and(angle2>=0) then begin if x1=x2 then t:=abs(x1-x) else t:=((y1-y2)/(x1-x2)*x-y+y1+(y2-y1)/(x1-x2)*x1)/(sqrt(sqr((y1-y2)/(x1-x2))+1)); t:=abs(t); ans:=min(ans,t); end else begin t:=min(d1,d2); ans:=min(ans,t); end; if odd(tot) then ans:=0; writeln(ans*2:0:3); end. just check your program on these tests ---------- 4 3 1 5 8 10 answer: 1 8 5 10 wrong answer: 1 5 8 10 ---------- 4 2 3 5 7 10 answer: 5 10 3 7 wrong answer: 7 10 5 3 That's helped me with wa#9: 9 0 1 1 6 6 23 35 36 38 40 Right answer has 2 trips. I know AC solution that answers 5 on this test instead of 3: /////////////start of test 6
////////////end of test 6 [1 space] [1 space] [2 spaces] [2 spaces] [3 spaces] [3 spaces] [EOF] Also this test should be answered 0 instead of 1. 2 MEXX MEXX I gues this tests suit the condition "The brands are the strings of Latin letters and blanks." and may be added. Thank you. You can Quote my post to view or copy&paste this tests. Blanks are abandoned by forum, but are in the post. It works like this: f1=1*f1+0*f2; f2=0*f1+1*f2; f3=1*f1+1*f2; f4=1*f1+2*f2; f5=2*f1+3*f2; f6=3*f1+5*f2; ...^....^ ...|....These coefficients are in the array c2 ...These coefficients are in the array c1 ----PROG BELOW---- program ural1133; const maxn=2001; var c1,c2:array[1..maxn]of extended; x,y,n,i:integer; fx,fy,f1,f2:longint; function min(a,b:integer):integer; begin if a<b then min:=a else min:=b; end; function max(a,b:integer):integer; begin if a>b then max:=a else max:=b; end; begin readln(x,fx,y,fy,n); i:=min(min(x,y),n); if i<=0 then begin i:=1-i; inc(x,i);inc(y,i);inc(n,i); end; c1[1]:=1;c2[1]:=0; c1[2]:=0;c2[2]:=1; for i:=3 to max(max(x,y),n) do begin c1[i]:=c1[i-2]+c1[i-1]; c2[i]:=c2[i-2]+c2[i-1]; end; f1:=round((fx*c2[y]-fy*c2[x])/(c1[x]*c2[y]-c1[y]*c2[x])); f2:=round((fx*c1[y]-fy*c1[x])/(c2[x]*c1[y]-c2[y]*c1[x])); writeln(round(c1[n]*f1+c2[n]*f2)); end. Try this: 46 1836311903 -46 -1836311903 45 Correct answer is 1134903170, but your program outputs 1073741824. Edited by author 10.11.2007 16:27 I have the same problem. Can anybody help? >> writeln(round(c1[n]*f1+c2[n]*f2)); For example c1[n]*f1 may exceed extended (c1[n]*f1+c2[n]*f2 don't, but you will lost correctness), and as I understand, test from UNKNOWN LAMER (thanks!), makes it true Edited by author 10.11.2007 16:33 Test 1 -2000000000 2 2000000000 3 is correct? Answer: 0 Yeah, why not? True thanks i found my mistake 2 -2 0 4 0 0 2 2 ans = INVISIBLE It is not test 3. I get WA3 whith ans INVISIBLE 0 -1 2 -1 2 0 0 0 answer: visible I don`t know what bug is in my programm. Who can give me this test. As I remember, in 12th test the point is in precision. If you write in Pascal, use Extended (in C++ - double), use very accurate value for PI (3.1415926 is NOT enough), in Pascal use standard constant "pi", in C - 2.0*acos(0.0) Well, in my case the reason for WA#12 was inproper implementation of Graham's algorithm, not a precision. Be careful with cases, when there are points with the same polar angle. My first code: #include <stdio.h> int n; long long f[501][501], g[501][501], res; void xxx(void) { for (int i=0; i<=n; i++) f[0][i] = 1; for (int i=1; i<35 && i<=n; i++) { for (int j=0; j<=n; j++) for (int k=0; k<=n; k++) { g[j][k] = f[j][k]; f[j][k] = 0; } for (int j=1; j<=n; j++) { for (int k=1; k<=j; k++) f[j][k] = f[j][k - 1] + g[j - k][k - 1]; for (int k=j+1; k<=n; k++) f[j][k] = f[j][j]; } res += f[n][n]; } } int main(void) { scanf("%d", &n); xxx(); printf("%lld\n", --res); return 0; } WA for #6. My second code: #include <stdio.h> int n; __int64 f[501][501], g[501][501], res; void xxx(void) { for (int i=0; i<=n; i++) f[0][i] = 1; for (int i=1; i<35 && i<=n; i++) { for (int j=0; j<=n; j++) for (int k=0; k<=n; k++) { g[j][k] = f[j][k]; f[j][k] = 0; } for (int j=1; j<=n; j++) { for (int k=1; k<=j; k++) f[j][k] = f[j][k - 1] + g[j - k][k - 1]; for (int k=j+1; k<=n; k++) f[j][k] = f[j][j]; } res += f[n][n]; } } int main(void) { scanf("%d", &n); xxx(); printf("%I64d\n", --res); return 0; } AC. Edited by author 30.08.2010 08:37 Difference only here: printf("%lld\n", --res); printf("%I64d\n", --res); #include <stdio.h> int n; long long f[501][501], g[501][501], res; void xxx(void) { for (int i=0; i<=n; i++) f[0][i] = 1; for (int i=1; i<35 && i<=n; i++) { for (int j=0; j<=n; j++) for (int k=0; k<=n; k++) { g[j][k] = f[j][k]; f[j][k] = 0; } for (int j=1; j<=n; j++) { for (int k=1; k<=j; k++) f[j][k] = f[j][k - 1] + g[j - k][k - 1]; for (int k=j+1; k<=n; k++) f[j][k] = f[j][j]; } res += f[n][n]; } } int main(void) { scanf("%d", &n); xxx(); printf("%I64d\n", --res); return 0; } This is AC. 20 7 D 9 D 7 D 6 D 1 D 2 D 3 L 2 The answer should be 4. > try these 11 13 12 : answer 72.00 5 6 11.01 13 12: answer 72.06 6 6 0 1 1000: answer 0.00 0 0 11 13 12: my answer is 72.00 6 6 why? I had the same problem too... I think 72,00 5 6 Edited by author 30.08.2010 13:02 thank you, your first test is really good. it helped me to solve problem successfuly)) the first test is really great! thank you :) During 3 days I couldn’t find appropriate way to work with big binomials having lost of order or overflow. Simple and clever routine was found and gave satisfaction. Could you give any hint: what kind of "simple routine" have you found? I also have got overflow / lost of order? Is it possible to avoid such problem in O(N^2) solution? I don't know what your algo is, but my dynamic O(N^2) solution hasn't problems with overflows - all numbers is of order N Thanks! I've got AC with O(N^2) DP. Every value was not exceeding N. But it's still interesting - how did some people get AC with 0.015sec and minimum of memory. |
|