洛谷$P3620\ [APIO/CTSC 2007]$数据备份 贪心
2024-09-02 09:53:19
正解:贪心
解题报告:
$umm$感觉这种问题还蛮经典的,,,就选了某个就不能选另一个这样儿,就可以用堆模拟反悔操作
举个$eg$,如果提出了$a_i$,那就$a_{i-1}$和$a_{i+1}$都不能选了,所以如果选了$a_i$之后想反悔选$a_{i-1}$和$a_{i+1}$就相当于只能另外获得$a_{i+1}+a_{i-1}-a_{i}$的收益了.所以每次取出堆顶$a_{i}$之后,就把$a_{i-1}$和$a_{i+1}$删了,然后插入$a_{i-1}+a_{i+1}-a_{i}$.然后多个数也差不多$QwQ$
所以就用个堆+链表维护下就成$QwQ$
$over$
#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define ll long long
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)
#define lb(x) lower_bound(st+1,st+st_cnt+1,x)-st const int N=+,inf=1e9;
int n,K,a[N],l[N],r[N],d[N];
ll as;
bool vis[N];
struct node{ll dat;int id;}tmp;
priority_queue<node>Q; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il bool operator < (node gd,node gs){return gd.dat>gs.dat;} int main()
{
//freopen("3620.in","r",stdin);freopen("3620.out","w",stdout);
n=read();K=read();
rp(i,,n-){d[i]=read();if(i){tmp=(node){a[i]=d[i]-d[i-],i};Q.push(tmp);l[i]=i-;r[i]=i+;}}
r[]=;l[n]=n-;a[]=a[n]=inf;
while(K--)
{
while(vis[Q.top().id])Q.pop();
node nw=Q.top();Q.pop();ri pos=nw.id;
as+=nw.dat;nw.dat=a[pos]=a[l[pos]]+a[r[pos]]-a[pos];Q.push(nw);
vis[l[pos]]=vis[r[pos]]=;l[pos]=l[l[pos]];r[pos]=r[r[pos]];r[l[pos]]=pos;l[r[pos]]=pos;
}
printf("%lld\n",as);
return ;
}
最新文章
- Oracle SQL Developer 连接 MySQL
- 如何在mac本上安装android sdk 避免被墙
- SQLite 创建自增长标识列
- 【转】web测试总结
- 元素JS拖动的实现
- 关于String和StringBuffer的使用
- iOS开发核心语言Objective C —— 所有知识点总结
- JAVA开发环境 - 环境变量及配置
- 【HDOJ】4704 Sum
- Swift - AppDelegate.swift类中默认方法的介绍
- itmacy_我的博客
- Vue 爬坑之路(九)—— 用正确的姿势封装组件
- JQuery AJAX 全局设置
- Docker 系列七(Dubbo 微服务部署实践).
- BUG描述规范管理
- kafka definitive guide - reading notes
- MFC坐标转换
- PHP和JS中全局变量和局部变量
- (The application/json Media Type for JavaScript Object Notation (JSON))RFC4627-JSON格式定义
- hdu-4725-The Shortest Path in Nya Graph-层次网络