Wust oj 1421 we love girl (greedy or DP)
Description
When taking part in ACM Programming Contest, middle school hope girls for registration like ccnu, cug and so on. so this year our wust send more girls as possible to have a partition tion. the ratio of male to female of wust is seven to one, so we may not have so far girls. of course the rest exceptionist is boys.
Now there are n schools take part in Wust ACM Programming Contest, x girls and y boys is willing to help. if a school is wrongly ed buy girls, they will get p satisfaction. if your Ed buy boys, they will get q satisfaction. we want to improve the total satisfaction of every school. do you know the max satisfaction?
Input
The first line contains an integer T (T <= 100), indicates the number of cases. for each test case, the first line contains three positive integers n (1 <= n <= 20), x and y (x> = 0, y> = 0, x + y> = n ). n is the amount of schools and x is the amount of girls and y is the amount of boys. then n lines follows, each line contains two integers pi and qi which means the two satisfaction of girls and boys.
Output
For each test case, print one integer which is the max satisfaction.
Sample Input
2
3 2 1
2 1
4 2
1 2
4 2 2
3 2
2 4
10 9
4 0
Sample Output
8
20
This was a greedy question. N is relatively small and cannot take advantage of greed.
Because there are only two options, selecting one means giving up the other.
Our greedy strategy is to sort the differences between boys and girls, and
Select the optimal (if possible)
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) typedef long LL; typedef pair
Pil; const int INF = 0x3f3f3f; const int maxn = (1e5 + 100) * 2; struct node {int x, y; int val;} e [25]; int t, n, x, y; int cmp (node l1, node l2) {return l1.val> l2.val;} int main () {scanf ("% d", & t ); while (t --) {scanf ("% d", & n, & x, & y); int sum = 0, ans = 0; REPF (I, 1, n) {scanf ("% d", & e [I]. x, & e [I]. y); e [I]. val = abs (e [I]. x-e [I]. y);} sort (e + 1, e + 1 + n, cmp); for (int I = 1; I <= n; I ++) {if (e [I]. x> e [I]. y & x) {ans + = e [I]. x; x --;} else if (e [I]. x
It can also be done by DP. I selected the maximum value for j girls.
Note that if x> n, x = n ..
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair
pil;const int INF = 0x3f3f3f3f;int t,n,x,y;int c1[110],c2[110];int dp[110][110];int main(){ scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&x,&y); REPF(i,1,n) scanf("%d%d",&c1[i],&c2[i]); if(x>n) x=n; for(int i=0;i<=n;i++) for(int j=0;j<=x;j++) dp[i][j]=-INF; for(int i=1;i<=x;i++) dp[i][i]=dp[i-1][i-1]+c1[i]; dp[0][0]=0; for(int i=1;i<=n;i++) { for(int j=0;j<=x;j++) { if(i-j>y) continue; dp[i][j]=dp[i-1][j]+c2[i]; if(j>0) dp[i][j]=max(dp[i][j],dp[i-1][j-1]+c1[i]); } } int ans=-INF; for(int i=0;i<=x;i++) ans=max(ans,dp[n][i]); printf("%d\n",ans); } return 0;}/**/