Codeforces Round #660 (Div. 2)B题(思维规律题、贪心)

传送门
题目:Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.

In the beginning, uncle Bogdan wrote on a board a positive integer x consisting of n digits. After that, he wiped out x and wrote integer k instead, which was the concatenation of binary representations of digits x consists of (without leading zeroes). For example, let x=729, then k=111101001 (since 7=111, 2=10, 9=1001).

After some time, uncle Bogdan understood that he doesn’t know what to do with k and asked Denis to help. Denis decided to wipe last n digits of k and named the new number as r.

As a result, Denis proposed to find such integer x of length n that r (as number) is maximum possible. If there are multiple valid x then Denis is interested in the minimum one.

All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?

Note: in this task, we compare integers (x or k) as numbers (despite what representations they are written in), so 729<1999 or 111<1000.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

Next t lines contain test cases — one per test case. The one and only line of each test case contains the single integer n (1≤n≤105) — the length of the integer x you need to find.

It’s guaranteed that the sum of n from all test cases doesn’t exceed 2⋅105.

Output
For each test case, print the minimum integer x of length n such that obtained by Denis number r is maximum possible.

题意:x是一个n位正数,将其每一位数字二进制化会成为一个二进制组合数k
譬如k=111101001 (since 7=111, 2=10, 9=1001).
然后去掉末尾的n位将成为一个新的二进制数r,要求由已知的n输出符合要求的x(这个x对应的r最大),如果同时有几个都符合,则输出最小的那个

看懂题意后,看了一下所给样例1->8,3->998,本能的反应这题可能会是个规律题,而且答案可能就是9和8两个数字的某种组合,事实证明我也想对了,接下来分享一下思考的过程

首先要求去掉n位后数字最大,那么每一位的数要尽可能的长尽可能大(1000是会比0111大的)所以我认为考虑范围就在二进制长达4位的9和8,而且前若干位一定是连续的9
至于后面的数,举个例子,这样讲的更清楚,98和99,分别是10011000和10011001,二者同时去掉两位后都是100110,效果一样且最大,这个时候就需要取98了,那什么时候会需要后面留两个8呢,我们接着看99998和99988,分别是10011001100110011000和10011001100110001000,如果去4位还是有区别的,但这是五位数,去掉五位后就需要取99988了
至此就能看出四位一循环,剩余就是打印打印就能ac了

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;

int t;
int n;
int main()
{
	cin >> t;
	while (t--)
	{
	
		cin >> n;
		int num;
		if (n % 4 == 0)
		{
		
			num = n / 4;
		}
		else
		{
		
			num = n / 4 + 1;
		}
		for (int i = 1; i <= n-num; i++)
		{
		
			cout << "9";
		}
		for (int i = 1; i <= num; i++)
		{
		
			cout << "8";
		}
		cout << endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值