Too Much Money

最关键的一点就是这个贪心可以在sqrt(n)级别算出答案。

因为最多有sqrt(n)个不同的数值加入。

我们可以发现最优肯定加入一个。

然后维护一个当前可以取的最大值, 枚举加入的数来贪心。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ;
const double eps = 1e-;
const double PI = acos(-); int tar, n, tot, a[N], hs[N], cnt[N], mx[N], now[N];
vector<int> vc; bool check(int add) {
vc.clear();
int c = tar;
int p = mx[c];
while(c) {
if(add > c) add = ;
while(!now[p] && p) p--;
p = min(p, mx[c]);
if(!p && !add) break;
if(p && hs[p] >= add) {
vc.push_back(p);
int v = hs[p], num = min(now[p], c / v);
now[p] -= num;
c -= num * v;
} else {
c -= add;
add = ;
}
}
for(auto& x : vc) now[x] = cnt[x];
return c;
} int main() {
scanf("%d%d", &tar, &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
hs[++tot] = a[i];
}
sort(hs + , hs + tot + );
tot = unique(hs + , hs + tot + ) - hs - ;
for(int i = ; i <= n; i++) cnt[lower_bound(hs + , hs + tot + , a[i]) - hs]++;
for(int i = ; i <= tot; i++) now[i] = cnt[i];
for(int i = ; i <= tar; i++) mx[i] = upper_bound(hs + , hs + tot + , i) - hs - ;
for(int i = ; i <= tar; i++) {
if(check(i)) {
printf("%d\n", i);
return ;
}
}
puts("Greed is good");
return ;
} /*
*/

Codeforces 725E Too Much Money (看题解)的更多相关文章

  1. Codeforces 1017F The Neutral Zone (看题解)

    这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces 513E2 Subarray Cuts dp (看题解)

    我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...

  3. Codeforces 822E Liar dp + SA (看题解)

    Liar 刚开始感觉只要开个dp[ i ][ j ][ 0 / 1 ]表示处理了s的前 i 个用了 k 段, i 是否是最后一段的最后一个字符 的 t串最长匹配长度, 然后wa24, 就gg了.感觉这 ...

  4. Codeforces 196E Opening Portals MST (看题解)

    Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...

  5. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  6. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  7. Codeforces Round #668 (Div. 2)A-C题解

    A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...

  8. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  9. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

随机推荐

  1. OpenVPN安装过程记录

    1.参考网上OpenVPN 的安装步骤进行安装,此处省略. 2.安装完后进行相应证书和密钥的生成. 3.启动openvpn,查看 netstat -tunlp ,如果有openvpn 1194的监听, ...

  2. ASP.NET MVC5高级编程 之 Ajax

    jQuery不仅支持所有现代浏览器,包括IE.Firefox.Safari.Opera和Chrome等,还可以在编写代码和浏览器API冲突时隐藏不一致性(和错误). 1. jQuery jQuery擅 ...

  3. Laravel 自定义分页、可以调整、显示数目

    {{-- 增加输入框,跳转任意页码和显示任意条数 --}} <ul class="pagination pagination-sm"> <li> <s ...

  4. Servlet随笔

    HttpServlet中的getRequestURL.getRequestURI.getContextPath方法获取的字符串为 jsp文件会被编译成一个Servlet,该Servlet继承自Http ...

  5. [转]golang中defer的使用规则

    转载于:https://studygolang.com/articles/10167 在golang当中,defer代码块会在函数调用链表中增加一个函数调用.这个函数调用不是普通的函数调用,而是会在函 ...

  6. scp命令:远程复制粘贴文件

    文章链接:https://www.cnblogs.com/webnote/p/5877920.html scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有 ...

  7. Elasticsearch入门,这一篇就够了

    实时搜索引擎Elasticsearch Elasticsearch(简称ES)是一个基于Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进 ...

  8. 8大排序之Python实现 冒泡排序优化

    1.冒泡排序(从大到小):交换发生在内部循环 稳定的排序 冒泡排序的平均时间复杂度是O(n2),最好的时间复杂度是O(n),最坏的时间复杂度是O(n2),空间复杂度为O(1) 冒泡排序的优化在于did ...

  9. CSS弹性(flexible)盒子

    弹性盒子         弹性盒子由弹性容器(Flex container)和弹性子元素(Flex item)组成 弹性容器通过display:flex | inline-flex将其定义为弹性容器 ...

  10. C# TTS 文字和英文

    using System;using System.Globalization;using System.Linq;using System.Speech.Synthesis;using System ...