D. Winter Is Coming 贪心(好题)
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 贪心(好题)的更多相关文章
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题
https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...
- LightOJ 1166 Old Sorting 置换群 或 贪心 水题
LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...
- DP+贪心水题合集_C++
本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...
- POJ:3040-Allowance(贪心好题)
Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4903 Accepted: 1943 Description ...
- 【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands
看似区间数据结构的一道题 题目描述 Bessie and her friends are playing a unique version of poker involving a deck with ...
- 「面试高频」二叉搜索树&双指针&贪心 算法题指北
本文将覆盖 「字符串处理」 + 「动态规划」 方面的面试算法题,文中我将给出: 面试中的题目 解题的思路 特定问题的技巧和注意事项 考察的知识点及其概念 详细的代码和解析 开始之前,我们先看下会有哪些 ...
- 洛谷P5019 铺设道路 题解 模拟/贪心基础题
题目链接:https://www.luogu.org/problemnew/show/P5019 这道题目是一道模拟题,但是它有一点贪心的思想. 我们假设当前最大的深度是 \(d\) ,那么我们需要把 ...
随机推荐
- hibernate4中HHH000273的错误
今天配置hibernate4.发现报 17:55:06,815 INFO AbstractPoolBackedDataSource:522 - Initializing c3p0 pool... co ...
- C语言将10进制转为2进制
第一种方法: #include<stdio.h> void dectobin(int n); int main() { int x=0; scanf("%d",& ...
- FileWriter简单用法并记录日志
import java.io.*; import java.util.Date; /** * Created by Administrator on 2018/4/6. */ public class ...
- 图像处理之基础---很好的一个快速比较两副图片是否相同的code 可用于公安鉴别
转自Codeproject http://www.codeproject.com/dotnet/comparingimages.asp Public Enum CompareResult ciComp ...
- ranlib
1 ranlib的缩写 random access library 2 ranlib的作用 为静态库的符号建立索引,可以加速链接,因此称用ranlib处理过的library为random access ...
- winform 无法修改控件的location
dock and location 是因为设置了控件的Dock,导致无法修改
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
一.通过动画实现 定义res/anim/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
- codeforces 689A A. Mike and Cellphone(水题)
题目链接: A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input s ...
- 《MuseGAN: Multi-track Sequential Generative Adversarial Networks for Symbolic Music Generation and Accompaniment》论文阅读笔记
出处:2018 AAAI SourceCode:https://github.com/salu133445/musegan abstract: (写得不错 值得借鉴)重点阐述了生成音乐和生成图片,视频 ...
- 【179】IDL 读写 NetCDF 文件
NetCDF(network Common Data Form)由位于科罗拉多州波尔市的 Unidata 程序中心开发,主要应用于大气科学的研究.NetCDF 的数据模式具有简单性和灵活性的特点.Ne ...