码迷,mamicode.com
首页 > 其他好文 > 详细

[迭代加深dfs] zoj 3768 Continuous Login

时间:2014-05-13 07:28:07      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   tar   

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3768

Continuous Login

Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge

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.


Author: ZHOU, Yuchen
Source: The 14th Zhejiang University Programming Contest
Submit    Status

题目意思:

给一个数n,把n分解成多个1+2+3+..+i的和的形式(i任意),求中断数最少的分法。

n<=12345678

解题思路:

迭代加深dfs

预处理出12345678内的所有可以被一个凑成的数,然后控制分的部分,然后从小于当前的最大的找起,如果找到就马上退出。

分成的部分数不会很多。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 110000

int dp[Maxn],n;
map<int,int>myp;
vector<int>ans;
bool flag;

void init()
{
    dp[0]=0;
    for(int i=1;i<=20000;i++)
    {
        dp[i]=dp[i-1]+i;
        myp[dp[i]]=i;
    }
    //printf("%d\n",dp[20000]);
}
void dfs(int dep,int cur)
{
    if(dep==1) //已经走完
    {
        if(myp[cur]) //最后一个可以用一部分凑出
        {
            ans.push_back(myp[cur]);
            flag=true;
            return ;
        }
        return ;
    }
    int i=upper_bound(dp+1,dp+20000+1,cur)-dp; //找到小于等于cur的最大的那个
    i--;
    for(;i>=1;i--) //从尽可能大的找起
    {
        ans.push_back(myp[dp[i]]);
        dfs(dep-1,cur-dp[i]); 
        if(flag)
            return ;
        ans.erase(--ans.end());

    }
}

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   init();
   int t;

   scanf("%d",&t);
   while(t--)
   {
       scanf("%d",&n);
       ans.clear();
       flag=false;

       for(int i=1;;i++) //控制部分数
       {
           dfs(i,n);
           if(flag)
           {
               printf("%d",ans[0]);
               for(int j=1;j<ans.size();j++)
                    printf(" %d",ans[j]);
               putchar(‘\n‘);
               break;
           }
       }
   }
   return 0;
}



[迭代加深dfs] zoj 3768 Continuous Login,布布扣,bubuko.com

[迭代加深dfs] zoj 3768 Continuous Login

标签:style   blog   class   code   c   tar   

原文地址:http://blog.csdn.net/cc_again/article/details/25566503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!