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. FastCGI sent in stderr: "PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '&lt;' not found in

    2018-4-16 17:59:03 星期一 1. 发送带有xml参数的请求时如果是用php curl, 需要将数组形式的post data 用 http_build_query() 转换 2. 接收 ...

  2. git pull/fectch

    git remote: show all remote repositories git push -u <X1> <>: set x1 as the default repo ...

  3. List<T>常用操作

    1.List<T>类型强制转换: List<UIData> datalist=null;datalist.ConvertAll<object>(input => ...

  4. C和C++相互调用

    在项目中融合C和C++有时是不可避免的,在调用对方的功能函数的时候,或许会出现这样那样的问题.近来在主程序是C语言,而调用C++功能函数的时候,C++的*.h头文件都能找到,功能函数也都定义了,最重要 ...

  5. python学习第4天

    03 初识列表 why: 字符串的缺点: 1,只能存储少量的数据. 2,s = '1True[1,2,3]' 无论索引,切片 获取的都是字符串类型,单一,转化成它原来的类型还需要再一步转换. int( ...

  6. 利用zxing生成二维码

    使用zxing类库可以很容易生成二维码QRCode,主要代码如下: private Bitmap createQRCode(String str,int width,int height) { Bit ...

  7. java移位运算符:<<(左移)、>>(带符号右移)和>>>(无符号右移)。

    1. 左移运算符 左移运算符<<使指定值的所有位都左移规定的次数. 1)它的通用格式如下所示: value << num num 指定要移位值value 移动的位数. 左移的规 ...

  8. iOS UIDatePicker设置为中文的方法

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 20, 200, 30)]; datePick ...

  9. FTRL优化算法

    飞机票 FTRL

  10. js调用ajax案例2,使用ok

    XMLHttpRequest 是 AJAX 的基础. XMLHttpRequest 对象所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject) ...