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; }

좋은 웹페이지 즐겨찾기