TCSRM 593 div2(1000)(dp)
Problem Statement |
|||||||||||||
The pony Rainbow Dash wants to choose her pet. There are N animals who want to be her pet. Rainbow Dash numbered them 0 through N-1. To help her make the decision, Rainbow Dash decided to organize a relay race for the animals. The race track is already known, and for each animal we know how fast it is. More precisely, you are given vector <int>s A and B with the following meaning: For each i, the animal number i will take between A[i] and B[i] seconds (inclusive) to complete the track. For the race the animals will be divided into two competing teams. This is a relay race, so the team members of each team will all run the same track, one after another -- when the first team member finishes, the second one may start, and so on. Thus the total time in which a team completes the race is the sum of the times of all team members. Note that we can use the estimates given by A and B to estimate the total time for any team of animals. Given two teams S and T, the value maxdiff(S,T) is defined as the largest possible difference in seconds between the time in which team S finishes the course and the time in which team T finishes the course. Rainbow Dash now needs to assign each of the animals to one of the two competing teams. She wants to see a close competition, so she wants the teams to finish as close to each other as possible. Formally, she wants to divide all animals into teams S and T in a way that minimizes maxdiff(S,T). Return the smallest possible value of maxdiff(S,T). |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | The teams are not required to contain the same number of animals. | ||||||||||||
Constraints |
|||||||||||||
- |
A will contain between 2 and 50 elements, inclusive. |
||||||||||||
- |
A and B will contain the same number of elements. |
||||||||||||
- |
Each element of A will be between 1 and 10,000, inclusive. |
||||||||||||
- |
Each element of B will be between 1 and 10,000, inclusive. |
||||||||||||
- | For each i, B[i] will be greater than or equal to A[i]. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
木想出来
按和来进行背包 这样来看吧 如果按a数组来背 那我们要算出这几个值
s1(前面背出来的和值) s2(剩余的) a数组
s3(前面背出来的和值) s4(剩余的) b数组
想求s3-s2 和s1-s4 最值也就从这两种情况里取一个
如果按和背的话 直接就保存了 s1+s3 而(s2+s1)和(s3+s4)是定值 每次背的值 s1+s3-(s2+s1) 这样就出了s3-s2 同样s1-s4也求出来了
so easy...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 using namespace std; 8 #define INF 0xfffffff 9 int dp[1000010]; 10 class MayTheBestPetWin 11 { 12 public: 13 int calc(vector <int> A, vector <int> B) 14 { 15 int n = A.size(),s1=0,s2=0,i,j; 16 for(i = 0; i < n ; i++) 17 { 18 s1+=A[i]; 19 s2+=B[i]; 20 A[i]+=B[i]; 21 } 22 int v = s1+s2; 23 memset(dp,0,sizeof(dp)); 24 dp[0] = 1; 25 for(i = 0; i < n ; i++) 26 for(j = v ; j>=A[i] ; j--) 27 dp[j]=max(dp[j],dp[j-A[i]]); 28 int ans = INF; 29 for(i = 1 ; i <= v ; i++) 30 { 31 if(dp[i]>0) 32 { 33 ans = min(ans,max(abs(i-s1),abs(i-s2))); 34 } 35 } 36 return ans; 37 } 38 };