QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#617901#1964. Stock Price PredictionCheek_support#RE 0ms0kbC++204.1kb2024-10-06 17:35:432024-10-06 17:35:45

Judging History

你现在查看的是最新测评结果

  • [2024-10-06 17:35:45]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-06 17:35:43]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define mk(x,y) make_pair(x,y)
using namespace std;
int read(){
    char ch=getchar();int x=0,f=1;
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
    for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
    return f*x;
}
int n,m;
const int N=300100;
ll a[N],b[N];
pair<ll ,int> c[N];
int x,y;
bool bo[N];
void work1(){
    int L=m,R=n-m;
    if(L==0){
        if(x==1)printf("%lld\n",a[n]-a[1]);
        else puts("-1");
        return;
    }
    if(R==0){
        if(x==n)printf("%lld\n",a[n]-a[1]);
        else puts("-1");
        return;
    }

    ll ans=(1ll<<60);
    {
        ll res=0;
        int t=n-x-R;
        if(t>0){
            for(int i=1;i<=n-x-2;i++)
                c[i]=mk(b[i+x],i+x);
            sort(c+1,c+n-x-1);
            ll num=0;res=(1ll<<60);
            for(int i=1;i<=t;i++)
                num+=c[i].first*2,bo[c[i].second]=1;
            res=min(res,num);
            int i=t;
            for(int j=n-1;j>=n-t;j--){
                if(bo[j-1]){
                    bo[j-1]=0;
                    num+=b[j]-2*b[j-1];
                }else{
                    for(;!bo[c[i].second];)i++;
                    bo[c[i].second]=1;
                    num+=b[j]-2*c[i].first;
                }
                res=min(res,num);
            }
            if(t==n-x-1)res=a[n]-a[x+1];
        }
        ans=min(ans,res+a[x]+a[n]-2*a[1]);
    }

    for(int i=1;i<=n;i++)a[i]=-a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<n;i++)b[i]=a[i+1]-a[i];
    x=n-x+1;y=n-y+1;
    swap(L,R);
    memset(bo,0,sizeof(bo));

    {
        ll res=0;
        int t=n-x-R;
        if(t>0){
            for(int i=1;i<=n-x-2;i++)
                c[i]=mk(b[i+x],i+x);
            sort(c+1,c+n-x-1);
            ll num=0;res=(1ll<<60);
            for(int i=1;i<=t;i++)
                num+=c[i].first*2,bo[c[i].second]=1;
            res=min(res,num);
            int i=t;
            for(int j=n-1;j>=n-t;j--){
                if(bo[j-1]){
                    bo[j-1]=0;
                    num+=b[j]-2*b[j-1];
                }else{
                    for(;!bo[c[i].second];)i++;
                    bo[c[i].second]=1;
                    num+=b[j]-2*c[i].first;
                }
                res=min(res,num);
            }
            if(t==n-x-1)res=a[n]-a[x+1];
        }
        ans=min(ans,res+a[x]+a[n]-2*a[1]);
    }

    printf("%lld\n",ans);
}
void work2(){
    // for(;;);
    int L=m,R=n-m;
    
    if(y<x){
        for(int i=1;i<=n;i++)a[i]=-a[i];
        sort(a+1,a+n+1);
        for(int i=1;i<n;i++)b[i]=a[i+1]-a[i];
        x=n-x+1;y=n-y+1;
        swap(L,R);
    }

    if(R==0){puts("-1");return;}
    if(L==1){
        if(x==1&&y==n){
            ll minn=(1ll<<60);
            for(int i=2;i<n-1;i++)
                minn=min(minn,b[i]);
            if(minn==(1ll<<60))puts("-1");
            else printf("%lld\n",a[n]-a[1]+2*minn);
            return;
        }
        if(x==1||y==n)printf("%lld\n",2*(a[n]-a[1])-(a[y]-a[x]));
        else puts("-1");
        return;
    }
    if(L==0){
        if(x==1&&y==n)printf("%lld\n",a[n]-a[1]);
        else puts("-1");
        return;
    }
    if(R==1){
        if(y-x==1)printf("%lld\n",2*(a[n]-a[1])-(a[y]-a[x]));
        else puts("-1");
        return;
    }

    int t=y-x-R;
    ll res=0;
    if(t>0){
        ll num=0;
        for(int i=1;i<y-x-1;i++)
            c[i]=mk(b[x+i],x+i);
        sort(c+1,c+y-x-1);
        for(int i=1;i<=t;i++)
            num+=2*c[i].first;
        res=num;
    }
    printf("%lld\n",res+2*(a[n]-a[1])-(a[y]-a[x]));

}
int main(){
    n=read();m=read();
    int k=read();
    for(int i=1;i<=n;i++)a[i]=read();
    x=0,y=a[n];
    a[n+1]=0;n++;
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
        if(a[i]==x){x=i;break;}
    for(int i=1;i<=n;i++)
        if(a[i]==y){y=i;break;}
    for(int i=1;i<n;i++)b[i]=a[i+1]-a[i];
    if(k==1)work1();
    else work2();
    return 0;
}
/*
5 4 2
-20 -15 20 30 10

5 4 1
-20 -15 20 30 10

7 1 2
10 13 -30 24 50 -5 -21
*/

詳細信息

Test #1:

score: 0
Runtime Error

input:

10000 1000000
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:


result: