代码拉取完成,页面将自动刷新
同步操作将从 charlieshu/Algorithms 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include <iostream>
#include <queue>
#include <string.h>
#include <math.h>
#define int long long
using namespace std;
struct step{
int left,right,left_index,right_index,now_index,length;
bool operator < (const step x)const{
return length>x.length;
}
};
signed main(){
int l,n,m;
priority_queue<step> q;
cin>>l>>n>>m;
step len[n+1];
bool vis[n+2];
memset(vis,true,sizeof(vis));
int bef=0;
for(signed i=0;i<n;i++){
int now;
cin>>now;
len[i].left = i;
len[i].right = i+1;
len[i].left_index = i-1;
len[i].now_index = i;
len[i].right_index = i+1;
len[i].length = now-bef;
q.push(len[i]);
bef = now;
}
len[n].left = n;
len[n].right = n+1;
len[n].left_index = n-1;
len[n].now_index = n;
len[n].right_index = n+1;
len[n].length = l-bef;
q.push(len[n]);
for(signed i=0;i<m;i++){
step now;
now = q.top();
q.pop();
if(vis[now.left] == false || vis[now.right] == false){
i--;
continue;
}
now = len[now.now_index];
if(now.left > 0 && now.right <= n){
if(len[now.left_index].length <= len[now.right_index].length){
vis[now.left] = false;
len[now.left_index].right = now.right;
len[now.left_index].right_index = now.right_index;
len[now.left_index].length += now.length;
len[now.right_index].left_index = now.left_index;
q.push(len[now.left_index]);
}
else{
vis[now.right] = false;
len[now.right_index].left = now.left;
len[now.right_index].left_index = now.left_index;
len[now.right_index].length += now.length;
len[now.left_index].right_index = now.right_index;
q.push(len[now.right_index]);
}
}
else{
if(now.left == 0){
vis[now.right] = false;
len[now.now_index].right = len[now.right_index].right;
len[now.now_index].right_index = len[now.right_index].right_index;
len[now.now_index].length += len[now.right_index].length;
len[len[now.right_index].right_index].left_index = now.now_index;
q.push(len[now.now_index]);
}
else{
vis[now.left] = false;
len[now.now_index].left = len[now.left_index].left;
len[now.now_index].left_index = len[now.left_index].left_index;
len[now.now_index].length += len[now.left_index].length;
len[len[now.left_index].left_index].right_index = now.now_index;
q.push(len[now.now_index]);
}
}
}
while(vis[q.top().left] == false || vis[q.top().right] == false){
q.pop();
}
cout<<q.top().length;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。