Общий форумHello, I often want to brag about my accomplishments at acm.timus.ru, I want to show how many problems I have solved and that I'm one of the most geeky participant of Timus. I very much like the way projecteuler encourages boasting, it has a profile with level and number of problems solved. In the end, such a feature would benefit everyone: Timus users can boast, Timus itself gets more visibility and new participants. # include <iostream> # include <cstdio> using namespace std; const int Maxn = 130012; int n, ans1, ans2, L1, L2, x, p[Maxn], q1, q2; int GCD (int a, int b){
if (b > a) return GCD(b, a); else if (b == 0) return a; else return GCD(b, a % b); } int main () { freopen ("1.txt", "r", stdin); freopen ("2.txt", "w", stdout); cin>>n; for (int i = 1; i <= n; i++){ cin>>x; p[x] = i; } q1 = 0; q2 = n + 1; ans1 = 0; ans2 = 0;
for (int i = 1; i <= Maxn; i++){ if (p[i] > 0) { q1 ++; q2 --; L1 = p[i] - q1; if (L1 < 0) L1 = -L1; L2 = p[i] - q2; if (L2 < 0) L2 = -L2; if (L1 > 0) if (ans1 > 0) ans1 = GCD(ans1, L1); else ans1 = L1; if (L2 > 0) if (ans2 > 0) ans2 = GCD(ans2, L2); else ans2 = L2;
}
} if (ans1 == 0) ans1 = n - 1; else ans1 --; if (ans2 == 0) ans2 = n - 1; else ans2 --; if (ans1 > ans2) cout<<ans1; else cout<<ans2; return 0; } Edited by author 19.09.2012 17:23 1) число может быть с ведущими нулями 2) выводить надо с ведущими нулями(если они есть) короче вот тест 1) 0 000000000002 2) 2 02 020000000089 AC code? Put here.. I hope nobody will put. Forum is not good place to post right solutions ;) You can write your email here... Btw, and did you try to solve|submit it yourself? Money? Put here... +7 926 153 72 30 my solution : for (i = 1; i<=n; i++) printf("%d ",i); for (i = n; i > 1; i--) printf("%d ",i); had ac... but it's wrong!! Edited by author 18.09.2012 18:57 Edited by author 18.09.2012 18:57 it is not necessary, it was comparing the real numbers find out such x that 2^x < n < 2^(x+1) 2^x means 2*2*...*2 x times x=log(n)/log(2);//c++ the maximum value of a[i] i=1,2,...,2^x is max=f(x); where f(x) is the fibonacci sequence f(0)=1 f(1)=1 f(n)=f(n-2)+f(n-1) so i just have to look if there is a larger number from 2^x to n it is easy to find out the value of a[i] without knowing a[1]...a[i-1] if l=2*k and r=2*p m=(l+r)/2 a[m]=a[l]+a[r]; it is rather easy to prove that a[2^x]=1; a[2^(x+1)]=1; so you just have to do a binary search for i from 2^x to 2^(x+1) calculating the values for the left and right border this is the function that does exactly that //c++ int b(long l,long r,long i,int v1,int v2) { long m=(l+r)/2; if (i==m) return (v1+v2); if (i<m) return j(l,m,i,v1,v1+v2); return j(m,r,i,v1+v2,v2); } I DON'T UNDERSTAND WHY, BUT WHEN I USE MATH.H I GET COMPILATION ERROR good luck !!! You have to use math instead of math.h ;-) Manastireanu, thank you for your ideas, they pushed me to more deeply study the sequence. A few comments: > if l=2*k and r=2*p, m=(l+r)/2, a[m]=a[l]+a[r]; This is not true for all k and p, e.g., k=4 and p=7. It is true starting with l=2^x and r = 2^(x+1), m=(l+r)/2 and then recursing through more (l,r) with (l, m) and (m, r). There are many interesting properties of this sequence, I encourage everyone to study it and to discover the patterns which will help solve this problem without "brute force", and will help with Maximum2 Hi Experts, Can you explain the sample test case in problem? Thanks in advance Anupam try this test 2 11.02.2000 12.02.2000 11.02.2001 09.02.2001 11.02.2001 10.02.2002 ----------- 1 :) Now AC, I had stupid mistake. I have submitted for about 100 times with my (another) ID and finally get AC. Here I want to say something about the problem. 1. Is this problem easy? Yes, very much. I think this problem could be regarded as 'PROBLEM FOR BEGINNERS'. BTW, I think after reading my note, EVERYONE will be able to solve the problem. 2. What does the chessboard look like? The chessboard is a common 8*8-board. The coordinates are marked with a~h(lowercase) and 1~8. 'a1' is a black cell. 3. How many players are there? EXACTLY TWO. 4. Are all the checkers put on the black cell of the board? Yes. 5. What does it mean by JUMPING OVER? B could JUMPING OVER A, if A is at one of the four corners of B. (Use coordinates, (-1, -1), (-1, 1), (1, -1), (1, 1)). Do NOT think there are far-away jumpings, such as 'a1' jumps over 'c3'. 6. It says that, after jumping over, the checker mustn't fall over the board, is it right? Yes! 7. It says that, after jumping over, the checker must be located at a FREE cell. What does FREE mean? It means EMPTY cell, a cell without ANYTHING (and, within the board boundary). 8. What does it mean by "ONE OF THE OPPONENTS" gets the opportunity? Let us call the two players Alice and Bob. After Bob, for example, makes a move, if one of Alice's checker could fell one of Bob's checker, OR one of Bob's checker could fell one of Alice's checker, we think that "ONE OF THE OPPONENTS" have the opportunity. WARNING: In BOTH cases above, it was Bob who will lose the game at once. 9. Are there mistakes in the test data? No. There are no checkers put outside the board (such as a9 or g3), and no two checkers at the same place. 10. Could the output be '32'? Yes. You can construct the case yourself. 11. Could the output be 'Draw'? Yes. You can construct the case yourself. 12. Why did the author of this note use nearly 100 times to get AC? Maybe something was wrong with him. Don't be like him :) At last, we take a look at some cases: (1) b2 a1 (Some other 30 moves) Answer should be 2: Although 'a1' could not be felled by 'b2' (Question 6), but 'b2' could be felled by 'a1', so the second player loses at once (Question 8). (2) a1 d4 b2 c3 (Some other 28 moves) In this case, we could NOT judge the result with the first 4 moves (Questions 7 & 8). (3) a1 h8 a3 h6 a5 h4 a7 h2 b2 g7 b4 g5 b6 g3 b8 g1 c1 f8 c3 f6 c5 f4 c7 f2 d2 e7 d4 e5 d6 e3 d8 e1 It's a 'Draw' game (Questions 6, 7 & 8). So, Good luck for EVERYONE. Edited by author 16.09.2012 22:37 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? |
|