http://codeforces.com/contest/747/problem/D

大概的思路就是找到所有两个负数夹着的线段,优先覆盖最小的长度。使得那时候不用换鞋,是最优的。

但是这里有个坑点,就是最后一段,如果最后一段的长度和中间某一段的长度相等,那么应该优先覆盖中间那段,因为中间的那些,如果没覆盖,则换鞋2次,而最后的那一段,换鞋只需一次。

22 11
1 -1 1 2 3 -1 1 2 3 4 5 6 7 -1 1 2 3 4 -1 1 2 3

答案是4

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5 + ;
int a[maxn];
struct node {
int val, id;
node(int vval, int iid) : val(vval), id(iid) {}
bool operator < (const struct node & rhs) const {
if (val != rhs.val) return val < rhs.val;
else return id < rhs.id;
}
};
vector<int>pos;
vector<int>dis;
set<struct node>ss;
bool vis[maxn];
void work() {
int n, k;
cin >> n >> k;
for (int i = ; i <= n; ++i) {
cin >> a[i];
if (a[i] < ) pos.push_back(i);
}
if (k < pos.size()) {
cout << - << endl;
return;
}
if (pos.size() == ) {
cout << << endl;
return;
}
for (int i = ; i < pos.size(); ++i) {
dis.push_back(pos[i] - pos[i - ] - );
ss.insert(node(pos[i] - pos[i - ] - , i - ));
}
int lef = k - pos.size();
for (set<struct node> :: iterator it = ss.begin(); it != ss.end(); ++it) {
// cout << it->val << endl;
if (lef >= it->val) {
// cout << "ff" << endl;
lef -= it->val;
vis[it->id] = true;
} else break;
}
int ans = ;
for (int i = ; i < dis.size(); ++i) {
if (vis[i]) continue;
ans += ;
}
if (lef < n - pos.back()) {
ans += ;
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

D. Winter Is Coming 贪心(好题)的更多相关文章

  1. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  2. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  3. 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题

    https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...

  4. LightOJ 1166 Old Sorting 置换群 或 贪心 水题

    LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...

  5. DP+贪心水题合集_C++

    本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...

  6. POJ:3040-Allowance(贪心好题)

    Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4903 Accepted: 1943 Description ...

  7. 【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands

    看似区间数据结构的一道题 题目描述 Bessie and her friends are playing a unique version of poker involving a deck with ...

  8. 「面试高频」二叉搜索树&双指针&贪心 算法题指北

    本文将覆盖 「字符串处理」 + 「动态规划」 方面的面试算法题,文中我将给出: 面试中的题目 解题的思路 特定问题的技巧和注意事项 考察的知识点及其概念 详细的代码和解析 开始之前,我们先看下会有哪些 ...

  9. 洛谷P5019 铺设道路 题解 模拟/贪心基础题

    题目链接:https://www.luogu.org/problemnew/show/P5019 这道题目是一道模拟题,但是它有一点贪心的思想. 我们假设当前最大的深度是 \(d\) ,那么我们需要把 ...

随机推荐

  1. 自己定义控件 播放GIF动画

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  2. Hive中行列转换

    1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...

  3. python 2.*和3.*的变化

    1.urllib2是python自带的模块,在python3.x中被改为urllib.request,如 <span style="font-size:12px;">u ...

  4. PL/SQL Developer导入导出Oracle数据库方法

    前一篇博客介绍了Navicat工具备份Oracle的方法.这篇博客介绍一下使用PL/SQL Developer工具导入导出Oracle数据库的方法. PL/SQL Developer是Oracle数据 ...

  5. xcode10的那些事

    前言 这里主要介绍一下Xcode10 版本主要更新的内容.随着iOS12的发布,Xcode10已经可以从Mac App Store下载.Xcode10包含了iOS12.watchOS 5.macOS1 ...

  6. POJ1182 食物链 —— 种类并查集

    题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  7. mongodb 关闭服务 mongod -f /root/mongodb/bin/xx.conf --shutdown

    /root/mongodb/bin/mongod -f /root/mongodb/bin/xx.conf --shutdown

  8. spring-boot快速搭建解析

    创建方式: 1.在File菜单里面选择 New > Project,然后选择Spring Initializr: 2.使用maven直接构建,添加依赖. 1 2 3 4 pom.xml:Mave ...

  9. AutoIT: 返回文件目录下所有的文件名,包括子文件

    _filelist("D:\Snagit 10") Func _filelist($searchdir) ;;;函数名(形参) $search = FileFindFirstFil ...

  10. bzoj2809 [Apio2012]dispatching——左偏树(可并堆)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 思路有点暴力和贪心,就是 dfs 枚举每个点作为管理者: 当然它的子树中派遣出去的忍者 ...