Common Board| Show all threads Hide all threads Show all messages Hide all messages | | Any Good algorithm?? My algo is O( H * W * ( 5 + 5 + 1 )^2 ). | c_pp | 1121. Branches | 12 Jan 2017 03:41 | 1 | Who know O(H*W) or, O(H*W*5) algo ??? | | WRONG TEST 15!!! => БЕРИТЕ МАССИВ CHAR РАЗМЕРОМ 21 ЭЛЕМЕНТ))) | Ilya | 1083. Factorials!!! | 12 Jan 2017 00:21 | 1 | С 20 не проходит 15 тест!!! | | Скажите, пожалуйста, хелп, что здесь не так?) | Ilya | 1079. Maximum | 11 Jan 2017 23:56 | 1 | PS. У меня тесты проходит PSS. базовые) #include<iostream> #include<vector> using namespace std; int test(int a); vector<int> list; //vector<int> answers; int main() { list.push_back(0); list.push_back(1); int N; cin >> N; while (N != 0) { cout << test(N); // answers.push_back(test(N)); cin >> N; } //for (int i = 0; i < answers.size(); i++) // cout << answers[i] << endl; return 0; } int test(int a) { int i = 2; if (a < list.size() && a % 2 == 0) return list[a - 1]; else if (a < list.size() && a % 2 != 0) return list[a]; else if (a >= list.size()) i = list.size(); for (i; i <= a; i++) { if (i % 2 == 0) list.push_back(list[i / 2]); else list.push_back(list[i / 2] + list[i / 2 + 1]); } if (a % 2 == 0) return list[a - 1]; else return list[a]; } | | No subject | Mukul Barai | 1020. Rope | 11 Jan 2017 16:18 | 1 | Edited by author 11.01.2017 16:19 Edited by author 11.01.2017 16:21 | | g++ vs visual studio | Sevak Sargsyan | 1196. History Exam | 11 Jan 2017 10:18 | 4 | the same c++ code was accepted when I used visual studio, but gave TLE 8 with g++ 9 (or g++ 9, C11) Same happened to me. I've removed everything from the code except scanf("%d", &t); and it took 1.9 seconds just to read test #8 input using G++. Same code runs in 0.25 seconds under Visual C++ 2010. Thank you. I also scaned with scanf("%d", &t) and got ~1.5+/- sec using G++ and ~0.3 sec using VC++. Another idea to optimize is to scan with gets() and hex values. Edited by author 28.09.2016 00:02 Edited by author 28.09.2016 00:02 | | WA6 | SProf | 1160. Network | 11 Jan 2017 01:38 | 2 | WA6 SProf 16 Feb 2016 17:19 i try to solve it with kruskal and it gives me wa6 can anyone help me? struct edge { int len; int a; int b; }; int rank[1024]; int parent[1024]; edge e[15008]; int n,m; int get_parent(int x) { if (x != parent[x])parent[x] = get_parent(parent[x]); return parent[x]; } bool join_dsu(int x, int y){ x = get_parent(x); y = get_parent(y); if (x == y) return false; if (rank[x] < rank[y]) parent[x] = y; else if (rank[x] > rank[y]) parent[y] = x; else parent[y] = x, ++rank[x]; return true; } // read edges // sort edges by len // p[i] = i, for i = 1..n // let l_min = 0; // i = 1..m // if ( join_dsu( e[i].a, e[i].b) ){ l_min = e[i].len; } // print l_min and all edges where len <= l_min, GOOD LUCK !! | | Why WA on test 5 ? help me!!! | Artem Ladik | 1348. Goat in the Garden 2 | 10 Jan 2017 22:54 | 6 | type Ta=record x,y:extended; end; var a,b,c:Ta; ab,ac,bc,p,s,h,m:extended; l:extended; function max(a,b:extended):extended; begin if a-b>0 then max:=a else max:=b; end; begin readln(a.x,a.y,b.x,b.y); readln(c.x,c.y,l); ac:=sqrt(sqr(a.x-c.x)+sqr(a.y-c.y)); ab:=sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)); bc:=sqrt(sqr(b.x-c.x)+sqr(b.y-c.y)); p:=(ab+ac+bc)/2; s:=sqrt(p*(p-ac)*(p-ab)*(p-bc)); if ab=0 then h:=ac else h:=2*s/ab; if h-l>0 then writeln(h-l:0:2) else writeln('0.00'); m:=max(ac,bc); if m-l>0 then writeln(m-l:0:2) else writeln('0.00'); end. Edited by author 03.08.2008 16:37 Try this test: -1 1 1 1 -4 -3 0 Correct answere is 5.00 6.40 I have: var ab,ca,cb,p,s1,s2,Streyg,cosA,cosB:real; Xa,Ya,Xb,Yb,Xc,Yc,L:integer; Begin Readln(Xa,Ya,Xb,Yb,Xc,Yc,L); ab:=sqrt(sqr(Xa-Xb)+sqr(Ya-Yb)); ca:=sqrt(sqr(Xa-Xc)+sqr(Ya-Yc)); cb:=sqrt(sqr(Xb-Xc)+sqr(Yb-Yc)); p:=(ab+ca+cb)/2; Streyg:=(sqrt(p*(p-ab)*(p-ca)*(p-cb)));
if ((ca>cb) and (L>=ca)) or ((ca<cb) and (L>=cb)) or ((Xa=Xb) and (Ya=Yb) and (L>=ca)) then begin s1:=0; s2:=s1; end else if (Xa=Xb) and (Ya=Yb) then begin s1:=ca; s2:=s1; end else begin if (Streyg=0) or (L>=ca) or (L>=cb) or (L>=2*Streyg/ab) then s1:=0 else begin cosA:=(ca*ca+ab*ab-cb*cb)/(2*ca*ab); cosB:=(ab*ab+cb*cb-ca*ca)/(2*ab*cb); if (cosA>=0) and (cosB>=0) then s1:=2*Streyg/ab-L else if ca>cb then s1:=cb-L else s1:=ca-L; end; if ca>cb then s2:=ca-L else s2:=cb-L; end;
writeln(s1:3:2); write(s2:3:2);
End. give me some test Основание перпендикуляра не попадёт в отрезок. А по теореме Пифагора получаем для катетов 3 и 4 гипотенузу 5 | | WA 5 pls. help me . thanks beforehand | Paata Julakidze[GTU] | 1348. Goat in the Garden 2 | 10 Jan 2017 22:51 | 2 | import java.util.Scanner; public class Problem_1348 { public static void main(String[] args) { Scanner in=new Scanner(System.in); int ax,ay,bx,by,cx,cy,l; double ac,bc,ab,x,min,A,B,C,h; x=h=0; ax=in.nextInt(); ay=in.nextInt(); bx=in.nextInt(); by=in.nextInt(); cx=in.nextInt(); cy=in.nextInt(); l=in.nextInt();
ab= Math.sqrt( (bx-ax)*(bx-ax)+(by-ay)*(by-ay) ); ac =Math.sqrt( (cx-ax)*(cx-ax)+ (cy-ay)*(cy-ay) ); bc= Math.sqrt( (cx-bx)*(cx-bx) + (cy-by)*(cy-by) ); A=ac; B=bc; C=ab;
x=(A*A-B*B+C*C)/(2*C); h= Math.sqrt((A*A-x*x)); min=h;
if(A>B){ if(min-l<0)System.out.printf("%.2f",0.00); else System.out.printf("%.2f",min-l); System.out.println(); if(A-l<0)System.out.printf("%.2f",0.00);else System.out.printf("%.2f",A-l); }else{ if(min-l<0)System.out.printf("%.2f",0.00); else System.out.printf("%.2f",min-l); System.out.println(); if(A-l<0)System.out.printf("%.2f",0.00);else System.out.printf("%.2f",B-l); }
} } Edited by author 06.05.2014 16:57 Если основание высоты не попадает в отрезок AB, то такого расстояния не хватит | | Something with getline() function . Please let me know... | Yermakov Alex <ONPU> | 1446. Sorting Hat | 10 Jan 2017 21:30 | 3 | #include <iostream> #include <string.h> int main() { char grif[1001][202],sliz[1001][202],huff[1001][202],rave[1001][202],name[202],group[20]; int n,i,gcur=0,scur=0,hcur=0,rcur=0; std:: cin >> n; while( n>=0 ) { std:: cin.getline(name,202); std:: cin.getline(group,20);
if(!strcmp(group,"Slytherin")) { strcpy(sliz[scur],name); scur++; } if(!strcmp(group,"Hufflepuff")) { strcpy(huff[hcur],name); hcur++; } if(!strcmp(group,"Gryffindor")) { strcpy(grif[gcur],name); gcur++; } if(!strcmp(group,"Ravenclaw")) { strcpy(rave[rcur],name); rcur++; }
--n; }
std::cout<<"Slytherin:\n"; for( i=0; i<scur; ++i ) std:: cout << sliz[i] << '\n'; std::cout<<'\n';
std::cout<<"Hufflepuff:\n"; for( i=0; i<hcur; ++i ) std:: cout << huff[i] << '\n'; std::cout<<'\n';
std::cout<<"Gryffindor:\n"; for( i=0; i<gcur; ++i ) std:: cout << grif[i] << '\n'; std::cout<<'\n';
std::cout<<"Ravenclaw:\n"; for( i=0; i<rcur; ++i ) std:: cout << rave[i] << '\n'; std::cout<<'\n';
return 0; } This is madness! Don't write such code =) int main() { int students; cin >> students; map< string, vector<string> > house_to_students; for (int student = 0; student < students; student++) { cin.ignore();
string student_name; getline(cin, student_name); string house; cin >> house; house_to_students[house].push_back(student_name); } for (auto house : { "Slytherin", "Hufflepuff", "Gryffindor", "Ravenclaw" }) { cout << house << ":" << endl;
for (auto student : house_to_students[house]) cout << student << endl; cout << endl; } } It happened bcs of cin function In the input they give n and endl after n; so, somehow getline reads this endl as a string, so you can do this insted of "cin>>n" use "scanf("%d\n", &n); | | Wrong answer test 2 on c++ | TIU_Sarexer | 1068. Sum | 10 Jan 2017 14:46 | 5 | #include <iostream> using namespace std; int main() { int n, res = 0; cin >> n; if (n < 0) { for (int i = -2; i >= n; i--) { res = res + i; } cout << res; } else { for (int i = 1; i <= n; i++) { res = res + i; } cout << res; } return 0; } int i = -2; i >= n; i-- i = 1 Optimization trick with initial i=-2 is funny. But now you aren't processing case "n == 0" correctly. if(n == 0) --> ans = ? bro.. i don't understand... i'm from vietnam Lets read task: > sum of all integer numbers lying between 1 and N inclusive If N==0 then set of integers to sum is [0..1], sum is 1. | | My algo complexity is O(n^4), Is it exists any more efficient? (-) | Victor Barinov (TNU) | 1103. Pencils and Circles | 9 Jan 2017 20:55 | 6 | Algo which I wrote is O(n*logn) but also I know O(n) one. Try to choose one point which will be on the circle. O(1) Than try to choose another one point which will be on the circle. O(n*logn) or O(n) Finally build circle by these two points. O(n) PS: I used such two points that all other points are at one side of the line which contains these two points. Why!? Victor Barinov (TNU) 12 May 2005 00:04 I not understand why your algo is right. Maybe we'll discuss some questions by e-mail. Mine is victorbarinov@ua.fm Thanks! My simple linear solution uses Inversive geometry. 1. Inverse plane with respect to one of points (let it is A). O(n). 2. Chose another (not A) most left point (let it is B). O(n). 3. Sort all points without A and B with respect to B by polar angle and chose middle (let it is C). We want to get exactly one element on middle place, so I used std::nth_element which work O(n). 4. Output A, B, C. O(1). Thx @Ripatti [Ufa]. A solve with O(n) , got AC 0.001s. Hint for others: 1. did not need any math functions, like acos, atan. use simple vector multiplications. 2. calc cos(v[i]) , i>2. values before sorting. 3. use std::nth_element 4. if need more speed, use buffered i/o. Good Luck!! I can guarantee that the above algorithm is working. I came up with a similar algorithm :) | | Java BigInteger... | tiancaihb | 1108. Heritage | 9 Jan 2017 17:55 | 2 | Very short,AC in 0.484s. Not so fast as I expected. C/C++ Karatsuba gives 0.015s!!! | | Help | Mukul Barai | 1008. Image Encoding | 9 Jan 2017 17:36 | 1 | Help Mukul Barai 9 Jan 2017 17:36 Please someone tell me where is the wrong in my code. #include <iostream> #include <queue> #include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; struct dott{ int x; int y; }; int main() { char str[8]; gets(str); if(strlen(str) <= 2){ int a, b, n; n = atoi(str); bool XY[200][200]; memset(XY, 0, sizeof(XY)); struct dott center; cin>>a>>b; center.x = a; center.y = b; for(int i=2; i<=n; i++){ cin>>a>>b; XY[a][b] = 1; } queue<dott> qu; qu.push(center); cout<<center.x<<" "<<center.y<<"\n"; while(!qu.empty()){ dott pix = qu.front(); qu.pop(); if(XY[pix.x + 1][pix.y] == 1){ cout<<"R"; center.x = pix.x + 1; center.y = pix.y; qu.push(center); XY[pix.x + 1][pix.y] = 0; } if(XY[pix.x][pix.y + 1] == 1){ cout<<"T"; center.x = pix.x; center.y = pix.y + 1; qu.push(center); XY[pix.x][pix.y + 1] = 0; } if(XY[pix.x - 1][pix.y] == 1){ cout<<"L"; center.x = pix.x - 1; center.y = pix.y; qu.push(center); XY[pix.x - 1][pix.y] = 0; } if(XY[pix.x][pix.y - 1] == 1){ cout<<"B"; center.x = pix.x; center.y = pix.y - 1; qu.push(center); XY[pix.x][pix.y - 1] = 0; } if(!qu.empty()){ cout<<",\n"; } else{ cout<<"."; } } } else{ int a, b, count=0; bool XY[200][200]; memset(XY, 0, sizeof(XY)); struct dott centre; char *s; s = strtok(str," "); a = atoi(s); s = strtok(NULL," "); b = atoi(s); XY[a][b] = 1; centre.x = a; centre.y = b; queue<dott> qu; qu.push(centre); string str[200]; for(int i=0; ;i++){ cin>>str[i]; if(str[i] == "."){ break; } count++; } for(int i=0; i<count; i++){ dott S = qu.front(); qu.pop(); for(int j=0; j<str[i].size(); j++){ if(str[i][j] == 'R'){ XY[S.x+1][S.y] = 1; centre.x = S.x+1; centre.y = S.y; qu.push(centre); } if(str[i][j] == 'T'){ XY[S.x][S.y+1] = 1; centre.x = S.x; centre.y = S.y+1; qu.push(centre); } if(str[i][j] == 'L'){ XY[S.x-1][S.y] = 1; centre.x = S.x-1; centre.y = S.y; qu.push(centre); } if(str[i][j] == 'B'){ XY[S.x][S.y-1] = 1; centre.x = S.x; centre.x = S.y-1; qu.push(centre); } } } cout<<count+1<<"\n"; for(int i=0; i<100; i++){ for(int j=0; j<100; j++){ if(XY[i][j] == 1){ cout<<i<<" "<<j<<"\n"; } } } } return 0; } Edited by author 09.01.2017 17:37 | | HELP | Mukul Barai | 1008. Image Encoding | 9 Jan 2017 17:31 | 1 | HELP Mukul Barai 9 Jan 2017 17:31 Please someone tell me where maximum people make mistake in this problem. Or please provide some hints. | | I can find the way to solve this problem but i can't use operate with bignum. Can anyone give me any trick or hint??? | Badd | 1108. Heritage | 9 Jan 2017 17:20 | 4 | | | HELP!CRASH in test 1!Please HELP ME! | GHLXLF | 1008. Image Encoding | 9 Jan 2017 16:38 | 2 | #include<stdio.h> #include<string.h> char s[100]; bool map[200][200],bo; int n,m,x,y,xx,yy,a[11000],b[11000],c,zy; void find(int xx,int yy,int xy) { bool l,r,s,x; l=r=s=x=false; map[xx][yy]=false; if (map[xx+1][yy]) {printf("R");map[xx+1][yy]=false;c++;a[c]=xx+1;b[c]=yy;} if (map[xx][yy+1]) {printf("T");map[xx][yy+1]=false;c++;a[c]=xx;b[c]=yy+1;} if (map[xx-1][yy]) {printf("L");map[xx-1][yy]=false;c++;a[c]=xx-1;b[c]=yy;} if (map[xx][yy-1]) {printf("B");map[xx][yy-1]=false;c++;a[c]=xx;b[c]=yy-1;} if (xy==c) return; printf(",\n"); if (xy<c) find(a[xy+1],b[xy+1],xy+1);
} int main() { memset(map,false,sizeof(map)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); gets(s); if (strlen(s)==1) { n=s[0]-48; for (int i=1;i<=n;i++) { scanf("%d%d",&x,&y); if (i==1) { xx=x; yy=y; } map[x][y]=true; } a[1]=xx; b[1]=xx; c=1; printf("%d %d\n",xx,yy); find(xx,yy,1); printf(".\n"); } else { x=s[0]-48; y=s[2]-48; map[x][y]=true; zy=1; c=1; while (true) { gets(s); if (s[0]=='.') break; for (int i=0;i<=strlen(s);i++) { if (s[i]=='R') { c++;a[c]=x+1;b[c]=y; map[x+1][y]=true; } else if (s[i]=='T') {map[x][y+1]=true;c++;a[c]=x;b[c]=y+1;} else if (s[i]=='L') {map[x-1][y]=true;c++;a[c]=x-1;b[c]=y;} else if (s[i]=='B') {map[x][y-1]=true;c++;a[c]=x;b[c]=y-1;} } zy++; x=a[zy]; y=b[zy]; } printf("%d\n",zy); for (int i=1;i<=10;i++) for (int j=1;j<=10;j++) if (map[i][j]) printf("%d %d\n",i,j); } } Edited by author 09.01.2017 16:46 | | Yeah!!My program run in 0.02s,whoese can faster than mine??:))) | CleverKid | 1122. Game | 9 Jan 2017 12:48 | 9 | Hi Clever Can you tell me your Email Address??? I've written a mail to you, but the chinese e-mail server is all borken...(there is some virus..) So I can't send the mail to you :( My mail address:vhappy@netease.com IT's not difficult to do it in 0.015s.=) Can you just say what's the idia of these fast solutions :? I want to know too...how does it do it so fast? wow, can you mail me my id is shlakshmanan@inautix.co.in There exists O(256*16*9) solution with 65536 byte dp[] memory..... a little hint: separate flipping cells in the 1- and 2- rows with flipping cells in the 3 - and 4- rows . Edited by author 09.01.2017 12:52 | | Different Execution Time | mike1808 | 1712. Cipher Grille | 8 Jan 2017 01:53 | 3 | Can anybody explain me why one program can have different execution time with the same code? My friend with my code got 0.015 but I got 0.031. Oops =) I send it one more time and get 0.015. Maybe it because of server... Edited by author 22.11.2010 21:59 I had 0.031 sec, but now I finally got 0.015 sec! I've just replaced four loops with one loop. Probably depends on server current loading. | | It can be solve in O(N+M) | Ted | 1126. Magnetic Storms | 8 Jan 2017 01:27 | 9 | I'm not good at English. A[I] is the Ith number. First,try to count a array F. F[I] means the nearest number A[F[I]]: A[F[I]]>A[I] and F[I]>I. You can count F in O(N). Find largest number in [L,R],just find a min I,F[I]>R. So you can solve the problem in O(N+M). And this problem is RMQ problem, also have a very hard standard solution in O(N+M). Edited by author 07.05.2004 20:31 Good idea. But I had to be very carefully solving it. Edited by author 04.05.2006 20:00 Thanks, @Ted. Your idea is brilliant. I count F[] array with O(N) , used stack. + buffered i/o ==> result is very good: AC. 0.001s There is an interesting situation here: when I declared arrays a and f with size 1..25000, I got WA#2. When I changed them to 1..25001, I got AC... I have used rather simple method, I have organized four stacks, first two to make a queue of M size, and others to store max elements of queue, by this way you can find elements in constant time. Very useful for Java, you have Class Stack. I have used rather simple method, I have organized four stacks, first two to make a queue of M size, and others to store max elements of queue, by this way you can find elements in constant time. Very useful for Java, you have Class Stack. Thanks for sharing the idea... :) | | There exists O(K^2) solution!!! | c_pp | 1119. Metro | 7 Jan 2017 21:27 | 1 | |
|
|