Pierre is recently obsessed with an online game. To encourage users to log in, this game will give users a continuous login reward. The mechanism of continuous login reward is as follows: If you have not logged in on a certain day, the reward of that day is 0, otherwise the reward is the previous day's plus 1.
On the other hand, Pierre is very fond of the number N. He wants to get exactly N points reward with the least possible interruption of continuous login.
Input
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
There is one integer N (1 <= N <= 123456789).
Output
For each test case, output the days of continuous login, separated by a space.
This problem is special judged so any correct answer will be accepted.
Sample Input
4 20 19 6 9
Sample Output
4 4 3 4 2 3 2 3
Hint
20 = (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4)
19 = (1 + 2 + 3) + (1 + 2 + 3 + 4) + (1 + 2)
6 = (1 + 2 + 3)
9 = (1 + 2) + (1 + 2 + 3)
Some problem has a simple, fast and correct solution.
题目的意思是给出一个数n是的n分成若干的数的和,每个数是重1累加奥x的,保证数字个数最少
找规律发现最多出现3项,分别对1项2项3项讨论,枚举加二分
#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include <cstring>
using namespace std;
#define LL long long
int a[100005];
int main()
{
int T,n;
a[0]=0;
for(int i=1;i<100005;i++)
a[i]=a[i-1]+i;
scanf("%d",&T);
while(T--)
{
int flag=0;
scanf("%d",&n);
for(int i=0;i<100000;i++)
{
if(a[i]==n){
flag=1;
printf("%d\n",i);
break;
}
if(a[i]>n) break;
}
if(flag) continue;
for(int i=0;a[i]<n;i++)
{
int x=n-a[i];
int pos=lower_bound(a,a+100000,x)-a;
if(a[pos]==x)
{
flag=1;
printf("%d %d\n",i,pos);
break;
}
}
if(flag) continue;
for(int i=0;a[i]<n;i++)
{
for(int j=0;a[j]<n-a[i];j++)
{
int x=n-a[i]-a[j];
int pos=lower_bound(a,a+100000,x)-a;
if(a[pos]==x)
{
flag=1;
printf("%d %d %d\n",i,j,pos);
break;
}
}
if(flag) break;
}
}
return 0;
}

本文介绍了一个有趣的算法问题:如何用最少的连续天数达到指定的登录积分奖励。通过分析问题特性,采用枚举与二分查找相结合的方法找到最优解。
342

被折叠的 条评论
为什么被折叠?



