2022.7.28 LeetCode AcWing

LeetCode

STL 模拟
https://leetcode.cn/problems/rank-transform-of-an-array/solution/1331-stl-by-sen-xm-w57d/

AcWing

贪心 如果有解,从大到小依次判断是否能加入部分和中使得部分和不会超过目标和,能加就加。

证明一下子懵了,感觉就是应该这么写。看到 AcWing大佬的证明 猛地又想起来了。最基本的归纳、反证、类比法这些证明方法应该想起来的。大概是前期不注重数学证明的,得过且过的后果罢。

#include <bits/stdc++.h>
using namespace std;

int T;
int n, x, y;

int main() {
	cin >> T;
	for (int ct = 1; ct <= T; ct++) {
		vector<int> ans;
		cin >> n >> x >> y;
		if ((n * (n + 1) / 2) % (x + y) == 0) {
			int t = (n * (n + 1) / 2) / (x + y) * x;
			for (int i = n; i >= 1; i--) {
				if (t == 0)
					break;
				if (t >= i) {
					ans.push_back(i);
					t -= i;
				} else
					continue;
			}
			if (t != 0) {
				cout << "Case #" << ct << ": IMPOSSIBLE" << endl;
			} else {
				sort(ans.begin(), ans.end());
				cout << "Case #" << ct << ": POSSIBLE" << endl;
				cout << ans.size() << endl;
				for (auto c : ans)
					cout << c << " ";
				cout << endl;
			}
		} else {
			cout << "Case #" << ct << ": IMPOSSIBLE" << endl;
		}
	}

	return 0;
}
posted @ 2022-07-28 10:06  superPG  阅读(18)  评论(0编辑  收藏  举报