poj 3017 Cut the Sequence dp
1599 단어 sequence
dp[k]는 점차적으로 증가하는 것이고 같은 전이 대가에 대해 우리는 i의 가장 작은 dp[i]와 가능함을 찾을 수 있다.
그래서 단조로운 대기열로 점차적으로 줄어드는 서열을 유지하고 전이 대가, 즉 한 단락의 최대 값을 저장할 수 있다.모든 이전 대가의 dp[i]+a[j]를 set에 압축합니다.dp[k]를 업데이트할 때마다 가장 작은 값을 찾습니다.
대기열 유지, set, 비교적 번거로운 경계 조건이 있으니 잘 처리해야 합니다.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
const int maxn=1e5+9;
typedef long long ll;
multiset <ll> d;
ll n,m;
int a[maxn];
struct
{
int id,data;
}que[maxn];
long long dp[maxn];
int main()
{
d.insert((ll)1<<50);
scanf("%lld %lld",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int st=1,ed=0,low=1;
long long sum=0;
for(int i=1;i<=n;i++)
{
if(a[i]>m)
{
printf("-1
");
return 0;
}
sum+=a[i];
while(sum>m) sum-=a[low++];
while(ed>=st&&a[i]>=que[ed].data)
{
if(ed>st) d.erase(d.find(dp[que[ed-1].id]+a[que[ed].id]));
ed--;
}
if(ed>=st) d.insert(dp[que[ed].id]+a[i]);
que[++ed].data=a[i];
que[ed].id=i;
while(que[st].id<low&&ed>=st)
{
if(st<ed) d.erase(d.find(dp[que[st].id]+a[que[st+1].id]));
st++;
}
long long tmp=min(*d.begin(),dp[low-1]+que[st].data);
dp[i]=tmp;
}
printf("%lld",dp[n]);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ---3061 Subsequence[대기열 구문 구간 및]Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6746 Accepted: 2465 Description A sequence of N p...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.