hdu5334 Virtual Participation 多校聯合第四場

題意:給你一個數k,讓你輸出一個長度爲n的數列, 該數列滿足 不相等的子序列的個數和爲k 關於不相等的定義題中有給出

思路:規律題  當k小於十萬時,直接輸出k個1,如果題目不要求n的範圍 這道題可以都是輸出k個1.。。。。

當k大於十萬時首先對於一個1到n的數列 它對應的k值爲n*(n+1)/2,然後打表 打到45000 就夠了,然後再判斷表中離k最近切大於k的數是多少,假設爲n,我們想辦法使其減去n即可辦法如下:對於一個1到n的數列,如果我們將其某個數變爲1(不與1相鄰) 那麼其k值就會減一,如果是從2的位置開始 連續將x個數變爲1,比如 1 2 3 4 5 6 7 8將3個數變爲1後爲 1 1 1 1 5 6 4 8,其k值減少的個數爲x(x+1)/2,先按連續變爲1的方案來 如果不夠 再接着不相鄰的變爲1的來 直到使其k值減成與輸入的k相等然後輸出該修改後的序列就行了

看不清楚的話就留言吧。。。。

代碼:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100000
#define mod 100000000
using namespace std;
void mys(int& res)
{
    int flag=0;
    char ch;
    while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
        if(ch==EOF)  res=INF;
    if(ch=='-')  flag=1;
    else if(ch>='0'&&ch<='9')  res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')  res=res*10+ch-'0';
    res=flag?-res:res;
}
void myp(int a)
{
    if(a>9)
        myp(a/10);
    putchar(a%10+'0');
}
/*************************THE END OF TEMPLATE************************/
int arr[N] = {0};
void init(){
    for(int i = 1; i<45000; i++){
        arr[i] = i*(i-1)/2 + i;
    }
}
int getR(int x){
    for(int i=1; i<45000; i++){
        if(arr[i] >= x) return i;
   }
}
int getL(int x){
    for(int i=1; i<45000; i++){
        if(arr[i] <= x && arr[i+1] >x) return i;
    }
}
int main()
{
    int k, i;
    init();
    w(~s(k)){
        int r;
        if(k <= 100000){
            cout<<k<<endl;
            for(int i=1; i<k; i++) cout<<"1 ";
            cout<<"1"<<endl;
        }
        else{
            r = getR(k);
            cout<<r<<endl;
            if(arr[r] == k){
                    for(i=1; i<r; i++) cout<<i<<" ";
                    cout<<i<<endl;
            }
            else{
                int cnt = arr[r] - k;
                int p = getL(cnt);
                int ans = cnt - arr[p];
                cout<<"1 ";
                for(i=2; i<=p+1; i++) cout<<"1 ";
                for(; i<r; i++){
                    if((i-p-1)%2==0 && ans>0){
                        cout<<"1 ";
                        ans--;
                    }
                    else cout<<i<<" ";
                }
                cout<<i<<endl;
            }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章