hdu1171--C - Big Event in HDU(多重背包+二进制优化)
来源:互联网 发布:linux crontab每秒 编辑:程序博客网 时间:2024/06/06 05:01
C - Big Event in HDU
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
Sample Input
210 120 1310 1 20 230 1-1
Sample Output
20 1040 40
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{ int v , m ;} q[100];int dp[260000] ;int main(){ int n , ans , i , j , k , sum , s , l; while(scanf("%d", &n) && n > 0) { ans = 0 ; for(i = 0 ; i < n ; i++) { scanf("%d %d", &q[i].v, &q[i].m); ans += q[i].v*q[i].m ; } sum = ans ; ans = ans / 2 ; memset(dp,0,sizeof(dp)); dp[0] = 1 ; for(i = 0 ; i < n ; i++) { k = -1 ; while( (1<<(k+1)) -1 < q[i].m ) k++ ; for(j = 0 ; j <= k ; j++) { if(j == k) s = q[i].m - (1<<j) + 1 ; else s = 1<<j ; for(l = ans ; l >= s*q[i].v ; l--) if( !dp[l] && dp[ l-s*q[i].v ] ) dp[l] = 1 ; } } for(i = ans ; i >= 0 ; i--) { if( dp[i] ) { printf("%d %d\n", sum-i, i); break; } } } return 0;}
0 0
- hdu1171--C - Big Event in HDU(多重背包+二进制优化)
- Big Event in HDU hdu1171 多重背包
- HDU1171:Big Event in HDU(多重背包)
- hdu1171 Big Event in HDU (多重背包)
- hdu1171---Big Event in HDU(多重背包)
- hdu1171 Big Event in HDU (多重背包)
- hdu1171 Big Event in HDU(典型多重背包)
- hdu1171 Big Event in HDU(多重背包)
- hdu1171 Big Event in HDU(多重背包)
- HDU1171--Big Event in HDU--01背包,多重背包
- hdu 1171 Big Event in HDU(多重背包+二进制优化)
- HDU-1171-Big Event in HDU(多重背包 二进制优化)
- HDU1171:Big Event in HDU(01背包)
- hdu1171 Big Event in HDU 01-背包
- hdu1171 Big Event in HDU 01背包
- hdu1171 Big Event in HDU --01背包
- HDU1171:Big Event in HDU(01背包)
- hdu1171 Big Event in HDU (01背包变形)
- Android中实现可滑动的Tab的3种方式
- 14-8-28
- Ural 1096-Get the Right Route Plate!(bfs)
- UVa 10791 - Minimum Sum LCM (数论 推理)
- 461C - Appleman and a Sheet of Paper 模拟
- hdu1171--C - Big Event in HDU(多重背包+二进制优化)
- python连接oracle
- Qt学习 QVariant类(转)
- Plus One Java
- hdu1203--D - I NEED A OFFER!(转化01背包)
- 使用DbUtils实现增删改查
- 蓝牙的工作原理
- 服务器操作系统CoreOS初体验
- classloader