POJ1338Ugly Numbers(DP)
http://poj.org/problem?id=1338
第一反应就是DP,DP[i] = min{2*DP[j], 3*DP[k], 5*DP[p] j,k,p<i};于是枚举一下0~i-1即可
后来听到室友说,可以通过上一个×2、×3、×5得到。于是搞了个优先队列预处理。
后来看了一下以前A的代码。O(n)的。。。(虽然不是我自己做出的= =)
时间都是0Ms
O(n^2)和O(nlogn)的:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a)) typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int num;
int flag;
NODE(){}
NODE(int _num, int _flag){num=_num;flag=_flag;}
bool operator < (NODE B)const
{
return num > B.num;
}
};
int DP[], N; //void init()
//{
// int time = 0;
// for(int i=1;i<=1500;i++) DP[i] = INF;
// DP[1] = 1; int now = 1;
// int two=1, three=1, five=1;
// for(int i=2;i<=1500;i++)
// {
// for(int j=min(two,min(three,five));j<i;j++)
// {
// time ++;
// if(DP[j]*2 > now && DP[i]>DP[j]*2){ DP[i] = min(DP[i], DP[j]*2); two = j;break;}
// if(DP[j]*3 > now && DP[i]>DP[j]*3){ DP[i] = min(DP[i], DP[j]*3); three = j;}
// if(DP[j]*5 > now && DP[i]>DP[j]*5){ DP[i] = min(DP[i], DP[j]*5); five = j;}
// }
// now = DP[i];
// }
// //printf("Time=%d\n", time);
//} void init()
{
priority_queue<NODE>q;
NODE U; U.num=; U.flag=-;
q.push(U);
int num = , tot = ;
while()
{
U = q.top(); q.pop();
DP[num++] = U.num;
if(num>) return ;
if(tot > ) continue;
q.push(NODE(U.num*, )); tot++;
if(U.flag<=) {q.push(NODE(U.num*, )); tot++; }
if(U.flag<=-){q.push(NODE(U.num*, -)); tot++; }
}
} int main()
{
// printf("%d\n", (int)(1600 * (log(1600.0)/log(2.0))));
// freopen("test.in", "r", stdin);
init();
while(~scanf("%d", &N) &&N)
{
printf("%d\n", DP[N]);
}
return ;
}
O(n)的:不是我写的= =
#include <stdio.h>
int min(int a,int b,int c)
{
if(b<a)
a=b;
if(c<a)
a=c;
return a;
}
int main()
{
int n;
int i2_mul;
int i3_mul;
int i5_mul;
unsigned long ugly[]; i2_mul = ;
i3_mul = ;
i5_mul = ;
ugly[]=; for( int i = ; i <= ; i++ )
{
ugly[i] = min(ugly[i2_mul]*,ugly[i3_mul]*,ugly[i5_mul]*);
if(ugly[i] == ugly[i2_mul]* )
i2_mul++;
if(ugly[i] == ugly[i3_mul]* )
i3_mul++;
if(ugly[i] == ugly[i5_mul]*)
i5_mul++; } while(true)
{
scanf("%d",&n); if( n == )
break; printf("%d\n",ugly[n]); } return ;
}
POJ1338Ugly Numbers(DP)的更多相关文章
- Gym 100703G---Game of numbers(DP)
题目链接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s ...
- URAL 1586 Threeprime Numbers(DP)
题目链接 题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数. 求对于一个n位数,存在多少个Threeprime数. 思路 : 记录[100, 999]范围内所有素数( ...
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...
- 【gym102394B】Binary Numbers(DP)
题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...
- 【CF55D】Beautiful numbers(动态规划)
[CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- Humble Numbers(hdu1058)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- asp.net web forms和asp.net mvc比较
ASP.NET Webforms Behind Code的好处和存在的问题 ASP.NET Webforms是一个RAD/VISUAL(快速可视化)的Web程序开发技术.也就是说,开发者简单地拖拽控件 ...
- PHP后台执行
php中实现后台执行的方法: ignore_user_abort(true); // 后台运行set_time_limit(0); // 取消脚本运行时间的超时上限后台运行的后面还要,set_time ...
- BZOJ 3391 Tree Cutting网络破坏
不想写. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> ...
- HDU 5264 pog loves szh I (字符串,水)
题意:设有两个串A和B,现将B反转,再用插入的形式合成一个串.如:A:abc B:efg:反转B先,变gfe:作插入,agbfce.现在给出一个串,要求还原出A和B. 思路:扫一遍O(n),串A在 ...
- (七)7.2 应用机器学习方法的技巧,准确率,召回率与 F值
建立模型 当使用机器学习的方法来解决问题时,比如垃圾邮件分类等,一般的步骤是这样的: 1)从一个简单的算法入手这样可以很快的实现这个算法,并且可以在交叉验证集上进行测试: 2)画学习曲线以决定是否更多 ...
- 【原创】搭建Nginx(负载均衡)+Redis(Session共享)+Tomcat集群
为什么移除首页?哪里不符合要求?倒是回我邮件啊! 一.环境搭建 Linux下Vagrant搭建Tomcat7.Java7 二.Nginx的安装配置与测试 *虚拟机下转至root sudo -i 1)下 ...
- Hadoop学习总结之四:Map-Reduce的过程解析
转:http://www.cnblogs.com/forfuture1978/archive/2010/11/19/1882268.html
- Hadoop学习总结之三:Map-Reduce入门
1.Map-Reduce的逻辑过程 假设我们需要处理一批有关天气的数据,其格式如下: 按照ASCII码存储,每行一条记录 每一行字符从0开始计数,第15个到第18个字符为年 第25个到第29个字符为温 ...
- Xcode5 支持 SVN 1.7
Xcode升级了之后出现了各种问题,SVN升级到subversion 1.7后,Xcode自带有svn,版本是1.6,所以svn的1.6和1.7不兼容. 解决的办法,要么是降低系统的svn 版本,要么 ...
- Oracle 数据库中日期时间的插入操作
Oracle 中如何插入日期时间类型的数据,首先为了演示, 新建数据表如下 create table t( mydate date); 插入日期时间 SQL> insert into t val ...