1 题目描述:

  被给一系列的正整数x1,x2,x3...xn和两个非负整数a和b,通过下面两步操作将a转化为b:

  1.对当前的a减1。

  2.对当前a减去a % xi (i=1,2...n)。

  计算a转化到b最小需要的最短步数。

2 输入

  第一行包含简单的整数n(1 ≤  n ≤ 10^5),第二行包含n个用空格分开的整数x1,x2,x3...xn(2 ≤  xi ≤ 10^9),第三行包含两个整数a和b(0  ≤ b ≤  a ≤ 10^9, a - b ≤ 10^6)。

3 输出

  输出一个整数,a转化为b的最小步数。

4 思路

  假设dp[k]表示从k+b转化到b所需要的最小步数,然后可以通过计算next=(b+k)-(b+k) % xi(i=1,2..n, next >= b)所能达到的最小值来计算dp[k],即dp[k]=dp[next-b]+1,这其实是一个贪心的选择,总是让每一步尽量产生大的跨度。为什么可以这样计算?由于dp[k]是一个递增函数。假设存在t,使得dp[k] = dp[k - t] + 1(k+b通过一步可以转化到k-t+b),如果t=1,显然dp[k-1]<dp[k];如果t > 1, 必然有dp[k-1] <= dp[k - t]+1,因为k+b可以通过一步转化为k-t+b,那么k-1+b也必然存在一步转化为k-t+b(只要k+b先-1转化为k+b-1,然后k+b-1必然就可以转化为k-t+b)。

5实现

 #include <stdio.h>
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include <functional> #define min(a, b) ((a) < (b) ? (a) : (b))
int n, x[], a, b; int main ( int argc, char *argv[] )
{
int i, t, tt, ans; scanf("%d", &n);
for(i = ; i < n; i++)
scanf("%d", &x[i]);
scanf("%d%d", &a, &b);
std::sort(x, x + n, std::greater<int>());
n = std::unique(x, x + n) - x; ans = ;
while (a != b)
{
tt = a - ;
for (i = ; i < n; i++)
{
t = a - (a % x[i]);
if (t < b)
x[i--] = x[--n];
else
tt = min(t, tt);
}
a = tt;
ans++;
}
printf("%d\n", ans);
return ;
} /* ---------- end of function main ---------- */

cf201.div1 Number Transformation II 【贪心】的更多相关文章

  1. Codeforces 346C Number Transformation II 贪心(复杂度计算)

    题意及思路:https://www.cnblogs.com/liuzhanshan/p/6560499.html 这个做法的复杂度看似是O(n ^ 2),实际上均摊是O(n)的.我们考虑两种极端数据: ...

  2. CodeForces346 C. Number Transformation II

    C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. CodeForces 346C Number Transformation II

    Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x ...

  4. Codeforces 346C Number Transformation II 构造

    题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...

  5. CSUOJ 1299 - Number Transformation II 打表预处理水DP

    http://122.207.68.93/OnlineJudge/problem.php?id=1299 第二个样例解释.. 3 6 3->4->6..两步.. 由此可以BFS也可以DP. ...

  6. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  7. 4.Single Number && Single Number (II)

    Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...

  8. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  9. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. yum 只下载不安装

    以下载busybox为例 1.首先确定有yumdownloader 这个软件,这个软件在yum-utils 工具包里面. # rpm -qa |grep yum-utils # yum -y inst ...

  2. C++ Primer Plus 第六版笔记

    C++ Primer Plus 第六版笔记 关于对象声明的思考 转自:http://www.cnblogs.com/weiqubo/archive/2009/11/02/1930042.html C+ ...

  3. HashMap实现缓存

    package com.cache; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.a ...

  4. 关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...

    在 GCC ARM Embedded https://launchpad.net/gcc-arm-embedded/ 上面下载了个arm-none-eabi-gcc 用cmake 编译时 #指定C交叉 ...

  5. 网站中使用中文个性字库字体--@font-face解决方案探索 l(转)

    最近的项目有用到特别中文字体,最终效果如下图: 红线标记处均为字体,可选中,交互起来,比图片方便太多了. 解决思路就是将体积巨大的中文字库,取子集,只包涵要使用的那部分文字,因此体积就很小了(包含10 ...

  6. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  7. Android课程---关于Service的学习(后台运行)

    MainActivity.java package com.hanqi.test2; import android.content.ComponentName; import android.cont ...

  8. Google云平台对于2014世界杯半决赛的预测,德国阿根廷胜!

    由于本人是个足球迷,前段日子Google利用自己云平台预测世界杯八进四的比赛并取得了75%的正确率的事情让我振动不小.虽然这些年一直听说大数据的预测和看趋势能力如何如何强大,但这次的感受更加震撼,因为 ...

  9. 什么情况下才要重写Objective-C中的description方法

    特别注意: 千万不要在description方法中同时使用%@和self,同时使用了%@和self,代表要调用self的description方法,因此最终会导致程序陷入死循环,循环调用descrip ...

  10. 简化 Web 应用程序与 Windows Azure Active Directory、ASP.NET 和 Visual Studio 的集成

    大家好! 今天的博文深入讨论我们今天推出的开发人员工具和框架中的一些新功能.我们通过与 ASP.NET 和 Visual Studio 团队合作开发了一些重大的增强功能,让开发人员能够轻松使用 Win ...