CodeForces 1154F: Shovels Shop LCP

传送门

题目描述

小a要去水果店给队友们买水果,水果店有 n 个水果 第i个水果需要 a_i 元.

小a需要购买k个水果,每个水果只能购买一次

水果店有m种促销活动,第i种优惠表明小a可以依此优惠购买xi个水果,在购买的xi个水果中,yi个最便宜的可以免费赠送.即买xi个水果免较便宜的yi个水果的花费

小a可以使用同一种优惠多次,也可以使用多种优惠,但不可以同时使用两种优惠,当然,他也可以不使用任何优惠进行购买

请帮助小a计算最小的花费

分析

用f[i]表示买i件物品的最小花费,b[i]表示买i件物品的最多可以免费的数量,b[i]维护前缀和,我们肯定是从价格小开始买,所以状态转移方程为

 f[i] = min(f[i],f[j] + a[i] - a[j + b[i - j]]);

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 200010;
int b[N];
int a[N];
int f[N];
int n,m,k;

int main(){
    
    
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 1;i <= k;i++) f[i] = INF;
    for(int i = 1;i <= n;i++){
    
    
        int x;
        scanf("%d",&a[i]);
    }
    sort(a + 1,a + 1 + n);
    for(int i = 1;i <= n;i++){
    
    
        a[i] = a[i - 1] + a[i];
    }
    while(m--){
    
    
        int x,y;
        scanf("%d%d",&x,&y);
        b[x] = max(b[x],y);
    }
    for(int i = 1;i <= k;i++)
        for(int j = 0;j < i;j++){
    
    
            f[i] = min(f[i],f[j] + a[i] - a[j + b[i - j]]);
        }
    printf("%d\n",f[k]);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/112791969