[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 ...
随机推荐
- Use Windows Azure AD to create SSO projects
Keywords Windows Azure AD, SSO Summary Use Windows Azure AD to create SSO projects Detailed Scenario ...
- EntityFramework_MVC4中EF5 新手入门教程之四 ---4.在EF中创建更复杂的数据模型
在以前的教程你曾与一个简单的数据模型,由三个实体组成.在本教程中,您将添加更多的实体和关系,并通过指定格式. 验证和数据库映射规则,您将自定义数据模型.你会看到自定义的数据模型的两种方式: 通过添加属 ...
- Bootstrap3.0学习第二十三轮(JavaScript插件——警告框)
详情请查看http://aehyok.com/Blog/Detail/29.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- “耐撕”团队 2016.03.24 站立会议
时间: 2016.03.22 17:00-17:30 18:30-19:00 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), ...
- 知道了grunt怎么用了
第一步: //如何安装指定grunt的插件,安装后自动在当前cmd目录下新建node_modules文件夹,文件夹里面包含了下载完成的插件 npm install <module> - ...
- Java基础-gs(垃圾回收)
Java垃圾回收概况 Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之一,作为Java开发者,一般不需要专门编写内存回收和垃圾清理代 ...
- BZOJ1046 [HAOI2007]上升序列
Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ...
- HDU 2242 考研路茫茫----空调教室
传送门 考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Opencv不用每次创建项目配置vs2010 vc++目录 库目录等项
可以设置成编译器的环境配置,VS2010相对其他版本虽然去掉了编译器配置 但可以通过属性管理器配置编译器环境. 设置对应的vc++目录 链接器就可以了,这样就是对整个编译器配置了 下次就不用再配了.
- PL/0编译器(java version)–Praser.java
1: package compiler; 2: 3: import java.io.IOException; 4: import java.util.BitSet; 5: 6: /** 7: ...