我的具体思路是他的答案一定在他的不同排序序列中我们只要对他的所有排序序列进行判断就可以分出数组了(但是有一组数据一直过不去,后来看到有几个人说最后一组数据过不去我的也是这样暂时先发出来我的题解,不一定对大家看看就好)
next_permutation()//从小打大排列要对想要排列的数组先进行排序
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cmath> using namespace std; const int N = 1e5 + 5; int main() { int n, r; cin >> n >> r; vector<int> A; vector<int> C; vector<int> B; int x; for (int i = 0; i < n; i++) { cin >> x; A.push_back(x); } int y; y = A[0]; sort(A.begin(), A.end()); int sum1 = 0, sum2 = 0; do { sum1 = 0; sum2 = 0; for (int i = 0; i < n; i++) { if (A[i] == y) { B.push_back(A[i]); sum1 += A[i]; if (fabs(sum1 - sum2) > 10) { break; } } else if (sum1 + A[i] - sum2 > r) { C.push_back(A[i]); sum2 += A[i]; } else if (sum1 + A[i] - sum2 <= r) { B.push_back(A[i]); sum1 += A[i]; } } if (fabs(sum1 - sum2) <= r && B.size() + C.size() == n) { break; } else { B.clear(); C.clear(); } } while (next_permutation(A.begin(), A.end())); sort(B.begin(), B.end()); sort(C.begin(), C.end()); for (int i = 0; i < B.size(); i++) { cout << B[i] << " "; } cout << endl; for (int i = 0; i < C.size(); i++) { cout << C[i] << " "; } }
10-12
578
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-15
998
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-18
12-13
02-26
1423
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-15
1535
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-06
220
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)