2 条题解
-
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, int> PLI; const int N=1e5+10; int a[N], 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, k; scanf("%d%d", &n, &k); scanf("%d", &a[1]); for(int i=2; i<=n; i++) scanf("%d", &a[i]), a[i-1] = a[i] - a[i-1];//开始计算权值 n--; a[0] = a[n+1] = 1e9; for(int i=1; i<=n; i++) q.push({a[i], i}), L[i] = i-1, R[i] = i+1;//设置前驱后继 LL ans = 0; memset(del, 0, sizeof(del)); while(k--) { while(del[q.top().second]) q.pop(); PLI no = q.top(); q.pop(); ans += no.first;//加上消耗的长度 int x = no.second; a[x] = a[L[x]] + a[R[x]] - a[x];//准备增加一条边的方案“ x的前继(选) + x本身异或 +x的后继(选) remove(L[x]); remove(R[x]); q.push({a[x], x}); } printf("%lld\n", ans); return 0; } -
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL,int> PLI; const int N=1e5+10; int a[N],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,k;scanf("%d%d",&n,&k); scanf("%d",&a[1]);for(int i=2; i<=n; i++) scanf("%d",&a[i]),a[i-1]=a[i]-a[i-1];//开始计算权值 n--; a[0]=a[n+1]=1e9; for(int i=1; i<=n; i++) q.push({a[i],i}),L[i]=i-1,R[i]=i+1;//设置前驱后继. LL ans=0; memset(del,0,sizeof(del)); while(k--) { while(del[q.top().second])q.pop(); PLI no=q.top();q.pop(); ans+=no.first;//加上消耗的长度 int x=no.second; a[x]=a[L[x]]+a[R[x]]-a[x];//准备增加一条边的方案“ x的前继(选) + x本身异或 +x的后继(选) remove(L[x]);remove(R[x]); q.push({a[x],x}); } printf("%lld\n",ans); return 0; }
- 1
信息
- ID
- 2803
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 9
- 标签
- 递交数
- 10
- 已通过
- 9
- 上传者