hdu 3794 Magic Coupon

好久没写blog了,来一道水题吧,分组贪心

#include <iostream> #include <algorithm> #include <cstdio> using namespace std; typedef long long ll; ll p[1000010]; ll c[1000010]; int main() { int np, nc; ll ans = 0; while(scanf("%d", &nc) != EOF && nc >= 0) { ans = 0; for(int i = 0; i < nc; i++) scanf("%I64d", &c[i]); scanf("%d", &np); for(int i = 0; i < np; i++) scanf("%I64d", &p[i]); sort(c, c + nc); sort(p, p + np); int ci = 0, pi = 0; while(ci < nc && c[ci] <= 0) ci++; while(pi < np && p[pi] <= 0) pi++; int count = min(nc - ci, np - pi); for(int k = 1; k <= count; k++) { ans += c[nc - k] * p[np - k]; } count = min(ci, pi); for(int k = 0; k < ci && k < pi; k++) { ans += c[k] * p[k]; } printf("%I64d/n", ans); } return 0; } 

你可能感兴趣的:(c,Blog)