Show all threads Hide all threads Show all messages Hide all messages | Page 8 | (input clarification) | M.Amini | 1005. Stone Pile | 10 Aug 2011 04:48 | 2 | In this problem, the example input had these lines : 5 5 8 13 27 14 I am a bit confused about the input will be inserted to the console ? Can i assume that : the first line is inserted and then (after hitting Enter), the second is inserted ? and after that the output is produced ? Thanks a lot you may assume you read from console as from file, but using console input | solved | Tom | 1005. Stone Pile | 29 Oct 2011 14:13 | 4 | Edited by author 05.08.2011 19:30 thank you. i meant that i solved my problem this post was about actually Re: solved Anupam Ghosh, Wipro Technologies 29 Oct 2011 14:13 | Why I ma getting wrong answer | Jai | 1005. Stone Pile | 5 Aug 2011 01:48 | 1 | | can anyone tell me what's wrong with this | Gumyamy | 1005. Stone Pile | 2 Aug 2011 00:55 | 1 | #include<stdio.h> #include<algorithm> using namespace std; int pile[22];//initial array size int pile_cmp(const void*a,const void *b) { return *(int*)a-*(int*)b; } int main() { int num; scanf("%d",&num); int sum=0; for(int i=0;i<num;i++){ scanf("%d",&(pile[i])); sum+=pile[i]; } if(sum==0)printf("0\n"); else{ qsort(pile,num,sizeof(int),pile_cmp); int ptr=num-1,min=2000000,min_p,tmp; while( ptr>=0 && sum ){ while(ptr>=0 && (sum-(pile[ptr]<<1))<0)ptr--; if(ptr<0)ptr++; tmp=pile[ptr]<<1; if(abs(sum-tmp)<min){ sum=abs(sum-tmp); min=sum; } ptr--; } printf("%d\n",min); } return 0; } before i submit this ,i submit another one ,which got AC but produce different answer with this one;however this one also got AC I wasn't quite clear about what is the correct algorithm for this problem Edited by author 02.08.2011 00:58 Edited by author 02.08.2011 01:05 | am i idiot? | Amir Aupov | 1005. Stone Pile | 22 Jul 2011 18:15 | 2 | #include <stdio.h> int main () { printf("3"); return 0; } this gives me WA #1. test #1 isn't from the example, is it? obviously, if you have wa#1 | Why my prog Crash? | Georgeek | 1005. Stone Pile | 30 Jun 2011 16:02 | 1 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class PileOfStones { private static int sum = 0; private static int minSum = 0; private static int myMinSum; private static int min; private static int m=0; private static int error = 0;
public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int pl = Integer.parseInt(in.readLine()); if(pl>=1 && pl<=20){ int[] stones = new int[pl]; int k = stones.length-1; Scanner sc1=new Scanner(System.in); for (int i = 0; i < pl; i++) { stones[i] = sc1.nextInt(); if(stones[i]<1 || stones[i]>100000){ error =1; break; } } if(error!=1){ for (int i = 0; i < stones.length; i++) { sum+=stones[i]; } Arrays.sort(stones); min=sum; while(k>0){ for (int i = k; i >= 0; i--) { if((stones[i] + minSum)<=(sum/2)){ minSum+=stones[i]; k=i; } } if((sum/2-minSum)<=min ){ myMinSum=minSum; } minSum-=stones[k]; if(k!=m){ m=k; } else{ k=-1; } } System.out.println(sum-2*myMinSum); } }
}
} | Accepted whis wrong answer | Actics | 1005. Stone Pile | 29 Jun 2011 04:25 | 1 | See, this is my code. For 4 stone (48 22 27 3) he said: 4. But correct answer is 2! -------------------------------------------------- #include <stdio.h> #include <algorithm> using namespace std; int main() { int n, i, s=0, x, y=0, *m; scanf("%d", &n); m = new int[n]; for (i=0; i<n; i++) { scanf("%d", &m[i]); s += m[i]; } s /= 2; sort(m, m+n); x = m[n-1]; for (i=n-2; i>-1; i--) if (x+m[i] <= s) x += m[i]; else y += m[i]; printf("%d", (x>y)?(x-y):(y-x)); return 0; } Edited by author 29.06.2011 04:25 | why wa #1?? | huangchen | 1005. Stone Pile | 14 Jun 2011 16:52 | 1 | #include <stdio.h> #include <math.h> int n;// the number of stones int w[21];//store the weight of stones int total;//the sum of all of the stones int ans = 100000000; //now is the sum of first piles //index is index of w void fun(int now,int index) { if (index == n)// the end if (ans > abs(total - 2 * now)) ans = abs(total - 2 * now); for (int i = index; i < n; i ++) if (now + w[i] < (total >> 1)) fun(now + w[i],i + 1); } int main(void) { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); #endif scanf("%d",&n); total = 0; for (int i = 0; i < n; i ++) { scanf("%d",&w[i]); total += w[i]; } fun(0,0); printf("%d\n",ans); return 0; } | What my solution is Time Limit Exceed? | bentham | 1005. Stone Pile | 17 May 2011 09:03 | 3 | Hello I have submitted my solution 2 times, and I get Time Limit Exceed, I dont understand why?, can someone help me, maybe its my loop, but I still dont understand, here its my code import java.io.*; import java.util.Arrays; public class StonePile { public static void main(String args[]) throws IOException { StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); //int diff = 0; str.nextToken(); int[] array = new int[(int) str.nval + 1]; int array2[] = new int[array.length - 1]; //System.out.println(array.length); int i = 0; int diff = 0; while (((str.nextToken() != StreamTokenizer.TT_EOL))) { if (str.ttype == StreamTokenizer.TT_NUMBER) { // System.out.println(i); array[i++] = (int) str.nval; } } for (int j = 0; j < array.length - 2; j++) { diff = Math.abs(array[j + 1] - array[j]); array2[j] = diff; // System.out.println("El array es en" + j+" es"+ array2[j]); } Arrays.sort(array2); System.out.println(array2[1]); } } may be there is no TT_EOL in input you need not to wait EOL or EOF because in this task input has a determined size. Read this: http://acm.timus.ru/help.aspx?topic=java. you can call pair of "str.nextToken(); (int)str.nval;" so many times how first number you readed, without any checking for EOL or EOF. after that you can get your WA ;) thanks for answer, so I still dont understand what the problem ask? in other thread it says that brute force search, can you explain me what the problem ask me to do, I need to generate permutation ?? I dont know much about algorithms this is my code import java.io.*; import java.util.Arrays; public class StonePile { public static void main(String args[]) throws IOException { StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); //int diff = 0; str.nextToken(); int[] array = new int[(int) str.nval]; int array2[] = new int[array.length]; //System.out.println(array.length); int i = 0; int diff = 0; //System.out.println(array.length); //System.out.println(array2.length); for (int k = 0; k <= array.length - 1 && str.nextToken() <= k; k++) { if (str.ttype == StreamTokenizer.TT_NUMBER) { //System.out.println(str.nval); array[i] = (int) str.nval; //System.out.println(array[i]); i++; } } for (int j = 0; j < array.length - 1; j++) { diff = Math.abs(array[j + 1] - array[j]); array2[j] = diff; //System.out.println("El array es en" + j+" es"+ array2[j]); } Arrays.sort(array2); System.out.println(array2[1]); } } Edited by author 18.05.2011 04:03 | Why? | fen | 1005. Stone Pile | 14 May 2011 18:31 | 1 | Why? fen 14 May 2011 18:31 #include<stdio.h> #include<math.h> #include<string.h> int dp[21][10000]; int w[21]; int sum,n; void beibao(int i,int x,int y) { dp[i][abs(x-y)]=abs(x-y); if(i+1>n)return; beibao(i+1,x+w[i+1],y); beibao(i+1,x,y+w[i+1]); } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",w+i); sum+=w[i]; } memset(dp,-1,sizeof(dp)); beibao(0,0,0); int min=2147483647; for(int i=0;i<=sum;i++) { if(dp[n][i]>=0) { if(min>dp[n][i]) { min=dp[n][i]; } } } printf("%d\n",min); return 0; } | I think the right algorithm for this problem is DP... | zxy_snow | 1005. Stone Pile | 11 May 2011 13:12 | 2 | 0-1 Knapsack, O(sum*n)...For the week data, I haven't got TLE... With 20 stones it can be solved with simple brute-force | I think program is ok, but I got WA#1 | Zashi | 1005. Stone Pile | 5 Oct 2011 02:15 | 2 | #include<iostream> using namespace std; int main(){ int n,y=100001; int x=0; cin >> n;
int *array = new int[n];
for( int i=0; i<n; i++ ) { cin >> array[i]; }
for (int i=0; i<n; i++) { x = (array[i+1] - array[i]); if(x < y && x >= 0) { y = x; } }
cout << y; system("PAUSE"); return 0; }
---------------------------------------------------------------- Why WA#1? I got 3 for: 5 5 8 13 27 14
So? Does someone know? | Has anyone solve this by dynamic programming? | phizaz | 1005. Stone Pile | 12 Dec 2011 22:50 | 3 | please tell me how do you? I found one algo but it uses memory equal to W<sub>i</sub> limitation. if maximum of W<sub>i</sub> is 100000 it uses 100000 too. Maybe it use 2 times more. Edited by author 05.04.2011 22:59 Hey, did you solve it with DP ? Please let me know how did you solve it ? Thanks | test #5 | lasha peradze [Tbilisi SU] | 1005. Stone Pile | 23 Mar 2011 01:47 | 1 | test #5 lasha peradze [Tbilisi SU] 23 Mar 2011 01:47 | can someone help me? plzzz | lasha peradze [Tbilisi SU] | 1005. Stone Pile | 22 Mar 2011 19:55 | 1 | #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> int p[20]; using namespace std; int main() { int a; int i,j,x,y,c; cin>>a; for(i=0; i<a; i++) cin>>p[i];
for (i=0; i<a; i++) for (j=i+1; j<a; j++) if (p[i]<p[j]) { c=p[j]; p[j]=p[i]; p[i]=c; }
if (a==1) cout<<p[0]; else if (a==2) cout<<abs(p[0]-p[1]); else x=p[0]; y=p[1]; for (i=2; i<a; i++) { if (x>y) y=y+p[i]; else x=x+p[i]; }
if(a>2) cout<<abs(x-y);
} what i have wrong? it sad "wrong answer" | where is my code wrong ? | Manish Patel | 1005. Stone Pile | 11 Feb 2011 23:32 | 1 | problem 1005 Edited by author 11.02.2011 23:32 Edited by author 11.02.2011 23:33 Edited by author 11.02.2011 23:33 i am sure that my program produces the right output but your compiler is showing wrong answer. my solution id is 3429148. Edited by author 11.02.2011 23:36 Edited by author 11.02.2011 23:36 | To admins | Hakobyan Tigran (RAU) | 1005. Stone Pile | 25 Feb 2011 14:00 | 2 | To admins Hakobyan Tigran (RAU) 9 Feb 2011 01:26 Why this solution got AC ? For the first test it outputs 15, but correct answer is 3 !!!! #include <iostream> #include <complex> #include <algorithm> #include <cmath> #include <vector> #include <string> using namespace std; int main() { int n,i,total=0,counter; int l; vector <int> x; cin>>n; for(i=0; i<n; i++) { cin>>l; x.push_back(l); } for(i=0; i<n; i++) { total+=x[i]; } counter=total/2; for(i=0; i<n; i++) { if(x[i]<=counter) { counter-=x[i]; } } cout<<2*counter+total%2; return 0; } | problem description has a mistake? | Takamoto | 1005. Stone Pile | 3 Feb 2011 22:27 | 2 | Hi, Maybe I misunderstood the problem. With the given numbers, you could actually make two piles 27,8 in one and 13,14,5,5 in the other, and the difference would be just 2. Where did I misunderstand? sorry, my mistake! the first number is actually the number of stones! | what algo to follow to solve this? hints please | Anupam Ghosh,Bengal Engg and Sc Uni,MtechIT,India | 1005. Stone Pile | 9 Nov 2013 18:50 | 19 | The bruteforce is only correct way to solve this problem you are to write function that will generate all permutations of(0,1) length n(where n-number of stones in pile) than you have calculate weight of all stones that you took in concrete permutation and find minimal difference between 2 piles. that's all sorry for poor eng Brute force is the easiest to implement here but not the only way. You can also develop a DP scheme to solve this problem, especially if you were given not 20 but, say, 100 stones... Could you explain, what does the abbreviation «DP» mean here, please? As usual, DP == Dynamic Programming Thank you so much for the hint. I did try with brute force. Not sure why I am getting #WA 1. Could you please kindly help me with some test cases? Hi All, Thank you for all your support and help. Just got AC with brute force method. regards Anupam I found i really fast and simple solution for this one. Main idea is: 1.Get 2 arrays(first one to store weights and sec to store 0 or 1 as includes in sum or not) 2.Find what sum would be best case 3.Go trough array and add to the sum until you get more than best sum 4.Now you need to make sum optimal,you do that this way: 1.If current sum is greater than best you remove one element that is closest to current sum - best sum but only if by doing that you get better solution than the one you've got before 2.If current sum is lest that optimal than you need to add one element that was not in sum and also you do that only if by doing that you get better solution than the one you've got before 5.If there were no changes in last pass you print result as abs(main sum - 2*current sum) Hope you like it :) i've got AC (0,015s and 212kb memory) using this algorithm Edited by author 27.01.2012 20:35 I dont think brute force would be the best approach. Because when the input is 20, maximum number of elements to be considered is 10. So that would leave us with something like this (20!)/(20-10)! = 20*19*18*17*16*15*14*13*12*11 I am not quite sure whether we would be able to check the last permutation (worst case) within the two seconds time limit. Please correct me if I am wrong.. Every stone can be in group A or in group B. Just brute every variant of every stone. O(2^N * N). Oops... I was wrong. On a second thought, we should not find the permutations but the combinations. But still that crosses 1.5 millions... :( Accepted 0.015 120 KB start: for each stone put stone in the other pile if the difference is greater than before then put it back else let it stay in this pile end for if the difference is smaller than before the for loop then goto start (do another pass) make sure that you don't get an infinite loop. it happened and I got TLE at test 2 Edited by author 15.06.2012 18:22 Edited by author 15.06.2012 18:22 Hm...I developed another solution, but don't know why it is wrong. "Put" means add to the sum, like: left += element; 1. Sort stones in their weight from bigger to lower. 2. If there is only 1 stone - it is an answer. 3. If there are 2 stones - absolute of their difference is an answer. 4. Else: put first stone to left (or right) and second to ther right (or left). 5. Put next stone to the side which is lowerю 6. Repeat 5 until number of stones will be reached. 7. Print the absolute value of difference of the left and right. Where I'm wrong? Edited by author 30.06.2012 23:35 Edited by author 30.06.2012 23:41 For Novosad. I go in that way to, but what's wrong with this solution? The flaw Hypбол is that this algorithm (which I did first, too, by the way) *always* places the largest and 2nd largest stones in different piles. Inputs like 5 4 6 7 8 9 require the 9 and 8 to be together in one pile. I'm not aware of any algorithm smarter than "try all combinations and choose the smallest difference). Any heuristic solution that doesn't try all combinations will fail to input specially crafted to fit the "blind spot" of the heuristic, like in this case. This problem does NOT belong in the beginner's section IMO! > This problem does NOT belong in the beginner's section IMO! OK, I see the simple O(2^n) algorithm, but still, I wouldn't call this a beginner's problem, since this technique of partitioning via binary strings is definitely not obvious to beginners. It's absolutely a very important technique, but someone who just picked up coding would take some while to think this up. Edited by author 15.09.2012 18:46 Edited by author 15.09.2012 18:46 > This problem does NOT belong in the beginner's section IMO! OK, I see the simple O(2^n) algorithm, but still, I wouldn't call this a beginner's problem, since this technique of partitioning via binary strings is definitely not obvious to beginners. It's absolutely a very important technique, but someone who just picked up coding would take some while to think this up. Edited by author 15.09.2012 18:46 I think so. Anyway, I can just use brute force right now. :-$ Edited by author 09.11.2013 18:50 The easiest one is brute-force There are 2 cycles, one of them is generating (0, 1) sequence, (1 if we include the stone in 1-st set of stones and 0 if not), there are 2^n permutations. The second cycle checks, if we should include the stone in the 1-st set and adds its weight to the current set weight. We should find the best difference between 2 sets' weights, the weight of one set is current_weight, of the second, obviously, sum - current_weight (sum is weight of all the stones), and the difference is abs(current_weight - (sum - current_weight)) = abs(sum - 2 * current_weight). But this is bad algorithm since it's complicity is O(n * 2^n), which is over 20 million in this problem in worst case. The DP algo complicity is O(n * sum), which is better in most of problems except this one, because if there are 20 stones and weight of each is 100000, then n * sum = 40 million, which is 2 times worse. Edited by author 29.03.2013 14:57 | unable to understand meaning of a statement | Anupam Ghosh,Bengal Engg and Sc Uni,MtechIT,India | 1005. Stone Pile | 29 Jan 2011 07:58 | 1 | Can any one please explain meaning of the statement "delimited by whitespace" because all inputs are given in newline. |
|
|