Wonderful Coloring - 2

题目大意:涂色游戏,求最后涂色序列;

要求:1.每个元素,可涂,可不涂

2.同种颜色的颜色数相等

3.数值相等的元素不能涂同种颜色

解题思路:先排除确定不涂的元素,再直接对剩余元素直接涂色;

确定不涂的元素:数值相等的元素个数超出所给的颜色数了的多出的那些元素确定不涂。

最后 确定要涂的元素数%颜色数k == 0,若确定要涂的元素数%颜色数k != 0 ,则找余数个未确定是否涂色的元素,将它们变为确定不涂色的元素;

最最后,顺序涂色,按下标重新排序输出。

PS:大无语事件:我就不明白之前写的错在哪了,看了一堆题解,才写出现在这个。。。。。

WA1:

#include <iostream>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
const int N =2e5+10;
struct point{int w, idx, color, cnt;
}a[N];
int c[N];
vector<int> st;
bool cmp(point a, point b)
{if(a.cnt != b.cnt) return a.cnt > b.cnt;if(a.w != b.w) return a.w<b.w;return a.idx<b.idx;
}
bool cmp2(point a, point b)
{return a.idx < b.idx;
}
int main()
{int t;cin>>t;while(t --){int n, k, pos = 0;cin>>n>>k;for(int i = 1; i<= n; i++){cin>>a[i].w;a[i].idx = i;st.push_back(a[i].w);}for(int i = 1; i<=n; i++){int num = count(st.begin(),st.end(),a[i].w);a[i].cnt = num;// cout<<a[i].cnt<<endl;}sort(a+1, a+n+1, cmp);int color = 1;for(int i = 1; i<= n; i++){// cout<<"value : "<<a[i].w<<"   color: ";if(i > k){if(a[i-k].color == color && a[i-k].w == a[i].w) a[i].color = 0;else if(a[i-1].color == 0 && a[i-1].w == a[i].w) a[i].color = 0;else a[i].color = color++;}else{a[i].color = color;color++;}if(color == k+1) color = 1;c[a[i].color]++;if(c[a[i].color] > n/k && a[i].color != 0) a[i].color = 0;// cout<<a[i].color<<"      "<<endl;}//cout<<endl;sort(a+1, a+n+1, cmp2);for(int i = 1; i<=n; i++){cout<<a[i].color<<' ';}cout<<endl;memset(a, 0, sizeof(a));memset(c, 0, sizeof(c));st.clear();}return 0;
}

WA2:

#include <iostream>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
const int N =2e5+10;
struct point{int w, idx, color, cnt;
}a[N];
int c[N];
bool cmp(point a, point b)
{if(a.w!=b.w) return a.w<b.w;return a.idx < b.idx;
}
bool cmp2(point a, point b)
{return a.idx < b.idx;
}
int main()
{int t;cin>>t;while(t --){int n, k, pos = 0;cin>>n>>k;for(int i = 1; i<= n; i++){cin>>a[i].w;a[i].idx = i;}sort(a+1, a+n+1, cmp);int color = 1;int cnt = 1;a[1].color = 1;for(int i = 2; i<= n; i++){if(a[i].w == a[i-1].w){cnt++;if(cnt>k){a[i].color = 0;continue;}}else{cnt = 1;}color++;if(color > k) color = 1;a[i].color = color;}int i = n;if(a[i].color != k){a[i].color = 0;i--;}sort(a+1, a+n+1, cmp2);for(int i = 1; i<=n; i++){cout<<a[i].color<<' ';}cout<<endl;memset(a, 0, sizeof(a));}return 0;
}

AC:(这是看了别人的题解改的)

#include <iostream>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
const int N =2e5+10;
struct point{int w, idx, color;
}a[N];
int c[N];
bool cmp(point a, point b)
{return a.w<b.w;
}
bool cmp2(point a, point b)
{return a.idx < b.idx;
}
int main()
{int t;cin>>t;while(t --){int n, k;cin>>n>>k;for(int i = 1; i<= n; i++){cin>>a[i].w;a[i].idx = i;a[i].color = -1;if(c[a[i].w] == k) a[i].color = 0;else c[a[i].w]++;//cout<<c[a[i].w]<<endl;}sort(a+1, a+n+1, cmp);int cnt = 0;for(int i = 1; i<= n; i++){if(a[i].color == -1) cnt++;}//cout<<cnt<<endl;int i = n;while(cnt % k){if(a[i].color == -1){a[i].color = 0;cnt--;}i--;}int color = 1;for(int i = 1; i<= n; i++){if(a[i].color == -1) a[i].color = color++;if(color == k+1) color = 1;}sort(a+1, a+n+1, cmp2);for(int i = 1; i<=n; i++){cout<<a[i].color<<' ';}cout<<endl;memset(a, 0, sizeof(a));memset(c, 0, sizeof(c));}return 0;
}

CF寒假补题集——1.21相关推荐

  1. 2022牛客寒假补题记录 2

    题目链接 A.小沙的炉石 数学结论/二分 感觉考虑区间合并会更好做一些,不过不好想 考虑在有限次的攻击中能造成伤害的最大和最小值 攻击最大次数 m a x n = m i n ( n , m + 1 ...

  2. 2020 7.12 -- 7.13 两场牛客多校 + 两场 unrated的cf的补题

    自闭ing之后的多校就不打了-太难了吧- 7.12多校 2020牛客暑期多校训练营(第一场) F Infinite String Comparision 少数几个会写的题 (唯二 开始想的是要lcm( ...

  3. 2019暑期个人排位集训补题--思维题

    2019暑期集训思维相关补题集 CodeForces - 768B Code For 1 Gym - 100066B CodeForces - 768B Code For 1 Jon fought b ...

  4. 牛客寒假算法基础集训营6补题和题解

    这是寒假写的题解,不知道为什么没发出去... 总结: 牛客做多了可以学很多奇葩的开数组方法和很多奇妙的方法!感觉题目出的不错2333 A.出题 链接:https://ac.nowcoder.com/a ...

  5. 2022.1.18(一测补题,树的遍历题目,并查集)

    由于深夜点了外卖要1:50才到 闲着没事就补一下题目! 题目如下: 题目描述 In one one-dimensional world there are nn platforms. Platform ...

  6. [nk] 2022牛客寒假算法基础集训营1 补题|题解

    目录 前言 L.牛牛学走路 MyCode OtherCode J.小朋友做游戏 MyCode A.九小时九个人九扇门 MyCode F. 中位数切分 MyCode 前言 根据难度系数补题,大概会补 A ...

  7. 2019寒假集训第五场(新生场)中石油补题和题解

    这场比赛本来我都出去了,然后看到老师发消息又回来了= = 然后做着做着正好mhr大佬在外面有一些事情突然没法比赛,于是就帮他交了两道题看看对不对,便于之后补题,然后还真都对了,下面是题目和解析: 问题 ...

  8. PTA寒假基础题训练(含解题思路)(下)

    目录 7-36 字符串的冒泡排序 AC代码: 7-37 龟兔赛跑 AC代码: 7-38 N个数求和 AC代码: 7-39 数列求和-加强版 AC代码: 7-40 抓老鼠啊~亏了还是赚了? AC代码: ...

  9. PTA寒假基础题训练(含解题思路)(中)

    目录 7-19 两个有序单链表求差集 AC代码: 7-20 统计数字字符和空格 AC代码: 7-21 打印沙漏 AC代码: 7-22 Wifi密码 AC代码: 7-23 福到了 AC代码: 7-24 ...

最新文章

  1. 用于EAM的SAP PM移动应用程序
  2. 18、计算机图形学——BRDF与渲染方程
  3. OpenLDAP实战指南上市发售
  4. java多态替换switch_使多态性无法解决那些switch / case语句的麻烦
  5. [javascript] Date 时间精确到天
  6. 【渝粤教育】广东开放大学 网页设计与制作 形成性考核 (25)
  7. Mysql存储引擎中InnoDB与Myisam的区别
  8. ios键盘done中文_IOS_IOS关闭键盘的方法,首先输入完成后按键盘上的done - phpStudy...
  9. 开发 OpenAM Java 客户端
  10. loss函数之L1Loss,MSELoss,SmoothL1Loss, HuberLoss
  11. oracle中dual最多存多大_ORACLE中dual用法详解
  12. asc怎么用 linux zip_File Roller/Unzip 解压中文 Zip 文件名乱码
  13. lg g7 刷机 救砖 root
  14. CN 国家顶级域名(摘自网络)
  15. 华为手机进入汽车自动打开热点
  16. python 红黑树_手把手教你43行代码写红黑树(包括删除操作)
  17. 做教育怎么引流?教育行业怎么引流?培训机构引流如何转化?
  18. 搞了一天存储过程,现在又说历史数据不用处理了。。。
  19. 单商户商城系统功能拆解39—分销应用—分销等级
  20. oracle索引的事

热门文章

  1. 电脑html文件删除,电脑plugin可以删除吗
  2. DeepLesion: Automated Deep Mining, Categorization and Detection of Significant RadiologyImageFinding
  3. 揭秘今年Android春招面试必问问题有哪些?成功拿下大厂offer
  4. Linux-系统管理10-服务器RAID及配置实战
  5. 未越狱iPhone访问限制密码忘了怎么办
  6. 黑马程序员C++学习笔记
  7. 树莓派 与Pi仪表盘安装
  8. Hadoop基础教程-第12章 Hive:进阶(12.3 HiveServer2)(草稿)
  9. 软件测试之 购物车测试用例
  10. Dicom 图像--像素值(灰度值)转为CT值