Codeforces Round #734 (Div. 3)-A. Polycarp and Coins-题解

这篇博客讲解了如何在支付时使用1元和2元硬币,使得两者数量之差最小。解题策略涉及找到恰好等于总金额的硬币组合,同时保持两者的数量差尽可能小。通过实例和代码展示了如何实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces Round #734 (Div. 3)-A. Polycarp and Coins

传送门
Time Limit: 1 second
Memory Limit: 256 megabytes

Problem Description

Polycarp must pay exactly n n n burles at the checkout. He has coins of two nominal values: 1 1 1 burle and 2 2 2 burles. Polycarp likes both kinds of coins equally. So he doesn’t want to pay with more coins of one type than with the other.

Thus, Polycarp wants to minimize the difference between the count of coins of 1 1 1 burle and 2 2 2 burles being used. Help him by determining two non-negative integer values c 1 c_1 c1 and c 2 c_2 c2 which are the number of coins of 1 1 1 burle and 2 2 2 burles, respectively, so that the total value of that number of coins is exactly n n n (i. e. c 1 + 2 ⋅ c 2 = n c_1 + 2 \cdot c_2 = n c1+2c2=n), and the absolute value of the difference between c 1 c_1 c1 and c 2 c_2 c2 is as little as possible (i. e. you must minimize ∣ c 1 − c 2 ∣ |c_1-c_2| c1c2).

Input

The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Then t t t test cases follow.

Each test case consists of one line. This line contains one integer n n n ( 1 ≤ n ≤ 1 0 9 1 \le n \le 10^9 1n109) — the number of burles to be paid by Polycarp.

Output

For each test case, output a separate line containing two integers c 1 c_1 c1 and c 2 c_2 c2 ( c 1 , c 2 ≥ 0 c_1, c_2 \ge 0 c1,c20) separated by a space where c 1 c_1 c1 is the number of coins of 1 1 1 burle and c 2 c_2 c2 is the number of coins of 2 2 2 burles. If there are multiple optimal solutions, print any one.

Sample Input

6
1000
30
1
32
1000000000
5

Sample Onput

334 333
10 10
1 0
10 11
333333334 333333333
1 2

Note

The answer for the first test case is “334 333”. The sum of the nominal values of all coins is 334 ⋅ 1 + 333 ⋅ 2 = 1000 334 \cdot 1 + 333 \cdot 2 = 1000 3341+3332=1000, whereas ∣ 334 − 333 ∣ = 1 |334 - 333| = 1 334333=1. One can’t get the better value because if ∣ c 1 − c 2 ∣ = 0 |c_1 - c_2| = 0 c1c2=0, then c 1 = c 2 c_1 = c_2 c1=c2 and c 1 ⋅ 1 + c 1 ⋅ 2 = 1000 c_1 \cdot 1 + c_1 \cdot 2 = 1000 c11+c12=1000, but then the value of c 1 c_1 c1 isn’t an integer.

The answer for the second test case is “10 10”. The sum of the nominal values is 10 ⋅ 1 + 10 ⋅ 2 = 30 10 \cdot 1 + 10 \cdot 2 = 30 101+102=30 and ∣ 10 − 10 ∣ = 0 |10 - 10| = 0 1010=0, whereas there’s no number having an absolute value less than 0 0 0.


题目大意

有两种钞票,面值分别是1元和2元。
买一个物品,想让使用两种钞票的数量的绝对值只差最小,请问两种钞票分别用几张。

解题思路

一张1元+一张2元是3元。先看金额中有多少个3,这些3都用相等数量的两种钞票构成。
多余的钱只能是0,1,2。如果是0就正好,是1就再来一张1元的,是2就再来一张2元的。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cin>>n;
        int t=n/3;
        int t2=t;
        int s=n%3;
        if(s==0);
        else if(s==1)t++;
        else if(s==2)t2++;
        printf("%d %d\n",t,t2);
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119063915

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tisfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值