国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

BZOJ 1150 數據備份Backup 【貪心】【堆】

2019-11-08 02:54:49
字體:
來源:轉載
供稿:網友

題目鏈接:http://www.lydsy.com/JudgeOnline/PRoblem.php?id=1150

題意: 就是問你求完差分后選出 k 個數,使得他們的和最小。 題解: TMD這道題搞得我快要神志不清了。。WA了12次才搞出來。。 這道題基本就是SB貪心,用一個堆維護就可以了。 但是一定要注意細節。。我快要被坑慘了。。

代碼:

#include <queue>#include <cstdio>#include <vector>using namespace std;const int size = 300005, inf = 1 << 29;int n, k;#define pii pair<int, int>int a[size];int pre[size], nxt[size];priority_queue<pii, vector<pii>, greater<pii> > que;int main() { scanf("%d %d", &n, &k); int t1 = 0, t2; for ( int i = 1; i <= n; i ++ ){ scanf("%d", &t2); a[i] = t2-t1; pre[i] = i-1; nxt[i] = i+1; t1 = t2; } int ans = 0; for ( int i = 2; i <= n; i ++ ) que.push(make_pair(a[i], i)); pre[2] = 0; nxt[n] = 0; for ( int i = 1; i <= k; i ++ ) { while(que.top().first != a[que.top().second]) que.pop(); int x = que.top().second; int left = pre[x], right = nxt[x]; ans += a[x]; que.pop(); pre[nxt[x] = nxt[right]] = x; nxt[pre[x] = pre[left]] = x; a[x] = left&&right?min(inf, a[left]+a[right]-a[x]):inf; a[left] = a[right] = inf; que.push(make_pair(a[x], x)); } printf("%d/n", ans); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 紫阳县| 东兰县| 交口县| 无棣县| 天峻县| 潞城市| 华蓥市| 安宁市| 通辽市| 六安市| 龙川县| 应城市| 仁布县| 铜梁县| 镇雄县| 达日县| 灵寿县| 望奎县| 郸城县| 博野县| 静宁县| 高陵县| 岳阳县| 呼图壁县| 乡城县| 紫阳县| 满洲里市| 定兴县| 新营市| 鄂温| 于田县| 甘谷县| 福泉市| 石林| 兰州市| 石门县| 乌拉特前旗| 察雅县| 张家港市| 巍山| 息烽县|