• Gym 100531B Buffcraft (贪心+暴力+前缀和)


    题意:给定两个加血的方式,一个是直接加多少,另一种是加百分之几,然后你能够你选 k 种,问你选哪 k 种。

    析:首先肯定要选加的多的,所以我们先排序,从大到小,然后用前缀和存储一下,再去枚举从第一种和从第二种选 i 个,从另一个中选 k-i的,

    注意这个 k 可能大于 m+n,讨论一下。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define debug puts("+++++")
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 50000 + 5;
    const LL mod = 1e3 + 7;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }
    inline int lcm(int a, int b){  return a * b / gcd(a, b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Node{
        int num, id;
        bool operator < (const Node &p) const{
            return num > p.num;
        }
    };
    Node a[maxn], b[maxn];
    LL sum1[maxn], sum2[maxn];
    
    void print(int n, Node *a){
        for(int i = 1; i <= n; ++i)
            if(1 == i)  printf("%d", a[i].id);
            else printf(" %d", a[i].id);
        printf("
    ");
    }
    
    int main(){
        freopen("buffcraft.in", "r", stdin);
        freopen("buffcraft.out", "w", stdout);
        int base, k;
        while(scanf("%d %d %d %d", &base, &k, &n, &m) == 4){
            for(int i = 1; i <= n; ++i){
                scanf("%d", &a[i].num);
                a[i].id = i;
            }
            for(int i = 1; i <= m; ++i){
                scanf("%d", &b[i].num);
                b[i].id = i;
            }
            if(k >= m + n){
                printf("%d %d
    ", n, m);
                print(n, a);
                print(m, b);
                continue;
            }
            sort(a+1, a+n+1);
            sort(b+1, b+m+1);
            sum1[0] = sum2[0] = 0;
            for(int i = 1; i <= n; ++i) sum1[i] = sum1[i-1] + a[i].num;
            for(int i = 1; i <= m; ++i) sum2[i] = sum2[i-1] + b[i].num;
            LL ans = 0;
            int idx = 0;
            for(int i = 0; i <= n && i <= k; ++i){
                if(m < k - i) continue;
                LL tmp = (base + sum1[i]) * (100 + sum2[k-i]);
                if(tmp > ans){
                    ans = tmp;
                    idx = i;
                }
            }
            for(int i = 0; i <= m && i <= k; ++i){
                if(n < k - i)  continue;
                LL tmp = (base + sum1[k-i]) * (100 + sum2[i]);
                if(tmp > ans){
                    ans = tmp;
                    idx = -i;
                }
            }
    
            if(idx < 0)  idx += k;
            printf("%d %d
    ", idx, k - idx);
            print(idx, a);
            print(k - idx, b);
        }
        return 0;
    }
    
  • 相关阅读:
    测试EF6.1.3和OrmLite性能
    MVC自定义分页(附表跳转页Loading提示)
    仿京东手机充值进度导航
    PTA(Advanced Level)1052.Linked List Sorting.
    用双指针法巧解——PTA(Advanced Level)1032.Sharing
    PTA(Advanced Level)1022.Digital Library
    PTA(Advanced Level)1071.Speech Patterns
    PTA(Advanced Level)1054.The Dominant Color
    PTA(Advanced Level)1100.Mars Numbers
    PTA(Advanced Level)1060.Are They Equal
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5990989.html
Copyright © 2020-2023  润新知