貪欲法

SRM 588 div 1

easy撃墜されて0pointだった 1225 -> 1194easy N=1のケースで撃墜されるコードを書いてしまった。 DP思いつかない人なので貪欲で解いた。 最大m個の歌を歌えるとして a1, a2, a3, ... , am の歌を歌うのにかかる時間Sは S = Σ(duration) + Σ(wait_time) = Σ(…

POJ 2709 Painter

問題 http://poj.org/problem?id=2709 #include <cstdio> #include <algorithm> using namespace std; int main(){ int N; while(scanf("%d", &N), N!=0){ int p[N], g; for(int i=0;i<N; i++) scanf("%d", &p[i]); scanf("%d", &g); while(g > 0){ sort(p, p+N); p[0]++; p[1]++; p[2]++; g--; } sort(p,p+N); printf("%d\n"…</n;></algorithm></cstdio>

AOJ_0220 Binary Digit A Doctor Loved

問題 解法 普通に2進数に変換すればいい。 #include <cstdio> #include <algorithm> using namespace std; const double EPS = 1e-9; const double bit[] = {128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.5, 0.25, 0.125, 0.0625}; int main(){ double r; while(scanf("%lf"</algorithm></cstdio>…

AOJ_0227 Thanksgiving

問題 解法 貪欲法を使って、値段の高いものから順に袋に入れていけばいい。 つまり、値段の高い順にソートして、mで割りきれる番号の値段以外の合計を求める。 #include <cstdio> #include <algorithm> using namespace std; const int MAX_N = 1000; int yasai[MAX_N]; int mai</algorithm></cstdio>…

AOJ_0112 A Milk Shop

解法 かかる時間が小さいほうから並べて足していくだけでいい。 カンニングしたら、signed int型だとオーバーフローするケースが あるらしいと分かったので、long longを使う。 コード #include <cstdio> #include <algorithm> using namespace std; const int MAX_N = 50000; i</algorithm></cstdio>…

AOJ_2271

やるだけ。 一番枚数の少ない文字の数が、作れる看板の数の最大値である。 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s[310]; int main(){ int K, U, P, C; K = U = P = C = 0; scanf("%s", s); for(int i = 0; i < strlen(s); i++){ if(s[i</algorithm></cstring></cstdio>…