2 条题解
-
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, int> PLI; const int N=1e5+100; LL a[N]; int L[N], R[N], del[N]; priority_queue<PLI, vector<PLI>, greater<PLI>> q; void remove(int x) { L[R[x]]=L[x]; R[L[x]]=R[x]; del[x]=1; } int main() { int n, m;scanf("%d%d", &n, &m); int k=0; for(int i=1;i<=n;i++) { LL x;scanf("%lld", &x); if(x!=0) { if(!k || a[k]*x < 0)a[++k]=x; else a[k]+=x; } } n=k; LL ans=0, cnt=0; for(int i=1;i<=n;i++) { L[i] = i-1; R[i] = i+1; q.push({abs(a[i]), i}); if(a[i] > 0)ans += a[i], cnt++; } memset(del, 0, sizeof(del)); while(cnt > m) { while(del[q.top().second])q.pop(); PLI no=q.top();q.pop(); int x=no.second; if(a[x] > 0 || (L[x] != 0 && R[x] != n + 1)) { ans -= abs(a[x]); a[x] += a[L[x]] + a[R[x]]; q.push({abs(a[x]), x}); remove(L[x]); remove(R[x]); cnt--; } } printf("%d", ans); return 0; } -
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL,int> PLI; const int N=1e5+100; LL a[N]; int L[N],R[N],del[N]; priority_queue<PLI,vector<PLI>,greater<PLI>> q; void remove(int x) { L[R[x]]=L[x]; R[L[x]]=R[x]; del[x]=1; } int main() { int n,m;scanf("%d%d",&n,&m); int k=0; for(int i=1;i<=n;i++) { LL x;scanf("%lld",&x); if(x!=0) { if(!k||a[k]*x<0)a[++k]=x; else a[k]+=x; } } n=k; LL ans=0,cnt=0; for(int i=1;i<=n;i++) { L[i]=i-1,R[i]=i+1; q.push({abs(a[i]),i}); if(a[i]>0)ans+=a[i],cnt++; } memset(del,0,sizeof(del)); while(cnt>m) { while(del[q.top().second])q.pop(); PLI no=q.top();q.pop(); int x=no.second; if(a[x]>0||(L[x]!=0&&R[x]!=n+1)) { ans-=abs(a[x]); a[x]+=a[L[x]]+a[R[x]]; q.push({abs(a[x]),x}); remove(L[x]); remove(R[x]); cnt--; } } printf("%d",ans); return 0; }
- 1
信息
- ID
- 1304
- 时间
- 1000ms
- 内存
- 64MiB
- 难度
- 6
- 标签
- 递交数
- 99
- 已通过
- 29
- 上传者