TCSRM 593 div2(1000)(dp)
2013-10-06 14:13
176 查看
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). | ||||||||||||
| ||||||||||||
| ||||||||||||
| ||||||||||||
- | The teams are not required to contain the same number of animals. | |||||||||||
| ||||||||||||
- | 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]. | |||||||||||
| ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
|
木想出来
按和来进行背包 这样来看吧 如果按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...
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stdlib.h> #include<vector> using namespace std; #define INF 0xfffffff int dp[1000010]; class MayTheBestPetWin { public: int calc(vector <int> A, vector <int> B) { int n = A.size(),s1=0,s2=0,i,j; for(i = 0; i < n ; i++) { s1+=A[i]; s2+=B[i]; A[i]+=B[i]; } int v = s1+s2; memset(dp,0,sizeof(dp)); dp[0] = 1; for(i = 0; i < n ; i++) for(j = v ; j>=A[i] ; j--) dp[j]=max(dp[j],dp[j-A[i]]); int ans = INF; for(i = 1 ; i <= v ; i++) { if(dp[i]>0) { ans = min(ans,max(abs(i-s1),abs(i-s2))); } } return ans; } };
View Code
相关文章推荐
- TCSRM 591 div2(1000)(dp)
- Codeforces Round #396(Div. 2) C. Mahmoud and a Message (dp)
- Codeforces Round #260 (Div. 2)C. Boredom(dp)
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip(dp)
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip 【线性DP】
- VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp
- srm 653 div2 1000(dp)
- DP方程+离散化——Codeforces Round #371 (Div. 1) C
- [矩乘优化DP] Topcoder SRM550 DIV1 Hard. ConversionMachine
- codeforces 277.5 div2 F:组合计数类dp
- Codeforces Round #106(Div. 2) 149D. Coloring Brackets 区间DP 记忆化搜索
- Codeforces Round #363 (Div. 2) C. Vacations【n-dp(max) or 暴力模拟】
- Codeforces Round #405 Div. 1 Bear and Company(DP)
- Codeforces Round #416 (Div. 2)A B C 水 暴力 dp
- 【Codeforces Round 363 (Div 2) E】【概率DP 期望DP 逆推等价法】LRU Cache替换LRU原则超多步数后每个数据在Cache中的概率
- Codeforces Round #260 (Div. 2)C. Boredom(dp)
- Codeforces Round #301 (Div. 2) D. Bad Luck Island(概率DP)
- Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp
- Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates(树形dp)
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)