[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 ...
随机推荐
- css边框阴影
<style type="text/css">.mydiv{width:250px;height:auto;border:#909090 1px solid;backg ...
- DOM(五)事件对象
浏览器中的事件都是以对象的形式存在的,同样ie浏览器与标准dom浏览器之间存在获取事件对象上也存在差别.在ie浏览器中事件对象是windows对象的一个属性event,访问通常采用如下方法. oP.o ...
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- git的牛逼
http://rogerdudler.github.io/git-guide/index.zh.html
- hdu3374 KMP+最大最小表示法
这题要求的是字符串左移时字典序最小和最大的第几次出现,并求出现次数.考虑一会可以发现,出现次数和循环节是有关系的. 出现了几次,就是循环了几次,如果循环节是他本身,也就是无循环,那这个字符串不管怎么移 ...
- 【HDU 1757】 A Simple Math Problem
题 Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 1 ...
- php复习
最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...
- 【uoj150】 NOIP2015—运输计划
http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...
- DLUTOJ 1209 字典序和r-子集
传送门 Time Limit: 6 Sec Memory Limit: 128 MBSubmit: 73 Solved: 14 Description Input 多组输入数据. 每组数据: 第一 ...
- C++ Standard-Library Random Numbers
Extracted from Section 17.4 Random Numbers, C++ Primer 5th. Ed. The random-number library generates ...