[Educational Codeforces Round 16]E. Generate a String

时间:2023-01-30 11:01:02

[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 nx 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.

输出

Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.

输入示例

  

输出示例


数据规模及约定

见“输入

题解

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的更多相关文章

  1. Educational Codeforces Round 16 E&period; Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  2. Educational Codeforces Round 16 E&period; Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

  3. &lbrack;Educational Codeforces Round 16&rsqb;D&period; Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. &lbrack;Educational Codeforces Round 16&rsqb;C&period; Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. &lbrack;Educational Codeforces Round 16&rsqb;B&period; Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  6. &lbrack;Educational Codeforces Round 16&rsqb;A&period; King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Educational Codeforces Round 9 C&period; The Smallest String Concatenation 排序

    C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...

  8. Educational Codeforces Round 8 C&period; Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

  9. Educational Codeforces Round 9 C&period; The Smallest String Concatenation —— 贪心 &plus; 字符串

    题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...

随机推荐

  1. &period;Net中使用OracleDataAdapter

    本来只想简单记录一下OracleDataAdapter的批量增加和修改用法的,在园子里看到一篇比较详细的就在这分享了(Oracle Data Provider for .NET),虽然用的是 Upda ...

  2. MySQL中select &ast; for update锁表的问题

    MySQL中select * for update锁表的问题 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例 ...

  3. java 导出Excel文件

    最近在做一个文件导出功能,发现大部分博客上通过引用各种的util工具包,其实说白了还是利用apache的poi,在项目中直接导入poi包就可以.直面其原理,随个人喜好封装. 1.首先准备一些poi的j ...

  4. Solr整合中文分词组件IKAnalyzer

    我用的Solr是4.10版本, 在csdn下载这个版本的IKAnalyzer:IK Analyzer 2012FF_hf1.zip 解压后目录如下: (1)这里还用solr自带的example实验分词 ...

  5. 如何设置UNIX&sol;Linux中新创建目录或文件的默认权限

    在unix或者linux中,每创建一个文件或者目录时,这个文件或者目录都具有一个默认的权限,比如目录755,文件644,那么这些默认权限是怎么控制的呢? 答案是"umask"权限掩 ...

  6. &lbrack;原&rsqb;Android Native Debug

    1,安装adt插件,cdt插件2,SDK目录配置: Eclipse文件菜单选择“Window”--->“Preferences”--->“Android”--->设置“SDK Loc ...

  7. 外网SSH访问内网LINUX的N种方法

    外网SSH访问内网LINUX的N种方法 http://www.nat123.com/Pages_8_260.jsp 一,动态公网IP环境 1,环境描述: 路由器分配的是动态公网IP,且有路由管理权限, ...

  8. Python处理图片缩略图

    CPU 密集型任务和 IO 密集型任务分别选择多进程multiprocessing.Pool.map 和多线程库multiprocessing.dummy.Pool.map import os imp ...

  9. 开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐!

    作者:编辑部的故事   <  开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐!   > 开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐! Tutoria ...

  10. python3 下列表与字典转换

    在写爬虫的时候,经常需要处理cookie,requests库里的cookie是dict,但是headers['cookie']却是一个key=value的字符串. 下面是几个用推导式实现的转换函数,供 ...