zoukankan      html  css  js  c++  java
  • 训练部队--全国模拟(三)

    [编程题] 训练部队
    时间限制:1秒
    空间限制:32768K
    小牛牛是牛牛王国的将军,为了训练出精锐的部队,他会对新兵进行训练。部队进入了n个新兵,每个新兵有一个战斗力值和潜力值,当两个新兵进行决斗时,总是战斗力值高的获胜。获胜的新兵的战斗力值就会变成对手的潜力值 + 自己的战斗力值 - 对手的战斗力值。败者将会被淘汰。若两者战斗力值一样,则会同归于尽,双双被淘汰(除了考察的那个新兵之外,其他新兵之间不会发生战斗) 。小牛牛想知道通过互相决斗之后新兵中战斗力值+潜力值最高的一个可能达到多少,你能帮助小牛牛将军求出来吗? 
    输入描述:
    输入包括n+1行,第一行包括一个整数n(1 ≤ n ≤ 10^5); 接下来的n行,每行两个整数x和y(1 ≤ x,y ≤ 10^9)
     
     
    输出描述:
    输出一个整数,表示新兵中战斗力值+潜力值最高的一个能达到多少。
     
    输入例子:
    2 1 2 2 1
     
    输出例子:
    4
     
    解题思路:
    可以考虑把新兵分为两种类型。
    一种战斗型(战斗值大于潜力值的),一种潜力型(相当于打了他可以获得潜力值),对潜力型的新兵进行战斗力值排序。
    然后一种情况是潜力型中战斗值最高的牛牛去打完剩余的潜力型,因为战斗力值会越大越多,另一种情况考虑打完所有潜力型获得值是固定的,那么在攻击型中找一个能打完所有的潜力型的牛牛,并且战斗力值和潜力值要最大。
    实现就是用的前缀和和二分
     1 #include <bits/stdc++.h>
     2  
     3 using namespace std;
     4  
     5 typedef long long LL;
     6 const int maxn = 100005;
     7 int n;
     8 int goodn,badn;
     9 LL sum[maxn],MAX[maxn];
    10 struct Node {
    11     LL x,y;
    12     Node() {}
    13     Node (const LL &_x,const LL &_y) { x=_x; y=_y; }
    14     bool operator < (const Node &t) const {
    15         if(x ^ t.x) return x < t.x;
    16         return y < t.y;
    17     }
    18 }good[maxn],bad[maxn];
    19 int search(int L, int R, LL x) {
    20     while(L < R) {
    21         int mid = (L + R + 1) >> 1;
    22         if(MAX[mid] < x) L = mid;
    23         else R = mid - 1;
    24     }
    25     return L;
    26 }
    27 LL solve() {
    28     LL ans = 0;
    29     ans = max(ans, sum[search(0, goodn - 1, good[goodn].x)] + good[goodn].x + good[goodn].y);
    30     for(int i = 1; i <= badn; i++) {
    31         int pos = search(0, goodn, bad[i].x);
    32         ans = max(ans, sum[pos] + bad[i].x + bad[i].y);
    33     }
    34     return ans;
    35 }
    36 int main() {
    37     scanf("%d", &n);
    38     for(int i = 1; i <= n; i++) {
    39         LL x, y;
    40         scanf("%lld%lld",&x, &y);
    41         if(x < y) good[++goodn] = Node(x,y);
    42         else bad[++badn] = Node(x,y);
    43     }
    44     sort(good + 1, good + goodn + 1);
    45     MAX[0] = 0; sum[0] = 0;
    46     for(int i = 1; i <= goodn; i++) {
    47         sum[i] = sum[i-1] + good[i].y - good[i].x;
    48         MAX[i] = max(MAX[i-1], good[i].x - sum[i-1]);
    49     }
    50     LL ans = solve();
    51     printf("%lld
    ", ans);
    52     return 0;
    53 }

    引自:https://www.nowcoder.com/discuss/27498?type=0&order=0&pos=4&page=1

  • 相关阅读:
    php目录递归删除
    php嵌套数据
    HTML 标签
    枚举 递归
    传值传址 结构体
    去超市选择要购买的商品 将数组放入集合
    函数
    集合 ArrayList 类
    特殊集合 Stack Queue Hashtable
    二维数组,多维数组
  • 原文地址:https://www.cnblogs.com/qqky/p/6888989.html
Copyright © 2011-2022 走看看