nyoj1032——Save Princess——————【set应用】
Save Princess
- 描述
-
Yesterday, the princess was kidnapped by a devil. The prince has to rescue our pretty princess."OK, if you want to save the beautiful princess, you must answer my questions correctly."the devil says."No problem!"."I’ll ask you t questions. For each question, I’ll tell you an integer n, you must tell me the i th beatuiful number. If your answer is wrong, the princess and you will all die"."But what is the characteristic of the beautiful number?" Pince asks."Beautiful numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, ... shows the first 9 beautiful numbers.By convention, 1 is included. "Can you help the prince to save the princess?
- 输入
- The input for each case is an integer n(1≤n≤5000) and it is terminated by a negative integer.
- 输出
- For each test case, you should print an integer which represents the i th beautiful number.
- 样例输入
-
2
3
-1 - 样例输出
-
2
3 解题思路:用优先队列来存放美丽数,每次从队列中拿出最小的美丽数,由它扩展出三个美丽数,用set的去重功能来判断是否放入set和优先队列。依次从优先队列中出来的就是从小到大的美丽数。#include<stdio.h>
#include<string.h>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int f[4]={2,3,5};
LL a[5500];
void prin(int n){ priority_queue<LL,vector<LL>,greater<LL> >pq;
set<LL>s;
pq.push(1);
s.insert(1);
for(int i=1;;i++){ LL tm=pq.top();
a[i]=tm;
pq.pop();
if(i>n)
break;
for(int i=0;i<3;i++){ LL x=tm*f[i];
if(!s.count(x)){//返回元素x的个数 s.insert(x);
pq.push(x);
}
}
} }
int main(){ int n;
prin(5010);
while( scanf("%d",&n)!=EOF&&n>0){ printf("%lld\n",a[n]);
}
return 0;
}
nyoj1032——Save Princess——————【set应用】的更多相关文章
- Save Princess(丑数)
Save Princess 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Yesterday, the princess was kidnapped by a de ...
- ZOJ 3369 Saving Princess
Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- [转]Laravel 4之路由
Laravel 4之路由 http://dingjiannan.com/2013/laravel-routing/ Laravel 4路由是一种支持RESTful的路由体系, 基于symfony2的R ...
- NHibernate初步使用
1.创建一个网站项目:QuickStart 2.引用程序集:NHibernate.dll 3.更改配置文件加入以下节点: <configSections> <section name ...
- C# 数据操作系列 - 10 NHibernate初试
0. 前言 在上一篇基本讲完了EF Core的入门级教程.从这一篇开始,我们试着去探索一下 .net core平台上更多的ORM框架.那么,这一篇开始我们就来试试NHibernate. 1. NHib ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- 山东省第四届acm.Rescue The Princess(数学推导)
Rescue The Princess Time Limit: 1 Sec Memory Limit: 128 MB Submit: 412 Solved: 168 [Submit][Status ...
- sdutoj 2603 Rescue The Princess
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...
- SDUT 2603:Rescue The Princess
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
随机推荐
- I-team 博客全文检索 Elasticsearch 实战
一直觉得博客缺点东西,最近还是发现了,当博客慢慢多起来的时候想要找一篇之前写的博客很是麻烦,于是作为后端开发的楼主觉得自己动手丰衣足食,也就有了这次博客全文检索功能Elasticsearch实战,这里 ...
- vs2015+opencv3.3.1 实现 c++ 彩色高斯滤波器(Gaussian Smoothing, Gaussian Blur, Gaussian Filter)
//高斯滤波器 https://github.com/scutlzk#include <opencv2\highgui\highgui.hpp> #include <iostream ...
- NSSize 尺寸
前言 结构体,这个结构体用来表示事物的宽度和高度. typedef CGSize NSSize; struct CGSize { CGFloat width; CGFloat height; }; t ...
- Mysql初识数据库《二》数据库管理软件的由来
数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件都运 ...
- luoguP1401 城市
https://www.luogu.org/problemnew/show/P1401 二分答案网络流判断是否可行即可 #include <bits/stdc++.h> using nam ...
- luoguP2495 [SDOI2011]消耗战
https://www.luogu.org/problemnew/show/P2495 Dp 方程很显然 设 Dp[u] 表示--使 u 不与其子树中任意一个关键点联通的最小代价 设 w[a, b] ...
- loj #6226. 「网络流 24 题」骑士共存问题
#6226. 「网络流 24 题」骑士共存问题 题目描述 在一个 n×n\text{n} \times \text{n}n×n 个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上 ...
- Kafka,Mq,Redis作为消息队列有何差异?
Kafka作为新一代的消息系统,mq是比较成熟消息系统,而redis也可以发布订阅,那么这三者有何异同? RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,X ...
- 用Hadoop,还是不用Hadoop?
本文由 伯乐在线 - Lex Lian 翻译.英文出处:Anand Krishnaswamy.欢迎加入翻译小组. Hadoop通常被认定是能够帮助你解决所有问题的唯一方案. 当人们提到“大数据”或是“ ...
- linux操作之文本编辑器
1.文本编辑器的作用 编辑和修改系统中的那些以文本形式存在的文件(特别是各种配置文件),也可以用于 编写程序代码 2.linux下的常见编辑器 nano.Emacs.gedit.vim等 3.vim三 ...