Общий форумWhy there so strict timelimit. I solved this problem in OpenCup (where LT is 2 s). But for solve it there I need to make some optimization. Is it possible to increse timelimit to 2sec ? My solution is O(NlogN). Edited by author 01.04.2008 15:17 I also needed some minor optimizations to pass 1616 here which was accepted in OpenCup. AC 0.078 sec. vector< vector<int> > and vector<int> get to big time ... I just delete edges with O(NlogN), but I understand how to do it with O(N). But I know O(M) solution for general but this solution don't use that we work with "cactus", I used it so have O(NlogN) solution. Who can tell me How to solve with O(n)? Just one DFS - 0.171 sec. even on vector<int> g[50000] Could somebody provide some tricky test cases? Edit: Or maybe provide TC 3 or 10? Edited by author 17.04.2011 09:46 can you give me some tests i've got 'wrong answer' for several times, thank you very much! So the problems says that 1 <= N <= 100, so I declare a nice unsigned char mem[100*100]. I get WA on test 4. I go and read all the discussion about wrong awsers but none of those offer anything I didn't figure out already. I change the declaration to unsigned char mem[256*256] and voila, the solution is magically accepted. Does anyone have an idea why 100*100 bytes aren't enough? I wrote a solution in Pascal (ages ago :-)), which AFAIR checks whether index is in range while accessing to some element in array. And I had [1..100,1..100]. That is why I suppose that somewhere in your program you access out of array. 100x100 is enough, assuming you start your indexing at 0 and end at n-1 what the fuck is test#4? y don't u just post the content of those fucking tests? Can anyone explain the DP algorithm logic posted for this problem through examples?? If you want to get AC with DP, you can gain inspiration by reading about knapsack problem on wikipedia. P.S. I suggest that you solve this problem without DP first. It has simple solution, that is why it got "problem for beginners" tag. Hi Artem Khizha I have done some brute force code and i am able to get the answers but when i submit it i get WA #1 but i get the correct answer on my compiler. This is the code i have posted, //timus ru programming problems //stone pile #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> //void even_diff(int [],int); //#define N 100000 int main(){ //clrscr(); int n; int i,j; int temp=0; int sum_l=0,sum_r=0; long cur_sum=0; long old_sum=0; long stones[100000];
//printf("\nenter n:"); scanf("%d",&n);
//printf("\nenter wts: ");
for(i=0;i<n;i++) scanf(" %ld",&stones[i]);
for(i=0;i<n;i++) cur_sum+=stones[i]; old_sum=cur_sum; cur_sum=cur_sum/2; if(n==1) printf("\n%ld",stones[0]); else if(n==2) printf("\n%ld",abs(stones[0]-stones[1])); else{
for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(stones[i]<stones[j]){ temp=stones[i]; stones[i]=stones[j]; stones[j]=temp; } } } sum_l=stones[0]; sum_r=stones[1];
for(i=2;i<n;i++){ if(sum_l<sum_r){ if(sum_l+stones[i]<=cur_sum){ sum_l+=stones[i]; } } else sum_r+=stones[i]; } if(old_sum%2==0) printf("\n%ld",abs(sum_l+sum_r-(2*cur_sum))); else printf("\n%ld",abs(sum_l-sum_r));
getch(); return 1;
} Edited by author 08.07.2012 19:03 Any help please?? Is it actually a Time Limit Exceeded error?? I dont understand!! Try this test: > 6 > 1 4 5 6 7 9 It will show you, why you get WA#1 first, and then you will see that your approach leads to WA#5. :-) And yes, it is not guaranteed that 1st test is the one from the statement. Try this test: > 6 > 1 4 5 6 7 9 It will show you, why you get WA#1 first, and then you will see that your approach leads to WA#5. :-) And yes, it is not guaranteed that 1st test is the one from the statement. Hi Artem I get 0 as the answer as (9+7)=16 and (1+4+5+6)=16 Is it not correct?? or does this correct answer lead to a wrong one for further tests? What is WA#5? So the first test need not be the sample given the problem, thanks for the info Oh, I forgot that I run your solution first on: > 4 > 1 3 9 27 Actually, I just do not understand the answer you print in cases of even N. :-) P.S. I cannot prove that sorting doesn't help here, but don't be surprised if it really doesn't. :-) Edited by author 08.07.2012 21:40 Hi Artem i get 0 for this which is wrong, This is because for even sum i print left+right-2*sum. I did this because for even sums i thought the answer would be 0 as most of the cases i tried were like that eg:3,1,1,1 total sum=6 here first half of the sum is 3 and next half is 1+1+1=3,so i add 3 and 1+1+1=3 and finally i subtract from 2*total sum/2. It is a wrong process as shown by the test case you have given. I have to think of another way now!! Hi Artem I have modified the code and now i am getting the proper answer for your test case: 27,9,1,3 the min diff is 14 this is the code #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> //void even_diff(int [],int); //#define N 100000 #define TRUE 1 #define FALSE 0 int main(){ //clrscr(); int n; int i,j; int temp=0; long sum_l=0,sum_r=0; long cur_sum=0; long old_sum=0; long stones[100000]; int left_out=0; int isLeftOut;
//printf("\nenter n:"); scanf("%d",&n);
//printf("\nenter wts: ");
for(i=0;i<n;i++) scanf(" %ld",&stones[i]);
for(i=0;i<n;i++) cur_sum+=stones[i]; old_sum=cur_sum; cur_sum=cur_sum/2; //printf("\ncur_sum=%ld",cur_sum); if(n==1) printf("\n%ld",stones[0]); else if(n==2) printf("\n%ld",abs(stones[0]-stones[1])); else{
for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(stones[i]<stones[j]){ temp=stones[i]; stones[i]=stones[j]; stones[j]=temp; } } } sum_l=stones[0]; sum_r=stones[1];
isLeftOut=FALSE; for(i=2;i<n;i++){ if(sum_l+stones[i]<=cur_sum) sum_l+=stones[i]; else if(sum_r+stones[i]<=cur_sum) sum_r+=stones[i]; else{ left_out=stones[i]; isLeftOut=TRUE; } } /*if(old_sum%2==0) printf("\n%ld",abs(sum_l+sum_r-(2*cur_sum))); else*/ if(isLeftOut) printf("\n%ld",abs(sum_l-sum_r-left_out));
else printf("\n%ld",abs(sum_l-sum_r));
} //printf("\nsum_l=%ld sum_r = %ld",sum_l,sum_r);
getch(); return 1;
} I am getting WA#5. I dont know what more i should do as i feel i have taken a long long time to do this simple problem!!! All polynomial algorithms are wrong, don't even try to write them, WA will be yours. Well, as I said, I don't know how to prove that your solution is wrong. But I know that you can get AC in two different ways: with backtracking in O(2^N) (quite easily) and with DP in O(N*W) (a bit trickier). Thank you Artem for your help So it must be done either with backtracking or DP, gues ill have to start learning these two techniques for this problem Backtracking (with memo-ization) and DP are two techniques that make the 'try all possible combinations" more efficient, but they're still "try all combinations" solutions. For problem sizes like this one ( 1 <= N <= 20 ), explicitly enumerating and trying all possible partitions runs pretty quickly. Adding together 20 numbers a million times is nothing for a 3GHz processor. This is my code, it works good on the sample test, but I get WA1 #include <stdio.h> unsigned int w[20], s, i, n, max, h, j, k, r[20], l[20]; bool a[50001]={1}, u[50001][20]; int main (void){ scanf("%u", &n); for(i = 0; i < n; i++){ scanf("%u", &w[i]); h += w[i]; } for(i = 1; i <= h/2; i++) for(j = 0; j < n; j++){ if(u[i - w[j]][j] == 0 && a[i - w[j]] && i - w[j] >= 0){ a[i]++; for(k = 0; k < n; k++) u[i][k] = u[i-w[j]][k]; u[i][j]++; max = i; j = n; } } printf("%i", h - 2*max); return 0; } It's not clear what your algorithm is (all the single letter variables make for unreadable code), but to quote from another thread about this question, "all polynomial time algorithms are wrong." You must consider all possible combinations, because otherwise there will be an input somewhere that will cause your solution to fail. horizontally of vertically in any direction -> horizontally or vertically in any direction i'm constantly getting the wrong answer while my answers are right as far as i can calculate them What is the test 8? if you figger out this test please reply? what was reason of wrong answer? What is the test 8? import java.io.*; public class D { public static void main (String args []) throws Exception { BufferedReader d = new BufferedReader(new InputStreamReader(System.in)); String v = d.readLine(); BufferedReader e = new BufferedReader(new InputStreamReader(System.in)); String c = e.readLine(); try { int a = Integer.parseInt(v); int b = Integer.parseInt(c); System.out.print(a+" ");System.out.println(b); System.out.print(b-1+" ");System.out.print(a-1); } catch(Exception e1) {System.out.println("Error translating text to int");} System.out.println(); } } I do smth wrong? Read FAQ how to input numbers Read FAQ how to output answer You used Java, that's what's wrong. Friendoship!!! What is wrong???? #include <math.h> #include <stdio.h> int main(){ int larry, herry,sum; scanf ("%i %i", &larry, &herry); sum=larry+herry; if (sum==11){ printf ("%i %i", herry-1, larry-1); } else printf ("wrong"); return 0; } Because your output only supports the example given in the problem statement. If the sum is NOT 11, your program will fail. Eliminate the "if", and use your first printf. Разрешается ли набирать высоту за время меньшее чем t? No comments Почему в ваших тестах при максимальной скорости подъёма в 50, вы поднимаетесь со скоростью в 80? (тест первый, другие не знаю) No comments Why no comments? :) Because questions either have no sense or have answers in statement. почаму в тесте минималка 125 секунд как так ? мин же будет 200 поделитесь кто нибудь своим опытом The key is noticing that the plane may change speeds, as noted in the problem, and changes speeds instantly. Edited by author 29.10.2011 13:54 Edited by author 29.10.2011 13:54 Edited by author 29.10.2011 13:55 #include<iostream> #include<stdio.h> #include<math.h> using namespace std; double distance(double x1, double y1, double x2, double y2); int main(){ int n ; double r; cin>>n>>r; double x1,y1,x2,y2; cin>>x1>>y1; cin>>x2>>y2; double ans = 0; //float pi = (float)22/(float)7; double pi=2*acos(0.0); //cout<<pi<<endl; ans = distance(x1,y1,x2,y2); //cout<<ans<<endl; int temp = n; temp = temp-2; double x3,y3; x3=x2; y3=y2; while(temp>0){ x2=x3; y2=y3; cin>>x3>>y3; ans = ans + distance(x2,y2,x3,y3); // cout<<ans<<endl; temp--; } ans = ans + distance(x1,y1,x3,y3); //distance of last and first point double con = 2*pi*r; ans = ans + con; cout.precision(2); cout<<fixed<<ans<<endl; return 0; } double distance(double x1, double y1, double x2, double y2){ x1 = x1-x2; y1 = y1-y2; x1 = x1*x1; y1 = y1*y1; double sum = x1+y1; sum = sqrt(sum); return sum; } try this test 1 1 0.0 0.0 //float pi = (float)22/(float)7; - lol :-)))) Thanks :) I was trying all sorts of things actually with the value of pi lol :P What's the expected output for this? 1 1 0.0 0.0 but i've trid this still its wrong. even it gives wrong on test 1 use array of N queues Edited by author 25.08.2011 19:25 kkk, i figured out about the LCMs pretty much myself, but i just keep getting WAs on tests like 4 or 3 and that's sooo embarassing <code> #include <stdio.h> #include <cmath> #include <set> using namespace std; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a,b) * b); } int main() {
int n;
scanf("%d", &n);
set<int> ds; // a usual stl set which stores the distaces of all elements int tmp; for(int i=1;i<n+1;i++) { scanf("%d", &tmp); ds.insert(abs(i-tmp)); }
long long res; if(ds.size() == 1 && ds.count(0)) res = 1; else { if(ds.count(0)) ds.erase(ds.find(0)); res = 1; for (set<int>::iterator it = ds.begin(); it != ds.end(); ++it) { res = lcm(res,*it); } }
printf("%lld", res);
} </code> And the worst thing is I just can't think of a straightforward test for which that gives a wrong answer. 5 4 1 5 2 3 => 6 1 1 => 1 5 1 2 3 4 5 => 1 6 6 5 3 4 2 1 => 15 didn't check for ns like 100 and i don't think that's the problem. also i don't think the problem is about malformed longlong operation, cause if i replace that with ints the WAs are all the same. would really like if someone helped me 'bout that (( Edited by author 28.11.2011 19:38 "Every problem has a simple, fast and wrong solution." - that one is soooo right :D i mean, i came up with the distances of the items, but those actually should be the lengths of cycles )) it was like 4 1 5 2 3 => 3 2 1 2 2 but should be like 4 1 5 2 3 => 3 3 2 3 2 - and it's really straightforward to figure out how it works :D the worst thing about my initial solution is that it worked in some trivial cases :) now i made something like that: [code deleted] and finally got AC after 23 WAs :) so happy 'bout that Edited by moderator 04.12.2019 20:47 Look out for left over debug prints! I was tearing my hair out over WAs after I figured out doing the LCM of all the unique cycle counts and was convinced the approach was correct, and I was right! But one left over debug print messed me up until I finally saw it. Here is my own testing program ... Now tell me please what is wrong here??? N ==0 Q == 10 N ==1 Q == 1 N ==2 Q == 2 N ==3 Q == 3 N ==4 Q == 4 N ==5 Q == 5 N ==6 Q == 6 N ==7 Q == 7 N ==8 Q == 8 N ==9 Q == 9 N ==10 Q == 25 N == 11 -1 N ==12 Q == 26 N == 13 -1 N ==14 Q == 27 N ==15 Q == 35 N ==16 Q == 28 N == 17 -1 N ==18 Q == 29 N == 19 -1 N ==20 Q == 45 N ==21 Q == 37 N == 22 -1 N == 23 -1 N ==24 Q == 38 N ==25 Q == 55 N == 26 -1 N ==27 Q == 39 N ==28 Q == 47 N == 29 -1 N ==30 Q == 56 N == 31 -1 N ==32 Q == 48 N == 33 -1 N == 34 -1 N ==35 Q == 57 N ==36 Q == 49 N == 37 -1 N == 38 -1 N == 39 -1 N ==40 Q == 58 N == 41 -1 N ==42 Q == 67 N == 43 -1 N == 44 -1 N ==45 Q == 59 N == 46 -1 N == 47 -1 N ==48 Q == 68 N ==49 Q == 77 N ==50 Q == 255 N == 51 -1 N == 52 -1 N == 53 -1 N ==54 Q == 69 N == 55 -1 N ==56 Q == 78 N == 57 -1 N == 58 -1 N == 59 -1 N ==60 Q == 256 N == 61 -1 N == 62 -1 N ==63 Q == 79 N ==64 Q == 88 N == 65 -1 N == 66 -1 N == 67 -1 N == 68 -1 N == 69 -1 N ==70 Q == 257 N == 71 -1 N ==72 Q == 89 N == 73 -1 N == 74 -1 N ==75 Q == 355 N == 76 -1 N == 77 -1 N == 78 -1 N == 79 -1 N ==80 Q == 258 N ==81 Q == 99 N == 82 -1 N == 83 -1 N ==84 Q == 267 N == 85 -1 N == 86 -1 N == 87 -1 N == 88 -1 N == 89 -1 N ==90 Q == 259 N == 91 -1 N == 92 -1 N == 93 -1 N == 94 -1 N == 95 -1 N ==96 Q == 268 N == 97 -1 N ==98 Q == 277 N == 99 -1 N ==100 Q == 455 N == 101 -1 N == 102 -1 N == 103 -1 N == 104 -1 N ==105 Q == 357 N == 106 -1 N == 107 -1 N ==108 Q == 269 this program fails (WA) at test 8 this program shouldn't fail... Edited by author 23.11.2010 22:39 #include<iostream> int main() {
int a, b; bool t = true; std::cin >> a; if(a < 0 || a > 109){ std::cin >> a; } int k = 6, j = 9, i = 9, test = 4; while(test >= 1){ k = 5; while(k >= 1){ i = 9; while(i >= 2){ j = 9; while(j >= 2){ if(test != 1){ if(test * j * i * k == a){ b = (test*1000) + (k*100) + (i * 10) + (j); t = false; } }else if(k != 1 && test == 1){ if(j * i * k == a){ b = (k*100) + (i * 10) + (j); t = false; } }else if(k == 1 && test == 1){ if(j * i == a){ b = (i*10) + j; //std::cout <<"i == "<<(i)<< " + j == " << j << '\n'; t = false; } } j--; } i--; } k--; }test--; } if(a < 10){ if(a==0){ b = 10; t = false; }else if(a>=1){ b = a; t = false; } } if(t == false){ std::cout << b << '\n'; }else{ std::cout << -1 << '\n'; } return 0; } Edited by author 23.11.2010 23:07 In fact i don't really know C but i had the same problem on test 8 and the reason was that i needed bigger in (in pascal int64). Thanks. try test 244140625 its answer is 555555555555 I think the limit is 0<= N <= (10^9) not 109 Can anybody help me? I have the same WA :( Help anybody, please ! :D n=1. Good luck if (n == 1) { double ffffuuuu = in.nextDouble(); ffffuuuu = in.nextDouble(); out.println((double) (int) (2 * radius * PI * 100) / 100.0); out.flush(); return; } в чём ошибка!!! what is wrong!!! Edited by author 10.09.2012 19:38 try test: 1 2 0.0 0.0 length is 4*PI = 12.56637061.. so right answer is 12.57 error is found - the number of rounds my code do not have compile error, but it still can not work, I do not know where went wrong. import java.lang.*; import java.util.*; /** * * @author manyang */ public class Timus_1001 { //create a arraylist from input //donot konw how to play with arraylist, use a array public static void main(String[] args){ double num[] = new double[10000];//10000 is not precise, should determined by 256kb Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int i = 0; double result = Math.sqrt(sc.nextDouble()); num[i] = result; i++; } /*for arraylist, use arrayName.length output reversely calc each for array, use num[i] */ for (int i = num.length - 1; i >=0; i--) {
System.out.printf("%.4f", num[i]); System.out.println(); } }
}
My AC solution gives wrong answer on this test: 5 100 30 50 30 90 Right answer: 0 what is in the test #7? here is my program var n,k:longint; begin readln(n,k); if (n<=k) then write(2*n) else if (n mod k=0) then write(trunc(2*n/k)) else write(trunc(1+(n-(n mod k))/k+((n-abs(k-(n mod k)))-((n-abs(k-(n mod k))) mod k))/k+((n-abs(k-(n mod k))) mod k)/k)); end. my sotution is WA#7 too. help me,pls |
|