Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解相关的知识,希望对你有一定的参考价值。
目录
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+2⋅c2=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| ∣c1−c2∣).
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \\le t \\le 10^4 1≤t≤104) — 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 1≤n≤109) — 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,c2≥0) 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 334⋅1+333⋅2=1000, whereas ∣ 334 − 333 ∣ = 1 |334 - 333| = 1 ∣334−333∣=1. One can’t get the better value because if ∣ c 1 − c 2 ∣ = 0 |c_1 - c_2| = 0 ∣c1−c2∣=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 c1⋅1+c1⋅2=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 10⋅1+10⋅2=30 and ∣ 10 − 10 ∣ = 0 |10 - 10| = 0 ∣10−10∣=0, whereas there’s no number having an absolute value less than 0 0 0.
题目大意
这题和上题的差别就是这题颜色种类更多,数字种类更多(26种字母变成 1 0 9 10^9 109种数字。然后让你输出具体怎么涂色。
但是方法一样。
我们只需多几个记录就行了。
- ans[i]表示第i个涂第ans[i]种颜色
解题思路
请见题目大意
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 ans[200010]; // 用来记录结果(ans[i]表示第i个涂第ans[i]种颜色)
int main()
{
int N;
cin>>N;
while(N--)
{
int n,k;
int s=0;
scanf("%d%d",&n,&k);
map<int,vector<int> >ma; // ma<a, [loc1, loc2, ...]> : 数字a的下标有:loc1、loc2...
for(int i=0;i<n;i++)
{
int t;
cd(t); // scanf("%d",&t);
ma[t].push_back(i); // 下标为i的数字也为t
if(ma[t].size()<=k)s++; // 如果这是这个数字的前k个,就可以涂色。
}
int t=s/k; // 每种颜色可以涂t次(总可涂色数/颜色总数 向下取整)
int color=1; // 现在涂哪种颜色
int all=t*k; // 总的要涂色的次数
s=0; // 已经涂了几个
for(map<int,vector<int> >::iterator it=ma.begin();it!=ma.end();it++) // 变量map
{
int size=(it->second).size(); // 这个数一共出现了几次
int to=min(size,k); // 这个数要涂到第几个(最多不超过k个)
for(int i=0;i<to;i++) // 这是所有要涂色的
{
int thisLocation = (it->second)[i]; // 这个数的下标
if(s>=all) // 已经涂够了
{
ans[thisLocation]=0; // 就不涂了
continue;
}
ans[thisLocation]=color; // 涂上颜色color
color%=k; // 颜色循环使用
color++; // 下一种颜色
s++; // 总涂色数++
}
for(int i=to;i<size;i++) // 多出部分不能涂色
{
ans[(it->second)[i]]=0; // 不涂色记为0
}
}
for(int i=0;i<n;i++)
{
printf("%d ",ans[i]); // 输出这种颜色
}
puts(""); // 换行
}
return 0;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119064680
以上是关于Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #734 (Div. 3)-C. Interesting Story-题解
Codeforces Round #734 (Div. 3)-B1. Wonderful Coloring - 1
Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解
Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解