Zoj 3768

本文介绍了一个关于连续登录奖励的游戏算法问题。玩家需达到特定积分,同时保持最少的连续登录中断。文章提供了一种通过组合不同长度连续登录来实现目标积分的有效解决方案,并附带源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

我暴力了些数据,发现答案的个数都没超过3,然后就怀着试一试的想法写了个暴力交了,然后就过了,不知道是数据水了,还是真的可以这样。

#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
using namespace std;

inline int input(){
    int ret=0;
    bool isN=0;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') isN=1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        ret=ret*10+c-'0';
        c=getchar();
    }
    return isN?-ret:ret;
}

int v[20000],cnt=0,a,b,t,n,e;
map<int,int>m;
map<int,int>to;

int main(){
    a=1,b=1;
    m.clear();
    to.clear();
    while(a<=123456789){
        v[cnt]=a;
        m[a]=1;
        to[a]=cnt;
        cnt++;
        b++;
        a+=b;
    }
    t=input();
    while(t--){
        n=input();
        if(m[n]!=0){
            printf("%d\n",to[n]+1);
            continue;
        }
        int tag=1;
        int e=0;
        while(v[e]<n) e++;
        for(int i=0;i<=e;i++){
            if(m[n-v[i]]!=0){
                printf("%d %d\n",i+1,to[n-v[i]]+1);
                tag=0;
                break;
            }
        }
        if(tag){
            for(int i=0;i<=e && tag;i++){
                for(int j=e;j>=0 && tag;j--){
                    a=n-v[i]-v[j];
                    if(m[a]!=0){
                        printf("%d %d %d\n",i+1,j+1,to[a]+1);
                        tag=0;break;
                    }
                }
            }
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值