-
题意:给你两个正整数\(N\)和\(S\),构造一个长度为\(N\)并且所有元素和为\(S\)的正整数数组,问是否能找到一个\(K (0\le K \le S)\)使得这个数组的任意_子数组_的和都不等于\(K\)或\(S-K\),如果存在则输出YES,并且输出这个数组和\(K\),不存在则输出\(NO\).
-
题解:这类题写多了其实就会发现基本上都是一连串1或2加上一个数来构造,这题也是如此.
我们可以将这个数组构造为\(N-1\)个\(1\)和一个\(S-N+1\),很明显,只要\((N-1,S-N+1)\)这个区间里面的元素个数\(>1\)即可.那么\(K\)只要取\(S-N\)就行了.
-
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #define ll long long #define fi first #define se second #define pb push_back #define me memset const int N = 1e6 + 10; const int mod = 1e9 + 7; using namespace std; typedef pair<int,int> PII; typedef pair<long,long> PLL; int n,s; int main() { ios::sync_with_stdio(false);cin.tie(0); cin>>n>>s; int cnt1=n-1; // 1的数目 int cnt2=s-cnt1; //末位数字 if(cnt1+1<cnt2){ puts("YES"); int tmp=cnt1; while(tmp--) printf("1 "); printf("%d\n",cnt2); printf("%d\n",cnt2-1); } else puts("NO"); return 0; }
相关文章
- 01-17Codeforces Round #705 (Div. 2) D. GCD of an Array
- 01-17Codeforces Round #509 (Div. 2) E. Tree Reconstruction (构造,思维)
- 01-17Codeforces Round #589 (Div. 2)D(思维,构造)
- 01-17D. GCD of an Array(Codeforces Round #705 (Div. 2)题解)
- 01-17Codeforces Round #715 (Div. 2) D. Binary Literature (构造)
- 01-17Educational Codeforces Round 63 (Rated for Div. 2) B. Game with Telephone Numbers 博弈思维+模拟+贪心思维
- 01-17Codeforces Round #741 (Div. 2) D. Two Hundred Twenty One (easy & hard version)(思维 + 前缀和)
- 01-17Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array (简单DP)
- 01-17Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
- 01-17Codeforces Round #643 (Div. 2) D. Game With Array (思维,构造)