table of Contents
The meaning of problems
In fact, CF 724 E
This is in fact the brothers adapted the title QMQ, the true scope should be \ (n <= 10000 \)
Thinking
This question may flow obviously do with the network:
But the scope of the direct T.
Then we can use the greedy
Then we found this map looks like a magical nature, using the maximum flow = minimum cut, we can enumerate minimum cut.
Minimum cut of nature that every point is either located in the \ (\ st) set or \ (ed \) collection, so we will be able to enumerate the case:
We found this in the middle of the edge we need to address, so the \ (st, ed \) we have to deal with it, but we want to do with minimal cut it?
We set \ (f [i] [j ] \) indicated that the first \ (i \) points, there \ (j \) a return to \ (st \) collection.
Then no count \ (C \) effects, it is not difficult listed:
\ (F [I] [J] = min (F [I-. 1] [J-. 1] + A [I], F [I-. 1 ] [J] + B [I]) \) .
但是如何处理\(C\)呢,不难发现对于\(i,j(i<j)\)如果\(i\)选了\(ed\),\(j\)选了\(st\),那么就会有个\(C\)。
这个该归到\(st\)还是\(ed\)呢,很明显为了DP没有后效性,我们就归到\(ed\)吧。
那么DP转移方程变成了:\(f[i][j]=min(f[i-1][j-1]+a[i],f[i-1][j]+b[i]+j*C)\)。
就可以转移了,时间复杂度\(O(n^2)\),当然,这个又叫模拟网络流。
我的思路
我是采用贪心,这道题目\(n\)才\(2000\),还开\(2s\),那么我们不难想到一种贪心思路,就是使得所有卖完粮食后剩余粮食大于\(C\)的位置的粮食尽量的平衡。
也就是尽可能的榨干这个\(C\),使得后面每个位置都可以堆满\(C\)。
As for the balance of values, we use binary search, then the time complexity \ (O (n ^ 2log range) \) .
But also optimized, \ (1S \) had lost inside.
#include<cstdio>
#include<cstring>
#include<queue>
#define N 3100
using namespace std;
typedef long long LL;
template <class T>
inline T mymin(T x,T y){return x<y?x:y;}
int a[N],b[N];
priority_queue<int>q;//储存粮食信息,当然是大于C的才给放
int sta[N],top;//表示可以移动的粮食
int list[N],tail;
int n,m;
LL zans;
bool check(int x,int k/*原本的数字*/)
{
for(int i=1;i<=top;i++)
{
if(sta[i]<=x)return false;
k+=mymin(sta[i]-x,m);//可以给你多少的数字
if(k>=x)return true;
}
return false;
}
void work(int x,int y)
{
top=0;
while(!q.empty())sta[++top]=q.top(),q.pop();
int k=x-y;//可能是负数
if(top)//有大于C的粮食位置
{
int l=k+1,r=mymin((LL)sta[1],(LL)k+(LL)top*(LL)m),mid,ans=k/*就是不变*/;//表示范围,而且能防止m过小时时间过大
while(l<=r)
{
mid=(l+r)/2;
if(check(mid,k)==true)ans=mid,l=mid+1;
else r=mid-1;
}
for(int i=1;i<=top;i++)
{
if(k==ans)break;
int zjj=mymin(mymin(ans-k,m),sta[i]-ans);sta[i]-=zjj;k+=zjj;
}
}
if(k<0)
{
while(k<0 && tail)k+=list[tail--];
}
zans+=mymin(y,k+y);
if(k>0)
{
if(k<=m)list[++tail]=k;
else q.push(k);
}
for(int i=1;i<=top;i++)
{
if(sta[i]<=m)list[++tail]=sta[i];
else q.push(sta[i]);
}
}
int main()
{
// freopen("c.in","r",stdin);
// freopen("c.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
for(int i=1;i<=n;i++)
{
work(a[i],b[i]);
}
printf("%lld\n",zans);
return 0;
}