zoukankan      html  css  js  c++  java
  • codeforces 710E Generate a String

    题目链接:http://codeforces.com/problemset/problem/710/E

    E. Generate a String
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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.

    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.

    Output

    Print the only integer t — the minimum amount of time needed to generate the input file.

    Examples
    input
    8 1 1
    output
    4
    input
    8 1 10
    output
    8

    题意:要输入n个字符'a',有两种操作,一种是输入或删除一个'a',耗时x;另一种是把当前的整个文本复制粘贴,耗时y。求最少时间。

    打CF的比赛总是不好好读题,一开始就看错题意,以为是随便复制粘贴,结果错了两发。

    考虑递推,用dp[i]表示输入n个字符的最少时间,则边界条件dp[1]=x。

    当i为偶数时,

    对于任意的i>=2,我们考虑最后一步操作,最后一步操作如果是输入一个字符,那么就有dp[i]=dp[i-1]+x。

    最后一步操作如果是复制粘贴,那么就有dp[i]=dp[i/2]+y。

    即dp[i]=min(dp[i-1]+x,dp[i/2]+y)

    当i为奇数时,如果最后一步操作是输入一个字符,同样有dp[i]=dp[i-1]+x。

    但是如果最后一步是复制粘贴的话,因为i是奇数,所以就没办法直接复制粘贴了。

    这里有两种选择,

    一种是从dp[i/2]的状态复制粘贴一遍,然后再插入一个字符。

    即dp[i]=dp[i/2]+x+y

    另一种是从dp[i/2+1]的状态复制粘贴一遍,然后再删除一个字符。

    即dp[i]=dp[i/2+1]+x+y

    所以当i为奇数时,有dp[i]=min(dp[i-1]+x,dp[i/2]+x+y,dp[i/2+1]+x+y)

      

    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int MAX=10000005;
     5 ll dp[MAX];
     6 
     7 int main()
     8 {
     9     ll n,x,y;
    10     scanf("%I64d%I64d%I64d",&n,&x,&y);
    11 
    12     dp[1]=x;
    13     for(int i=2; i<=n; i++)
    14         if(i%2)
    15             dp[i]=min(dp[i-1]+x,min(dp[i/2]+x+y,dp[i/2+1]+x+y));
    16         else
    17             dp[i]=min(dp[i-1]+x,dp[i/2]+y);
    18             
    19     printf("%I64d",dp[n]);
    20     return 0;
    21 }
  • 相关阅读:
    应用默认编码不对的问题定位
    以http server为例简要分析netty3实现
    用qemu+gdb tcp server+CDT调试linux内核启动-起步
    用virtualbox+模拟串口+CDT调试linux内核 TCP/IP协议栈-起步
    【转】常见容错机制
    python文档注释参数获取
    scrapy爬取图片
    xpath语法
    python爬虫爬取赶集网数据
    爬虫小总结
  • 原文地址:https://www.cnblogs.com/from00/p/5799795.html
Copyright © 2011-2022 走看看