zhx and contest (枚举  + dfs)

zhx and contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 324    Accepted Submission(s): 118

Problem Description
As one of the most powerful brushes in the world, zhx usually takes part in all kinds of contests. One day, zhx takes part in an contest. He found the contest very easy for him. There are n problems in the contest. He knows that he can solve the ith problem in ti units of time and he can get vi points. As he is too powerful, the administrator is watching him. If he finishes the ith problem before time li, he will be considered to cheat. zhx doesn't really want to solve all these boring problems. He only wants to get no less than w points. You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points.  Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
 
Input
Multiply test cases(less than 50). Seek EOF as the end of the file. For each test, there are two integers n and w separated by a space. (1≤n≤30, 0≤w≤109) Then come n lines which contain three integers ti,vi,li. (1≤ti,li≤105,1≤vi≤109)
 
Output
For each test case, output a single line indicating the answer. If zhx is able to get enough points, output the minimal time it takes. Otherwise, output a single line saying "zhx is naive!" (Do not output quotation marks).
 
Sample Input
1 3
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3
 
Sample Output
7 8 zhx is naive!
 
Source
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <numeric>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <complex>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
using namespace std;
template<class T> inline T sqr(T x) { return x * x; }
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0); #define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u)) using namespace std; #define MAXN 35 struct Question{
int t,v,l;
bool operator<(const Question& q) const{
return l - t < q.l - q.t ;
}
} q[MAXN]; LL remain[MAXN];
int n,w; bool dfs(int cur,int t,LL val){
if(val>=w) return true;
if(cur<) return false;
if(val+remain[cur]<w) return false;
if(t>=q[cur].l&&t>=q[cur].t)
if(dfs(cur-,t-q[cur].t,val+q[cur].v)) return true;
if(dfs(cur-,t,val)) return true;
return false;
} int main(void){
#ifndef ONLINE_JUDGE
freopen("a.txt","r",stdin);
#endif
while(scanf("%d %d",&n,&w)!=EOF){
LL sum = ;
FOR(i,n) scanf("%d %d %d",&q[i].t,&q[i].v,&q[i].l);
sort(q,q+n);
for(int i = ;i<n;i++){
if(i==) remain[i] = q[i].v;
else remain[i] = remain[i-]+q[i].v;
}
if(remain[n-]<w){
puts("zhx is naive!");
continue;
}
int ans = INF;
int low = ,high = *n;
while(low<=high){
int mid = (low+high)>>;
if(dfs(n-,mid,)){
high = mid-;
ans = mid;
}else low = mid+;
}
printf("%d\n",ans);
}
return ;
}
一个简单的想法是状态压缩dp。
但是30的数据范围存不下来。
首先发现如果选定了做哪些题的话,按题的l值递增的顺序做一定不会更劣。

然后我们把所有题按 l - t 排序,分成大小相同(或者相差1)的两部分。
对于前一部分,枚举出所有可能的取法的得分和结束时间。然后把它们按照得分排序。可以算出得分不少于某个值的时候完成时间最早是多少。

然后对于后一部分也是相同的枚举,然后在得分符合条件的里面找完成时间最早的,用和上面相同的办法就可以处理出答案。
上一篇:常用正则表达式


下一篇:微信小程序验证车牌号(含新能源车牌)