Bzoj2667 [CQOI2012] 模拟工厂

Description

https://www.luogu.com.cn/problem/P3161

Solution

显然如果只有一个订单,把提高生产力的时间都放在前面更优。

观察到数据范围 $n\le 15$,用 $O(2^n)$ 枚举订单来判断全部做完是否可行。

考虑现在处理到第 $i$ 个订单,有两种定性的决策:

  • 多学一点,这样之后做就会更优。但是如果第 $i+1$ 个订单离第 $i$ 个订单很近,就不太行。
  • 多做一点,留一点商品放到后面直接用。但是如果后面有充分的时间生产这样也不太优。

可以对之后的所有任务分别解一次方程,取有解的右端点,这样对这个任务肯定最优。然后为了bao’zheng都有解,对所有右端点取个 min。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define maxn
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
int power(int x,int y=mod-2) {
int sum=1;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
struct yyy {
int t,x,w;
}a[25],s[25];
int ans=0,n,tot;
bool cmp(yyy x,yyy y) {return x.t<y.t;}
int get(int a,int b,int c) {
int delta=b*b-4*a*c;
if (delta<0) return -1;
return floor((-b+1.0*sqrtl(delta))/2.0/a);
}
void calc(int S) {
int i,j,sum=0;tot=0;
for (i=0;i<n;i++) if ((S>>i)&1) s[++tot]=a[i+1],sum+=s[tot].w;
int p=1,res=0;
for (i=1;i<=tot;i++) {
int d=0,t=s[i].t-s[i-1].t,nums=0;
for (j=i;j<=tot;j++) {
d+=s[j].t-s[j-1].t;
nums+=s[j].x;
if (nums>=res) t=min(t,get(1,p-d,nums-res-p*d));
}
if (t<0) return ;
p+=t;
res+=(s[i].t-s[i-1].t-t)*p-s[i].x;
}
ans=max(ans,sum);
}
signed main(void){
int i;
read(n);
for (i=1;i<=n;i++) read(a[i].t),read(a[i].x),read(a[i].w);
sort(a+1,a+1+n,cmp);
for (i=1;i<(1<<n);i++) calc(i);
printf("%lld",ans);
return 0;
}