题目链接:点击这里
题目大意:除了 t i t_i ti 范围从 1 ≤ t i ≤ 100 1\le t_i \le100 1≤ti≤100 变成了 ∣ t i ∣ ≤ 2 8 ∣t_i∣\le2^8 ∣ti∣≤28其余同P2365 任务安排
题目分析:
同P2365 任务安排
我们可以得到
s
u
m
t
[
i
]
+
s
>
d
p
[
b
]
−
d
p
[
a
]
s
u
m
c
[
b
]
−
s
u
m
c
[
a
]
sumt[i]+s>\frac{dp[b]-dp[a]}{sumc[b]-sumc[a]}
sumt[i]+s>sumc[b]−sumc[a]dp[b]−dp[a]
但是不同的是此题的
t
i
t_i
ti 存在负数情况,即
s
u
m
t
[
i
]
sumt[i]
sumt[i] 不再单调,故不能再像之前那样通过维护一个单调队列每次取队首元素来获取最优决策点了
那我们现在如何找这个最优决策点呢,之前推的式子真就没用了吗,答案是否定的,我们发现我们要找的最优决策点就是单调队列里面相邻两点斜率第一个大于
s
u
m
t
[
i
]
+
s
sumt[i]+s
sumt[i]+s的点,而我们单调队列中的元素都是单调的,因此这个寻找可以通过二分来完成
注意此时的单调队列不再能弹出队首元素了,因为虽然此时队首元素不是最优决策点,但是其可能是之后决策中的最优决策点(因为
s
u
m
t
[
i
]
+
s
sumt[i]+s
sumt[i]+s 不再单调)
然后我们就有了如下代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll read()
{
ll res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 3e5+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
ll sumc[maxn],sumt[maxn];
ll n,s,dp[maxn],qu[maxn],h,t;
double calc(int i,int j)
{
return 1.0*(dp[i]-dp[j])/(sumc[i]-sumc[j]);
}
int binary_search(int val)
{
if(h == t) return qu[h];
int l = h,r = t;
while(l < r)
{
int mid = (l+r)>>1;
if(calc(qu[mid],qu[mid+1]) > 1.0*val) r = mid;
else l = mid+1;
}
return qu[l];
}
int main()
{
n = read(),s = read();
for(int i = 1;i <= n;i++)
{
int t = read(),c = read();
sumt[i] = sumt[i-1]+t;
sumc[i] = sumc[i-1]+c;
}
h = t = 1;
for(int i = 1;i <= n;i++)
{
int j = binary_search(sumt[i]+s);
dp[i] = dp[j]+sumt[i]*(sumc[i]-sumc[j])+s*(sumc[n]-sumc[j]);
while(h < t && calc(qu[t-1],qu[t]) >= calc(qu[t],i)) t--;
qu[++t] = i;
}
printf("%lld\n",dp[n]);
return 0;
}
提交上去你就会发现你只有40分,原因是你被 卡 精 度 了,此时你想把
d
o
u
b
l
e
double
double 改成
l
o
n
g
d
o
u
b
l
e
long double
longdouble 你会发现你还是40分(大哭) ,我们把除法转化为乘法就可以完美通过此题了
具体细节见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll read()
{
ll res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 3e5+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
ll sumc[maxn],sumt[maxn];
ll n,s,dp[maxn],qu[maxn],h,t;
long double calc(int i,int j)
{
return 1.0*(dp[i]-dp[j])/(sumc[i]-sumc[j]);
}
int binary_search(int val)
{
if(h == t) return qu[h];
int l = h,r = t;
while(l < r)
{
int mid = (l+r)>>1;
//if(calc(qu[mid],qu[mid+1]) > 1.0*val) r = mid;
if(dp[qu[mid+1]]-dp[qu[mid]] > 1.0*val*(sumc[qu[mid+1]]-sumc[qu[mid]])) r = mid;
else l = mid+1;
}
return qu[l];
}
int main()
{
n = read(),s = read();
for(int i = 1;i <= n;i++)
{
int t = read(),c = read();
sumt[i] = sumt[i-1]+t;
sumc[i] = sumc[i-1]+c;
}
h = t = 1;
for(int i = 1;i <= n;i++)
{
int j = binary_search(sumt[i]+s);
dp[i] = dp[j]+sumt[i]*(sumc[i]-sumc[j])+s*(sumc[n]-sumc[j]);
//while(h < t && calc(qu[t-1],qu[t]) >= calc(qu[t],i)) t--;
while(h < t && (dp[qu[t]]-dp[qu[t-1]])*(sumc[i]-sumc[qu[t]]) >= (sumc[qu[t]]-sumc[qu[t-1]])*(dp[i]-dp[qu[t]])) t--;
qu[++t] = i;
}
printf("%lld\n",dp[n]);
return 0;
}