题意:有n门课的作业,每门课的作业都有必须花费时间以及截止期限,到截止期限还没做完的作业每天要扣一分,判断怎么安排作业顺序使得扣分最少
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074
思路:n的范围很小(n<15),容易想到是状压dp,在状态转移的过程中记录前一个状态的位置,最后利用stack还原顺序即可
注意点:无
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
13669771 | 2015-05-11 10:34:04 | Accepted | 1074 | 15MS | 2648K | 3720 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
//#include <unordered_map>
//#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define RS(s) scanf ( "%s", s )
#define PI(a) printf ( "%d", a )
#define PIL(a) printf ( "%d\n", a )
#define PII(a,b) printf ( "%d %d", a, b )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIII(a,b,c) printf ( "%d %d %d", a, b, c )
#define PIIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define PL() printf ( "\n" )
#define PSL(s) printf ( "%s\n", s )
#define rep(i,m,n) for ( int i = m; i < n; i ++ )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define dep(i,m,n) for ( int i = m; i > n; i -- )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define repi(i,m,n,k) for ( int i = m; i < n; i += k )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define depi(i,m,n,k) for ( int i = m; i > n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
template <class T>
inline bool RD ( T &ret )
{
char c;
int sgn;
if ( c = getchar(), c ==EOF )return 0; //EOF
while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar();
sgn = ( c == '-' ) ? -1 : 1;
ret = ( c == '-' ) ? 0 : ( c - '0' );
while ( c = getchar() , c >= '0' && c <= '9' ) ret = ret * 10 + ( c - '0' );
ret *= sgn;
return 1;
}
inline void PD ( int x )
{
if ( x > 9 ) PD ( x / 10 );
putchar ( x % 10 + '0' );
}
const double eps = 1e-10;
const int inf = 1<<30;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
string str;
int dl;
int u;
void input ()
{
cin >> str >> dl >> u;
}
}data[50];
struct temp{
int now, pre;
int val, date;
}dp[1<<16];
int tmp;
int n;
int main()
{
//ios::sync_with_stdio( false );
int k;
RDI ( k );
while ( k -- ){
clr ( dp, 0 );
RDI ( n );
rep ( i, 0, n ){
data[i].input();
}
int tmp = 1 << n;
rep ( s, 1, tmp ){
dp[s].val = inf;
DEP ( i, n - 1, 0 ){
int tem = 1 << i;
if ( s & tem ){
int past = s - tem;
int st = dp[past].date + data[i].u - data[i].dl;
if ( st < 0 ) st = 0;
if ( st + dp[past].val < dp[s].val ){
dp[s].val = st + dp[past].val;
dp[s].now = i;
dp[s].pre = past;
dp[s].date = dp[past].date + data[i].u;
}
}
}
}
stack<int> S;
int tem = tmp - 1;
cout << dp[tem].val << endl;
while ( tem ){
S.push( dp[tem].now );
tem = dp[tem].pre;
}
while ( ! S.empty() ){
cout << data[S.top()].str << endl;
S.pop();
}
}
return 0;
}