题目链接

题意

有 \(n\) 个容积无限的水缸,初始时水量为\(a_1,a_2,...,a_n\),有一把容积为\(k\)的勺子,可以从一个水缸中舀水倒入另一个水缸中。问能否给出操作序列,使得最终某一个水缸中水的容量为\(V\).

思路

参考 - 粉兔.

结论

首先,如果\(\sum_{i=1}^{n}a_i\lt V\),显然不可行。

否则,一旦\(\exists p_1,p_2,...,p_t,s.t.(\sum_{i=1}^{t}a_{p_i})\%k==V\%k\),则我们说,这件事是可行的。

为什么呢?因为一旦可以取到同余的值,那么无论总量是多余还是不足都可以用勺子解决:多了,就往外舀;少了,就从其他缸(\(i.e.\) 第\(j\)个\((j!=p_i(i=1,2,...,t)\)))中补进来。

至于操作的具体细节,暂放一下。

dp

那么该怎么判断是否有上面的条件成立呢?

用 \(dp[i][j]\) 表示前 \(i\) 个缸能否取到模数为 \(j\) 的值,特别的,

\[dp[i][j]=\begin{cases}0,&cannot\ make\ it\cr1,&can\ make\ it\ without\ a_i\cr2,&a_i\ needed\ to\ make\ it\cr\end{cases}
\]

则 \(dp[n][V\%k]\) 即表示前 \(n\) 个缸能否取到模数为 \(k\) 的值。

构造

现在有了这个结论和中间记录的 \(dp\) 值,该怎么推出步骤呢?

注意到,上面的记录过程提供给了我们 \(p_1,p_2,...,p_t\),即必须全部取的缸;而其余的缸,不妨记为 \(q_1,q_2,...,q_s\)。

则可行操作如下:

  1. 将 \(p1,...,p_{t-1}\) 缸中的水全部舀入 \(p_t\) 中;
  2. 将 \(q1,...,q_{s-1}\) 缸中的水全部舀入 \(q_s\) 中;
  3. 在 \(p_t\) 与 \(q_s\) 之间进行多退少补。

注意几种 特殊情况,比如 \(t=n\) 和 \(s=n\) 的情况:

\(t=n\) 即所有的缸都需要,最后总量肯定只会超出,不会不够,并且超出的部分必然是 \(k\) 的整倍数。

此时,将所有缸中的水都舀到某一个缸中,再将超出的部分舀到另一个缸中;

(这种情况可以与一般情况合在一起统一处理)。

\(s=n\) 即所有的缸都不需要,这是什么回事呢?当 \(k|V\) 时就会发生这种情况,即所需要的容积恰好可以用若干勺舀出。

此时,将所有缸中的水都舀到某一个缸中,再将需要的部分舀到另一个缸中。

// 很佩服粉兔啦%%%

Code

#include <bits/stdc++.h>
#define maxn 5010
using namespace std;
typedef long long LL;
int a[maxn], b[maxn], dp[maxn][maxn];
bool flag[maxn];
int main() {
int n, k, v, sum = 0;
scanf("%d%d%d", &n, &k, &v);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
sum += (b[i] = a[i]), a[i] %= k;
} if (sum < v) { puts("NO"); return 0; }
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < k; ++j) {
if (dp[i-1][j]) {
dp[i][j] = 1;
if (!dp[i][(j+a[i])%k]) dp[i][(j+a[i])%k] = 2;
}
}
}
int tar = v % k;
if (!dp[n][tar]) { puts("NO"); return 0; }
puts("YES");
int ans = 0, fnl1 = -1, fnl2 = -1;
for (int i = n; i >= 1; --i) {
if (dp[i][tar]==2) {
flag[i] = true, (tar += k-a[i]) %= k, ans += b[i];
if (fnl1==-1) fnl1 = i;
}
else if (fnl2 == -1) fnl2 = i;
}
if (fnl1==-1) {
for (int i = 1; i < n; ++i) if (b[i]) {
printf("%d %d %d\n", (b[i] + k-1)/k, i, n);
}
if (v/k) printf("%d %d %d\n", v/k, n, 1);
return 0;
}
assert((v-ans) % k == 0);
int rem = v - ans;
int S1 = b[fnl1], S2 = b[fnl2];
for (int i = 1; i <= n; ++i) {
if (i == fnl1 || i == fnl2 || !b[i]) continue;
if (!flag[i]) printf("%d %d %d\n", (b[i]+k-1)/k, i, fnl2), S2 += b[i];
else printf("%d %d %d\n", (b[i]+k-1)/k, i, fnl1), S1 += b[i];
}
assert((v-S1) % k == 0);
int cnt = (v-S1) / k;
if (cnt>0) printf("%d %d %d\n", cnt, fnl2, fnl1);
else if (cnt<0) printf("%d %d %d\n", -cnt, fnl1, 1);
return 0;
}

