CF1607G.Banquet Preparations 1
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A known chef has prepared n dishes: the i-th dish consists of ai grams of fish and bi grams of meat.
The banquet organizers estimate the balance of n dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.
Technically, the balance equals to i=1∑nai−i=1∑nbi. The smaller the balance, the better.
In order to improve the balance, a taster was invited. He will eat exactly m grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly m grams of each dish in total.
Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them.
一位知名厨师准备了 n 道菜肴:第 i 道菜肴由 ai 克鱼肉和 bi 克牛肉组成。
宴会组织者对这 n 道菜肴的“平衡度”定义如下:平衡度等于鱼肉总质量与牛肉总质量之差的绝对值。
严格来说,平衡度等于 i=1∑nai−i=1∑nbi。平衡度越小越好。
为改善平衡度,主办方邀请了一位品鉴师。他将从每道菜肴中恰好吃掉 m 克食物。对于每道菜肴,品鉴师需分别决定吃多少克鱼肉、多少克牛肉;唯一约束是:从每道菜肴中吃掉的食物总量必须恰好为 m 克。
请确定品鉴师应从每道菜肴中各吃多少克鱼肉和牛肉,使得最终的平衡度尽可能小。若存在多种可行方案,任选其一即可。
输入格式
The first line of input data contains an integer t (1≤t≤104) — the number of the test cases.
Each test case's description is preceded by a blank line. Next comes a line that contains integers n and m (1≤n≤2⋅105; 0≤m≤106). The next n lines describe dishes, the i-th of them contains a pair of integers ai and bi (0≤ai,bi≤106) — the masses of fish and meat in the i-th dish.
It is guaranteed that it is possible to eat m grams of food from each dish. In other words, m≤ai+bi for all i from 1 to n inclusive.
The sum of all n values over all test cases in the test does not exceed 2⋅105.
输入数据的第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。
每个测试用例的描述前有一空行。接下来是一行,包含两个整数 n 和 m(1≤n≤2⋅105;0≤m≤106)。随后的 n 行描述了 n 道菜肴,其中第 i 行包含一对整数 ai 和 bi(0≤ai,bi≤106),分别表示第 i 道菜肴中鱼肉和牛肉的质量(单位:克)。
题目保证可以从每道菜肴中恰好吃掉 m 克食物。换言之,对所有 i(1≤i≤n),均有 m≤ai+bi。
所有测试用例中的 n 值之和不超过 2⋅105。
输出格式
For each test case, print on the first line the minimal balance value that can be achieved by eating exactly m grams of food from each dish.
Then print n lines that describe a way to do this: the i-th line should contain two integers xi and yi (0≤xi≤ai; 0≤yi≤bi; xi+yi=m), where xi is how many grams of fish taster should eat from the i-th meal and yi is how many grams of meat.
If there are several ways to achieve a minimal balance, find any of them.
对于每个测试用例,在第一行输出通过从每道菜中恰好吃 m 克食物所能达到的最小平衡值。
然后输出 n 行,描述一种实现该最小平衡值的方法:第 i 行应包含两个整数 xi 和 yi(满足 0≤xi≤ai;0≤yi≤bi;且 xi+yi=m),其中 xi 表示食客应从第 i 道菜中吃的鱼的克数,yi 表示应吃的肉的克数。
如果存在多种方式可达到最小平衡值,则输出任意一种即可。
输入输出样例
输入#1
8 1 5 3 4 1 6 3 4 2 2 1 3 4 2 2 4 1 3 1 7 3 6 1 7 1 8 1 9 3 6 1 8 1 9 30 10 3 4 3 1 3 2 4 1 5 4 0 7 6 4 0 8 4 1 5 3
输出#1
0 2 3 1 3 3 0 1 1 1 1 2 1 3 0 4 3 0 6 0 6 0 6 7 1 5 1 5 6 0 0 3 1 3 1 3 1 0 0 4 2 2 0 4 3 1 1 3
输入解题思路,AI测评打分。不知道怎么写?