[Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String
试题描述
zscoder wants to generate an input file for some programming competition problem.
His input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor.
Initially, the text editor is empty. It takes him x seconds to insert or delete a letter 'a' from the text file and y seconds to copy the contents of the entire text file, and duplicate it.
zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters 'a'. Help him to determine the amount of time needed to generate the input.
输入
The only line contains three integers n, x and y (1 ≤ n ≤ 107, 1 ≤ x, y ≤ 109) — the number of letters 'a' in the input file and the parameters from the problem statement.
输出
输入示例
输出示例
数据规模及约定
见“输入”
题解
dp,设 f(i) 表示凑到 i 需要的最小花费,那么
1. i 为奇数,f(i) = min{ f(i-1) + x, f((i+1)/2) + y + x },注意到奇数不可能从某个数乘 2 得到。
2. i 为偶数,f(i) = min{ f(i-1) + x, f(i / 2) + y },注意到偶数不可能从奇数减 1 得到,否则不是最优的。
O(n) dp 一下就好了。
我的方法:
设 f(i, j) 表示凑到 i 用了 j 次删除的最小花费,则
1. i 为奇数 f(i, j) = min{ f(i-1, j) + x, f(i+1, j-1) + x }
2. i 为偶数 f(i, j) = min{ f(i-1, j) + x, f(i/2, j) + y, f(i+1, j-1) + x }
感觉减法最多不会超过 log(n) 次,所以 j 就只做到 log(n),时间复杂度是 O(nlogn) 的,需要开滚动数组。woc 这个算法在 cf 上居然 1996 ms 过了!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 10000110
#define maxlog 25
#define LL long long
int n, x, y, cur;
LL f[maxn][2]; int main() {
n = read(); x = read(); y = read(); for(int j = 0; j < 2; j++)
for(int i = 1; i <= n + maxlog; i++) f[i][j] = (1ll << 60);
LL ans = 1ll << 60;
for(int j = 0; j < maxlog; j++, cur ^= 1) {
for(int i = 1; i <= n + maxlog; i++) f[i][cur] = (1ll << 60);
for(int i = 1; i <= n + maxlog; i++) {
f[i][cur] = f[i-1][cur] + x;
if(!(i & 1)) f[i][cur] = min(f[i][cur], f[i>>1][cur] + y);
f[i][cur] = min(f[i][cur], f[i+1][cur^1] + x);
// f[i][j] = min(min(f[i-1][j] + x, f[i>>1][j] + y), f[i+1][j-1] + x);
if(i == n) ans = min(ans, f[i][cur]);
// if(i == n) printf("%d %d %I64d\n", i, j, f[i][cur]);
}
} printf("%I64d\n", ans); return 0;
}
[Educational Codeforces Round 16]E. Generate a String的更多相关文章
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
随机推荐
- Bootstrap系列 -- 30. 按钮工具栏
在富文本编辑器中,将按钮组分组排列在一起,比如说复制.剪切和粘贴一组:左对齐.中间对齐.右对齐和两端对齐一组.Bootstrap框架按钮工具栏也提供了这样的制作方法,你只需要将按钮组“btn-grou ...
- maven integration with eclipse 3.0.4 does not work with NTLM proxy
Recently downloaded m2e(maven integration with eclipse). The version is 3.0.4. My environment is beh ...
- css限制图片大小,避免页面撑爆
/*==========限制图片大小======避免页面撑暴========*/img { max-width:100%;width:expression(width>669?"100 ...
- angular的splitter案例学习
angular的splitter案例学习,都有注释了,作为自己的备忘. <!DOCTYPE html> <html ng-app="APP"> <he ...
- tableviewCell的xib中collectionView签协议
- Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合
题目链接: http://codeforces.com/problemset/problem/272/D D. Dima and Two Sequences time limit per test2 ...
- Maven 初学(一)基本概念
Pom中有三个主要元素 Groupid,artifactid,version goupid 是一个组织唯一的标识 例如 com.ibm.www artifactid 是一个工程呢ID ...
- 读JS高级(兼容&&BOM&&私有变量&&面向对象)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Hibernate-清理一级缓存
Session执行一些sql语句把内存中的对象的状态同步到数据库,这个过程被称为session清理. 在默认情况下,Session会在下面的时间点清理缓存. 1 当应用程序调用net.sf.hiber ...
- 【CodeForces 618B】Guess the Permutation
题 题意 有个1到n的一个全排列,告诉你第i个数和全部n个数相比的较小的是哪个,和自己相比时为0,于是有个主对角线为0的矩阵,求原数列 分析 我的想法是,给我们的每一行之和按大小排一下,就知道第i个数 ...