Codeforces 920D Tanks的更多相关文章

  1. Codeforces Educational Round 37

    Solved   CodeForces 920A Water The Garden   Solved   CodeForces 920B Tea Queue   Solved   CodeForces ...

  2. Codeforces Round #115 B. Plane of Tanks: Pro 水题

    B. Plane of Tanks: Pro Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/17 ...

  3. Codeforces 877 C. Slava and tanks

    http://codeforces.com/problemset/problem/877/C   C. Slava and tanks time limit per test 2 seconds me ...

  4. codeforces 414D Mashmokh and Water Tanks

    codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...

  5. 【codeforces 175D】 Plane of Tanks: Duel

    http://codeforces.com/problemset/problem/175/D (题目链接) 题意 A,B两人玩坦克大战,坦克有生命值,射击间隔,伤害范围,未命中的概率.问A赢的概率是多 ...

  6. Codeforces 877C Slava and tanks(思维)

    题目链接:http://codeforces.com/problemset 题目大意:有n个格子,某些格子里可能有一个或多个坦克,但不知道具体位置,每个坦克被轰炸一次就会移动到相邻的格子里(第1个格子 ...

  7. 【Codeforces Round #442 (Div. 2) C】Slava and tanks

    [链接] 我是链接,点我呀:) [题意] 有n个位置,每个位置都可能有不定数量的tank; 你每次可以选择一个位置投掷炸弹. 并且,这个位置上的所有tank都会受到你的攻击. 并且失去一点体力. 然后 ...

  8. Codeforces Round #339 (Div. 2) B. Gena's Code 水题

    B. Gena's Code 题目连接: http://www.codeforces.com/contest/614/problem/B Description It's the year 4527 ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. linux下/dev/null被误删

    /dev/null文件是一个特殊的设备文件,可以用于清空一些日志文件,或者是使一些信息输出到此文件,用以节省硬盘空间.如果该空文件/dev/null文件被误删除掉, 如何再使用系统命令重新创建并设置该 ...

  2. 无屏幕和键盘配置树莓派WiFi和SSH

    原文转载:http://shumeipai.nxez.com/2017/09/13/raspberry-pi-network-configuration-before-boot.html 不算是什么新 ...

  3. WebUploader压缩图片上传

    WebUploader,由Baidu FEX 团队开发,以H5为主,FLASH为辅,兼容 IE6+,iOS 6+, android 4+,采用大文件分片并发上传,极大的提高了文件上传效率,看了官方文档 ...

  4. 微信小程序中 this.setData is not a function报错

    在微信小程序中我们一般通过以下方式来修改data中的数据: 比如获取小程序缓存: wx.getStorage({ key: 'is_screen', success: function (res) { ...

  5. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  6. cf976f Minimal k-covering

    枚举 \(k\),对于每个点 \(i\) 我们最多删 \(deg_i-k\) 条边,就源点向第一部.第二部向汇点连边,容量是 \(deg_i-k\),原边连上,容量是 \(1\),这样每流过一条原边在 ...

  7. win8 远程桌面时提示凭证不工作问题的终极解决办法

    环境说明 远程办公电脑(放置于公司.自用办公电脑.win8系统) 远程连接客户机(放置于家中.家庭日常所用.win8系统) 故障现象 最近在使用远程桌面连接公司的办公电脑时,突然发现win8系统总是无 ...

  8. 手机APP设计网

     http://hao.xueui.cn/ http://www.25xt.com/ 

  9. [转]手写数字识别错误NameError: name 'mnist' is not defined

    转自:https://blog.csdn.net/coder_Gray/article/details/78562382 在Tensorflow上进行mnist数字识别实例时,出现如下错误 NameE ...

  10. Spring整合hibernate -SessionFactory

    本文目录 1  本文采用 hibernate4 整合 Spring3.1 2 把Spring获取datasource通过class="org.springframework.orm.hibe ...