NOI 4.5 动态规划 1249: Humble Numbers(单调队列)

题目来源:http://noi.openjudge.cn/ch0405/1249/

1249: Humble Numbers

总时间限制1000ms    内存限制65536kB

描述

A number whose only prime factors are 2,3,5 or 7 is called ahumble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18,20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 

Write a program to find and print the nth element in this sequence.

输入

The input consists of one or more test cases. Each test caseconsists of one integer n with 1 <= n <= 5842. Input is terminated by avalue of zero (0) for n.

输出

For each test case, print one line saying "The nth humblenumber is number.". Depending on the value of n, the correct suffix"st", "nd", "rd", or "th" for theordinal number nth has to be used like it is shown in the sample output.

样例输入

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

样例输出

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

来源

Ulm Local 1996

 -----------------------------------------------------

思路

【题意】

humble数指的是因子只有2,3,5,7的数。给出索引,求第索引个humble数。

【题解】

单调队列,类似NOI 3.4 队列 2729:Blah数集(单调队列)。在老的humble数基础上构造新的humble数有4种方式:*2,*3,*5,*7.故开4个指针h1,h2,h3,h4分别指向队尾的4个数(可能相同),分别表示构造规律为*2,*3,*5,*7。用tail表示队尾元素。每次比较h1,h2,h3,h4构造的新数,维护tail.

NOI 4.5 动态规划 1665:完美覆盖,由于计算靠后的humble数的同时靠前的humble数也被计算了出来,所以用一个vector存储输入索引,在计算过程中实时判断,实时输出,比每输入一个索引就计算一次要节省计算量。

特别注意索引尾巴”th,st,nd,rd”的变化规律。凡是模10011,12,13的结尾用”th”,除此之外模101,2,3的结尾分别用”st,nd,rd”,剩下的结尾都用”th”.

-----------------------------------------------------

代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int q[5843] = {};

int main()
{
	int n;
	vector<int> in;
	while (cin >> n)
	{
		if (n==0)
		{
			break;
		}
		in.push_back(n);
	}
	sort(in.begin(), in.end());
	q[1] = 1;
	int h1=1,h2=1,h3=1,h4=1,x1,x2,x3,x4,tail=1,i;
	if (in.at(0)==1)
	{
		cout << "The 1st humble number is 1." << endl;
		i = 1;
	}
	else
	{
		i = 0;
	}
	while (tail<in.back())
	{
		x1 = q[h1]*2;
		x2 = q[h2]*3;
		x3 = q[h3]*5;
		x4 = q[h4]*7;
		if (x1 < x2 && x1 < x3 && x1 < x4)
		{
			q[++tail] = x1;
			h1++;
		}
		else if (x2 < x1 && x2 < x3 && x2 < x4)
		{
			q[++tail] = x2;
			h2++;
		}
		else if (x3 < x1 && x3 < x2 && x3 < x4)
		{
			q[++tail] = x3;
			h3++;
		}
		else if (x4 < x1 && x4 < x2 && x4 < x3)
		{
			q[++tail] = x4;
			h4++;
		}
		else if (x1==x2 && x1 < x3 && x1 < x4)
		{
			q[++tail] = x1;
			h1++;
			h2++;
		}
		else if (x1==x3 && x1 < x2 && x1 < x4)
		{
			q[++tail] = x1;
			h1++;
			h3++;
		}
		else if (x1==x4 && x1 < x2 && x1 < x3)
		{
			q[++tail] = x1;
			h1++;
			h4++;
		}
		else if (x2==x3 && x2 < x1 && x2 < x4)
		{
			q[++tail] = x2;
			h2++;
			h3++;
		}
		else if (x2==x4 && x2 < x1 && x2 < x3)
		{
			q[++tail] = x2;
			h2++;
			h4++;
		}
		else if (x3==x4 && x3 < x1 && x3 < x2)
		{
			q[++tail] = x3;
			h3++;
			h4++;
		}
		else if (x1==x2 && x2==x3 && x1 < x4)
		{
			q[++tail] = x1;
			h1++;
			h2++;
			h3++;
		}
		else if (x1==x2 && x2==x4 && x1 < x3)
		{
			q[++tail] = x1;
			h1++;
			h2++;
			h4++;
		}
		else if (x1==x3 && x3==x4 && x1 < x2)
		{
			q[++tail] = x1;
			h1++;
			h3++;
			h4++;
		}
		else if (x2==x3 && x3==x4 && x2 < x1)
		{
			q[++tail] = x2;
			h2++;
			h3++;
			h4++;
		}
		else
		{
			q[++tail] = x1;
			h1++;
			h2++;
			h3++;
			h4++;
		}
		if (tail==in.at(i))
		{
			if (tail%100==11 || tail%100==12 || tail%100==13)
			{
				cout << "The " << tail << "th humble number is " << q[tail] << "." << endl;
			}
			else if (tail%10==1)
			{
				cout << "The " << tail << "st humble number is " << q[tail] << "." << endl;
			}
			else if (tail%10==2)
			{
				cout << "The " << tail << "nd humble number is " << q[tail] << "." << endl;
			}
			else if (tail%10==3)
			{
				cout << "The " << tail << "rd humble number is " << q[tail] << "." << endl;
			}
			else
			{
				cout << "The " << tail << "th humble number is " << q[tail] << "." << endl;
			}
			i++;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80951484