D. Game With Array(构造)

题目传送门

题意
要求你组成一个有n个正整数元素的数组,使得数组元素总和为s,要求存在一个k,(0<=k<=s),使得找不到任何非空子数组的元素和等于k或s-k,如果有这样的数组,任意输出数组元素,并且输出k。

思路
构造一个数组,前面都是1,最后一个是s-n+1,这个数组,如果s-n+1>n,那么k=n的时候,就找不到一个子数组,使得子数组和为k或s-k。

代码

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e6+5;
const int inf=0x7fffffff;
const int mod=998244353;
const double eps=1e-6;
signed main()
{
    int n,s;
    cin>>n>>s;
    if(s-n+1>n)
    {
        cout<<"YES"<<endl;
        for(int i=1;i<n;i++)
            cout<<1<<' ';
        cout<<s-n+1<<endl;
        cout<<n<<endl;
    }
    else
        cout<<"NO"<<endl;
}

原创文章 144 获赞 13 访问量 8651

猜你喜欢

转载自blog.csdn.net/Joker_He/article/details/106171171