传送门:http://codeforces.com/contest/913/problem/C

n类物品,第i(i=0,1,2,...,n-1)类物品的价值为2i,花费为ci。任意选择物品,使得总价值至少为L。求此时的总花费的最小值。(n≤30,L≤109ci109

这是一个完全背包问题,但是鉴于数据规模,常规的DP是不可取的(TLE+MLE)。

首先考虑到的是递归地搜索答案。对于当前状态(cur,k),对价值cur做一个简单的划分:商q=cur/vi,余数r=cur%vi。商作为不可继续划分的部分计算花费:qcost=q*vi*ci,对余数继续搜索rcost,状态为(r,i)。对于当前状态,枚举i=k,k-1,...,0,找到最小的qcost+rcost,作为当前状态的返回值。这个做法一定可以得到正确的答案,但是,如此会TLE。

接下来考虑这个问题的数据是否可以优化。首先考虑c数组:

首先确定c数组的目标状态。这个目标状态应该满足约束:ci≤ci+12ci

ci+1≥ci:保证代价c的序列是上升的;

ci+12ci:保证性价比v/c的序列是上升的。

于是对于不满足约束的cici+1,则约束之。

ci=min(ci,ci+1);

ci+1=min(ci+1,2ci)。

完成c数组的约束之后,可以简单地求解这个问题了。

在约束后,由于代价序列是上升的,因此应从价值最小的物品开始考虑;

由于性价比序列是上升的,因此每一次花费,应获得价值尽可能高的物品。

于是考虑L的二进制位,设获得第0~i位的物品,对应的总花费为ans(i)。

则当第i位为0时,不获得物品i,价值0,花费0:ans(i)=ans(i-1);

当第i位为1时,获得物品i,价值vi,花费cians(i)=ans(i-1)+ci

有一个例外的情况:当获得更高位的物品i+1所花费的ci+1≤ans(i)时,丢弃0~i位的所有物品,转而获得物品i+1:ans(i)=ci+1

于是从低位到高位遍历即可。参考程序如下:

#include <stdio.h>
#include <stdint.h>
#define MAX_N 31 const int64_t inf = (int64_t)1e18;
int64_t c[MAX_N], v[MAX_N];
int64_t minc[MAX_N]; int64_t min(int64_t a, int64_t b)
{
return a < b? a: b;
} int main(void)
{
int n, l;
scanf("%d%d", &n, &l);
for (int i = ; i < n; i++) {
scanf("%d", &c[i]);
if (i) c[i] = min(c[i], 2LL * c[i - ]);
if (i) c[i - ] = min(c[i - ], c[i]);
v[i] = << i;
}
for (int i = n; i < MAX_N; i++)
c[i] = 2LL * c[i - ];
int64_t ans = 0LL;
for (int i = ; i < MAX_N - ; i++) {
int p = << i;
if (l & p) ans += c[i];
ans = min(ans, c[i + ]);
}
printf("%I64d\n", ans);
return ;
}

Codeforces Hello 2018 C - Party Lemonade的更多相关文章

  1. Codeforces Goodbye 2018

    Goodbye 2018 可能是我太菜考试的时候出不了$E$ 可能是我太菜考试的时候调不出$F$ 所以转化为手速场之后手速还上不去.jpg A 模拟题意... #include <cstdio& ...

  2. Codeforces Hello 2018 E题Logical Expression dp+最短路 好题

    j题目链接: http://codeforces.com/contest/913/problem/E 题意: 给你x,y,z三个变量,与&   或|  非!  括号()   四种运算符,规定括 ...

  3. 【CodeForces】913 C. Party Lemonade

    [题目]C. Party Lemonade [题意]给定n个物品,第i个物品重量为2^(i-1)价值为ci,每个物品可以无限取,求取总重量>=L的最小代价.1<=30<=n,1< ...

  4. Codeforces - Gym102028 - 2018 Jiaozuo Regional Contest

    http://codeforces.com/gym/102028 A. Xu Xiake in Henan Province 看起来像水题.乱搞一下,还真是. #include<bits/std ...

  5. Week Five

    2018.12.25 1.[BZOJ 4310] 2.[BZOJ 3879] 3.[BZOJ 2754] 4.[BZOJ 4698] 5.[Codeforces 914E] 6.[Codeforces ...

  6. Codeforces Avito Code Challenge 2018 D. Bookshelves

    Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...

  7. Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...

  8. Codeforces:Good Bye 2018(题解)

    Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...

  9. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】

    C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

随机推荐

  1. Selenium中配置链接使用FTP服务

    Enable the default report solution Step1: Create a suite listener and add codes into it, please watc ...

  2. ROADS - Roads

    N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters a ...

  3. java线程异常处理方法

    工作中常发现有些程序发生异常但却没有错误日志,原因就是一些开发线程异常处理错误,导致程序报错但异常信息打印到堆栈上,不好在生产环境中定位问题. 在java多线程程序中,所有线程都不允许抛出未捕获的ch ...

  4. 洛谷 P1312 [ NOIP 2011 ] Mayan游戏 —— 搜索+模拟

    题目:https://www.luogu.org/problemnew/show/P1312 还是不擅长这种题,所以参考了一下TJ: 其实也很好搜,按字典序,先搜右移,再搜左移: 不交换相同颜色的两个 ...

  5. 67.员工职位变动js

    1.员工职位jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  6. Android Studio笔记

    1. toolbar xml: <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:la ...

  7. hebernate基础学习2---属性

    ---------------------------------------------------------------------------------------------------- ...

  8. RAP接口文档的安装

    本机环境 系统:CentOS 6.7 64 位 MySQL 5.6 JDK 1.8 Tomcat 8 Redis 3.0.7 Rap 0.14.1 Rap 说明 官网:https://github.c ...

  9. 题解报告:hdu 1564 Play a game(找规律博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1564 Problem Description New Year is Coming! ailyanlu ...

  10. 为什么选择Android Studio 而是 Eclipse

    Android Studio 现在的版本已经比较稳定了,刚出来时也是各种BUG,自己用了下,摸索了一天,感觉挺好的. 优点之一:代码提示和搜索功能非常强大,非常智能. 1).自定义theme有个名字叫 ...