标签:des style blog http color strong
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8187 | Accepted: 2204 |
Description
Input
Output
Sample Input
3 100 90 200
Sample Output
190 200
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <math.h> 5 #include <stdlib.h> 6 #include <time.h> 7 using namespace std; 8 int aabs(int a) 9 { 10 if(a>0)return a; 11 else return -a; 12 } 13 int main() 14 { 15 int n,a[110][2],b1=0,b0=0,sum0=0,sum1=0,cha,i; 16 scanf("%d",&n); 17 srand((unsigned)time(NULL)); 18 for(i=0; i<n; i++) 19 { 20 if(i&1) 21 scanf("%d",&a[1][b1]),sum1+=a[1][b1++]; 22 else scanf("%d",&a[0][b0]),sum0+=a[0][b0++]; 23 } 24 cha=aabs(sum1-sum0); 25 n=1000000; 26 while(n--) 27 { 28 int c0=rand()%b0; 29 int c1=rand()%b1; 30 if(cha>aabs((sum0-a[0][c0]+a[1][c1])-(sum1-a[1][c1]+a[0][c0]))) 31 { 32 sum0=sum0-a[0][c0]+a[1][c1]; 33 sum1=sum1-a[1][c1]+a[0][c0]; 34 swap(a[0][c0],a[1][c1]); 35 cha=aabs(sum0-sum1); 36 } 37 } 38 if(sum0>sum1)swap(sum0,sum1); 39 cout<<sum0<<" "<<sum1<<endl; 40 }
随机算法 poj 2576 Tug of War,布布扣,bubuko.com
标签:des style blog http color strong
原文地址:http://www.cnblogs.com/ERKE/p/3830408.html