Codeforces 920D Tanks (看题解)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 920D Tanks (看题解)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Tanks
最關鍵的一點就是怎么判方案是否存在。。 只要存在若干個坦克之和的sum % k == v % k 就有解, 否則無解。
我怎么想不到呢。。。?
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 5000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}int n, k, v, sum, now1, now2, a[N]; bool dp[N][N]; bool f[N][N]; vector<int> vc[2];int main() {scanf("%d%d%d", &n, &k, &v);for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);sum += a[i];}if(sum < v) return puts("NO"), 0;dp[0][0] = true;for(int i = 0; i < n; i++) {for(int j = 0; j < k; j++) {if(!dp[i][j]) continue;dp[i + 1][(j + a[i + 1]) % k] = true;f[i + 1][(j + a[i + 1]) % k] = true;dp[i + 1][j] = true;f[i + 1][j] = false;}}if(!dp[n][v % k]) return puts("NO"), 0;int tmp = v % k;for(int i = n; i >= 1; i--) {if(f[i][tmp]) {vc[0].push_back(i);tmp = ((tmp - a[i]) % k + k) % k;} else {vc[1].push_back(i);}}sort(ALL(vc[0]));sort(ALL(vc[1]));for(auto& t : vc[0]) now1 += a[t];now2 = sum - now1;puts("YES");for(int i = 1; i < SZ(vc[0]); i++) printf("100000 %d %d\n", vc[0][i], vc[0][0]);for(int i = 1; i < SZ(vc[1]); i++) printf("100000 %d %d\n", vc[1][i], vc[1][0]);if(!SZ(vc[1])) vc[1].push_back(vc[0].back());if(!SZ(vc[0])) vc[0].push_back(vc[1].back());if(now1 < v) printf("%d %d %d\n", (v - now1) / k, vc[1][0], vc[0][0]);if(now1 > v) printf("%d %d %d\n", (now1 - v) / k, vc[0][0], vc[1][0]);return 0; }/* */?
轉載于:https://www.cnblogs.com/CJLHY/p/10821693.html
總結
以上是生活随笔為你收集整理的Codeforces 920D Tanks (看题解)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 213. 打家劫舍 I
- 下一篇: 判断一个python字符串中是否包含中文