• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

LA 3507 Keep the Customer Satisfied (Greedy)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1508

  贪心,经典题的变形,只是变成了要消耗时间,其实还是跟经典的求最大价值相似。

View Code
 1 #define REP(i, n) for (int i = 0; i < (n); i++)
 2 #define PRIQ priority_queue
 3 typedef pair<int, int> PII;
 4 typedef vector<PII> VPII;
 5 
 6 PRIQ<int, VI, less<int> > Q;
 7 VPII rec;
 8 
 9 int main() {
10 //    freopen("in", "r", stdin);
11     int T, n, q, d;
12     scanf("%d", &T);
13     while (T--) {
14         scanf("%d", &n);
15         rec.clear();
16         REP(i, n) {
17             scanf("%d%d", &q, &d);
18             rec.PB(MPR(d, q));
19         }
20         sort(ALL(rec));
21         while (!Q.empty()) Q.pop();
22         int sum = 0;
23         REP(i, n) {
24             if (sum + rec[i].SE > rec[i].FI) {
25                 if (!Q.empty()) {
26                     if (Q.top() > rec[i].SE && (sum - Q.top() + rec[i].SE <= rec[i].FI)) {
27                         sum -= Q.top();
28                         Q.pop();
29                         sum += rec[i].SE;
30                         Q.push(rec[i].SE);
31                     }
32                 }
33             } else {
34                 sum += rec[i].SE;
35                 Q.push(rec[i].SE);
36             }
37         }
38         printf("%d\n", SZ(Q));
39         if (T) puts("");
40     }
41     return 0;
42 }

 

——written by Lyon

posted @ 2013-03-02 01:06  LyonLys  阅读(190)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3