思路:
在1, 2, 3, ... , k的基础上贪心构造。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int a[100005]; 5 int main() 6 { 7 int n, k; 8 while (cin >> n >> k) 9 { 10 if (n < (ll)k * (k + 1) / 2) { cout << "NO" << endl; continue; } 11 if (k == 1) { cout << "YES " << n << endl; continue; } 12 if (k == 2 && n == 4) { cout << "NO" << endl; continue; } 13 for (int i = 1; i <= k; i++) a[i] = i; 14 ll r = n - k * (k + 1) / 2; 15 int d = r / k, t = r % k; 16 for (int i = 1; i <= k; i++) a[i] += d; 17 while (t && a[k] < a[k - 1] * 2) { a[k]++; t--; } 18 int i = k - 1; 19 for ( ; i > k - 1 - t; i--) a[i]++; 20 if (a[i + 1] > a[i] * 2) cout << "NO" << endl; 21 else 22 { 23 cout << "YES" << endl; 24 for (int i = 1; i <= k; i++) cout << a[i] << " "; 25 cout << endl; 26 } 27 } 28 return 0; 29 }