1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
P2678 [NOIP2015 提高组] 跳石头_重构版.cpp 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#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;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385