• 【 【henuacm2016级暑期训练】动态规划专题 P】Animals


    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    第i只动物如果饲养它的话。 代价是固定的就是(n-i+1)*a[i] 所以相当于给你n个物品,每个物品的重量为(n-i+1)*a[i],价值为1 背包容量为x 问你最大价值是多少。 但因为价值是单位价值。 所以直接重量小的优先选就好了。 不够了就停止。

    【代码】

    #include <bits/stdc++.h>
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define all(x) x.begin(),x.end()
    #define pb push_back
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    using namespace std;
    
    const double pi = acos(-1);
    const int dx[4] = {0,0,1,-1};
    const int dy[4] = {1,-1,0,0};
    
    const int M = 1e5;
    
    int n,x;
    multiset<int> myset;
    
    int main(){
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    	ios::sync_with_stdio(0),cin.tie(0);
    	cin >> n >> x;
        for (int i = 1;i <= n;i++){
            int a;
            cin >> a;
            myset.insert((n-i+1)*a);
        }
        int cnt = 0;
        for (int y:myset){
            if (y>x) break;
            cnt++;
            x-=y;
        }
        cout<<cnt<<endl;
    	return 0;
    }
    
    
  • 相关阅读:
    汉诺塔难题
    函数的两种调用方式

    汉诺塔难题
    汉诺塔难题

    python中对小数取整
    linux中部署apache服务(http服务或者web服务)
    python中如何判断变量类型
    python中求余数
  • 原文地址:https://www.cnblogs.com/AWCXV/p/9337071.html
Copyright © 2020-2023  润新知