Общий форум#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 I feel wonderful.:) 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 mine is 0.015 or better 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 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. 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... :) I get Wa6. I don't know my error. Give me some test please. Edited by author 07.01.2017 13:53 my first attempt based on partitions // 1 2 // 3 // 7 // 6 4 // 8 // 5 gave WA1, is there any kind of tricks in computing smallest group? or Don`t they use sample as a test 1? sorry 2 cant be in the same group with 3 and 7, did not count that moment in my solution I write Pascal a few day's. I not understand and nor find my error. Please help! My code: function M(a,b:integer):integer ; begin if a > b then result := a else result := b end;
procedure update(t: array of integer; v, tl, tr, pos, new_val: integer) ; var tm: integer; begin if tl = tr then t[v] := new_val else begin tm := (tl+tr) div 2; if pos <= tm then update (t,v*2, tl, tm, pos, new_val) else update (t,v*2+1, tm+1, tr, pos, new_val); t[v] := M(t[v*2], t[2*v+1]); end; end;
var u, j, a, i, flag: integer; f : string; t : array of integer;
begin readln(u); SetLength(t,25001); for j:=1 to 25000 do t[j] := -1; i := 1; flag := 0; WHILE true do begin readln(a); if a = -1 then break; update(t, 2, 1, u, i, a); i := i + 1; if i = u+1 then flag := 1 ; if i = u+1 then i := 1 ; if flag = 1 then writeln(t[2]); end; end. Please show your program answer on task example. 11 11 10 0 1 2 3 3 You missed "var" here: procedure update(var t: array of integer; v, tl, tr, pos, new_val: integer) ; Your local compiler is weird a bit if your program works as is. What is it? Thanks! I got AC. I used Pascal ABC. Edited by author 07.03.2010 10:26 Hi, Alright guys I have solved the problem. Being a newbie to c# caused me to submit program 35 times. I didn't realize that Math.Round() function will change 100.00 to 100 instead. Here are the lines which I changed. double miles = distance(ship_latitude_radian, ship_longitude_radian, iceberg_latitude_radian, iceberg_longitude_radian); if (Math.Round(miles, 2) < 100.00) { Console.WriteLine("The distance to the iceberg: {0:F2} miles.", miles); Console.WriteLine("DANGER!"); } else { Console.WriteLine("The distance to the iceberg: {0:F2} miles.", miles); } Solved by making the following comparison in java: if (Math.round(distance*100) < 10000) { System.out.println("DANGER!"); } it will force it to compare with 2 digits precision. I would like some tests, in particular test #3. Thank you. All of the test cases invented by myself pass however I have got TLE on test #14. I would like some of these difficult tests to see. Thank you. Could anyone provide me with an O(n) solution? My works O(n log(n)) 'n' is the number of test cases and 'res' it's a function that print the number of positions that can go. The problem is that it only reads one case, for example "a2", prints the result and ends the execution. Why that happen? void res(char p[2]); int main () { int i,n; char pos[2]; scanf("%d", &n); for(i=0; i<n; i++) { scanf("%s", &pos); res(pos); } Edited by author 05.01.2017 08:22 You need at least 3 chars for pos - for ending zero char. Probably it ruined n during first "scanf("%s", &pos);" What's the difference in memory or similar between the next two codes when i create a new array, cause in the first case (array inside main) the message of jugde is "Run time error/Stack overflow" and in the second case (array outside main) the jugde accept the code: Case 1: #include <stdio.h> #include <math.h> int main(){ double array[128*1024]; int i = 0; unsigned long long n; while (scanf("%llu", &n) != EOF) { array[i++] = sqrt(n); } //Code for print...... Case 2: #include <stdio.h> #include <math.h> double array[128*1024]; int main(){ int i = 0; unsigned long long n; while (scanf("%llu", &n) != EOF) { array[i++] = sqrt(n); } //Code for print...... In example 1 array is allocated on stack, in example 2 - on global memory http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c My opinion: - stack is small; - stack size is unpredictable. Conclusion: there shouldn't be big variables on stack. There shouldn't be recursion with more than logarithm complexity. If recursion depth is above 100 then it isn't usable for practical purpose. Note: STL containers aren't big despite dozens of elements inside. They use heap to hold elements and don't spend too much stack size. So here is ok: int main() { std::vector<double> array(128*1024); ... Thank you very much for the clarification..and link of course. I don't understand what's wrong with my code #include <iostream> using namespace std; int main() { int n,i,mark; double t,coun; coun=0.0; cin >> n; for (i=0; i<n; i++) { cin >> mark; if (mark==3) t=0; coun+=mark; } if (t) {t=coun/n;} if (!t) { cout << "None";} if (t == 5) { cout << "Named"; } else if (t >= 4.5) { cout << "High"; } else if (t != 0) { cout << "Common"; } return 0; } May be u have WA2 in this case: 1 1 output have to be: NO u need to find out can u from 1 get N in odd number of this operations : multiply number by odd number or 2 There is about nmax*nmax*smax=40 844 804 iterations and time must be about 0.1,0.2 sec. But program works >0.5 sec. How I can to optimize my code? const nmax=101; kmax=4; smax=4004; type myint=0..smax+1; var ans:array[1..nmax,1..nmax] of boolean; count,s,t:myint; n,m,i,j,k,x,y,ii,jj:myint; command:array[1..nmax,1..nmax,1..kmax,1..2] of byte; memory:array[1..nmax,1..nmax,1..smax] of boolean; list:array[1..smax] of byte;
begin readln(n,m);
for k:=1 to kmax do begin for i:=1 to n do begin for j:=1 to m do read(command[i,j,k,1],command[i,j,k,2]); end; end;
read(s); for i:=1 to s do read(list[i]);
for t:=1 to s do begin for i:=1 to n do begin for j:=1 to m do memory[i,j,t]:=false; end; end;
count:=0; for i:=1 to n do begin for j:=1 to m do begin ans[i,j]:=false; end; end;
for i:=1 to n do begin for j:=1 to m do begin t:=1; ii:=i; jj:=j; while(t<=s)and(not memory[ii,jj,t]) do begin memory[ii,jj,t]:=true; x:=command[ii,jj,list[t],1]; y:=command[ii,jj,list[t],2]; ii:=x; jj:=y; inc(t); end; if(t>s)and(not ans[ii,jj]) then begin inc(count); ans[ii,jj]:=true; end; end; end;
writeln(count); for i:=1 to n do begin for j:=1 to m do begin if(ans[i,j]) then writeln(i,' ',j); end; end; end. Edited by author 04.01.2017 23:24 Now AC. When I used a lot of memory, I got TL32. When I used a little memory, I got AC.Perhaps the cache helped. Edited by author 23.06.2017 19:24 |
|