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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 08:43:50

hdu5334 Virtual Participation 多校联合第四场的相关文章

HDOJ多校联合第四场

B题: C题:仅由'A','G','C','T',4个字母组成,给定一个字符串S,|S|<=15,给定一个整数m,以m为长度且仅含4种字母的字符串T,求LCS(S,T)为0,1,2,3....|S|,时相应字符串T的数目. 分析:dp+状态压缩 反正我不会这题,也是看了羊神的代码之后才明白这题的思路 下面说说我的理解吧: 由于|S|长度最大为15,所以用一个二进制编码表示是哪些位置上的字母构成LCS,并求相应的数目. dp[0][st],dp[1][st]记录的是相应字母构成LCS时,T可能的数

hdu5336 多校联合第四场1010 模拟+bfs优先队列

http://acm.hdu.edu.cn/showproblem.php?pid=5336 Problem Description XYZ is playing an interesting game called "drops". It is played on a r?c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "siz

hdu5335 多校联合第四场1009 搜索

http://acm.hdu.edu.cn/showproblem.php?pid=5335 Problem Description In an n?m maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it. An explorer gets lost in th

解题报告 之 HDU5334 Virtual Participation

解题报告 之 HDU5334 Virtual Participation Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he asks rikka to have some practice on codeforces. Then she opens the problem B: Given an integer , she needs to come up wit

HDOJ多校联合第五场

1001 题意:求逆序对,然后交换k次相邻的两个数,使得剩下的逆序对最少. 分析:题目用到的结论是:数组中存在一对逆序对,那么可以通过交换相邻两个数使得逆序对减少1,交换k次,可以最多减少k个. 嘉定ai>aj,i < j,如果ai,aj相邻的,那么显然可以通过交换减少1:不相邻的情况, 考虑ak,k = j-1; #11:ak > aj,那么ak,aj构成逆序对,交换后逆序对减少1: #12:ak<=aj,那么ai,ak构成逆序对,问题转化为更小规模,可以通过同样的方法进一步分析

2014多校联合-第六场

最近这两场好无奈啊... 今天这场最后30分钟敲1001,压力倍增,虽然思路比较明确,但是代码打起来不怎么容易. 但是还是好在25分钟左右debug结束.提交wa,再提交,依然WA.......最后5分钟,还是没有AC掉. 一开始以为是精度问题,后来才sb的发现原来数组开小了. 在压力环境下保证代码的效率和质量真不是一件容易的事情.不过数组开小了,真是不可原谅. 1001:Map 题目相当于几条链表.把链表排成几行. 然后枚举每一列的状态会被操作多少次. 然后把和累加起来,然后直接除以状态总数.

hdu5379||2015多校联合第7场1011 树形统计

http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n vertexs, i

hdu5353||2015多校联合第六场1001 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5353 Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and i-th soda is adjacent to (i+1)-th soda, 1-st soda is adjacent to n-th soda. Each soda has some candies

hdu5360||多校联合第6场1008 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5360 Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hi