codeforces 471B. MUH and Important Things 解题报告
题目链接:http://codeforces.com/problemset/problem/471/B
题目意思:有 n 个 tasks,编号依次为 1 ~ n,每个 task 都有一定的难度值来评估。例如,第 i 个 task 的难度值为 hi。现在的任务是把 n 个 task 全部完成,难度值越小的task越先完成。有3个人,都希望自己完成所有task的输出序列是与众不同的,问是否存在至少3条完成所有task的不同序列,没有的话输出 “NO”,否则输出“YES”并输出3条不同的完成序列。
昨晚比赛的题目,有一点点思路,但最终调不出来,泪~~。
小小的思路: 首先要知道有解的情况是什么。当时想到的是:两种情况。(1)相同难度值(difficulty)的task 至少有3个,类似这种序列:1 2 3 3 3 5 (2)相同 difficulty 的 task 有2个,但这种类型的task 至少有2个,类似这种序列: 1 2 2 4 4 5
不过处理起来确实复杂无比,甚至用到 vector 来存储每个难度值的邻接表,总的来说就是想得太复杂啦!!
看了下别人的代码,发现思路其实是没有错的,还算是有一点点欣慰 ^_^
首先判断是否能找到3条不同序列。用一个 used 数组来统计difficulty相同的task有多少个。处理的时候同常规办法有一点点不同,是先 used[h[i]] 再 count,但 used[h[i]]++在这条语句之后,这样处理的巧妙之处就是把我上面的两种情况都包括上去了,不用另外讨论。
另外,可以用pair<int, int> 来解决存储问题,first值保存task的difficulty,second值保存task的id。排序是很容易想到的,因为要按题目要求,难度值越少的task越先做嘛~~
最后一个问题就是,如何得到3条不同的序列,用 swap 可以解决。由于排序之后,对于相同difficulty的task,它们的 id 默认是从小到大排序的,那么swap 的条件是,对于相同difficulty 的task,交换那些 后面 id > 前面 id 的task。可以知道,每交换一次,就得到一条新的完成序列。
版本1(个人比较喜欢这个多点)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; #define f first
#define s second
typedef pair<int, int> pii; const int maxn = + ;
pii task[maxn];
int used[maxn]; int main()
{
int n;
// freopen("input.txt", "r", stdin);
while (scanf("%d", &n) != EOF)
{
int in, count = ;
memset(used, , sizeof(used));
for (int i = ; i <= n; i++)
{
scanf("%d", &in);
task[i] = pii(in, i); // 巧妙的赋值
if (used[in])
count++;
used[in]++;
}
if (count < )
printf("NO\n");
else
{
printf("YES\n");
sort(task+, task++n); // 默认按pair first 排序 for (int time = ; time < ; time++)
{
for (int i = ; i <= n; i++)
printf("%d ", task[i].s);
printf("\n"); for (int i = ; i < n; i++)
{
if (task[i].f == task[i+].f && task[i].s < task[i+].s)
{
swap(task[i], task[i+]);
break;
}
}
}
}
}
return ;
}
版本2(比赛时死也调不出来,看了别人的在原来基础上改的)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
struct node{
int id;
int h;
}task[maxn]; int cmp(node a, node b)
{
return a.h < b.h;
} int cnt[maxn]; int main()
{
int n;
// freopen("input.txt", "r", stdin);
while (scanf("%d", &n) != EOF)
{
int t, c0 = ;
bool flag = false;
memset(cnt, , sizeof(cnt));
for (int i = ; i <= n; i++)
{
scanf("%d", &t);
task[i].h = t;
task[i].id = i; if (cnt[t]) // 同一个h值出现3次以上/两个以上的h值出现2次以上
c0++;
cnt[t]++;
}
if (c0 < )
printf("NO\n");
else
{
printf("YES\n");
sort(task+, task++n, cmp);
for (int time = ; time < ; time++)
{
for (int i = ; i <= n; i++)
printf("%d ", task[i].id);
printf("\n"); for (int i = ; i < n; i++)
{
if (task[i].h == task[i+].h && task[i].id < task[i+].id)
{
swap(task[i], task[i+]);
break;
}
}
}
}
}
return ;
}
codeforces 471B. MUH and Important Things 解题报告的更多相关文章
- codeforces C1. The Great Julya Calendar 解题报告
题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...
- codeforces B. Eugeny and Play List 解题报告
题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- codeforces 505A. Mr. Kitayuta's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...
- codeforces 499A.Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...
- codeforces 374A Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行 m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...
- codeforces B. Making Sequences is Fun 解题报告
题目链接:http://codeforces.com/problemset/problem/373/B 题目意思:给出w,m和k,需要找出从m开始,可以有多少个连续的数(m+1,m+2,...)(在添 ...
随机推荐
- python 学习5--matplotlib画图实践
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 学习参考: http://www.cnblogs.com/vamei/archive/2013/01/30/28 ...
- BZOJ-2756 奇怪的游戏 黑白染色+最大流+当前弧优化+二分判断+分类讨论
这个题的数据,太卡了,TLE了两晚上,各种调试优化,各种蛋疼. 2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MB Submit ...
- linux 的jdk安装
1.1 解压上传的安装包 1.2 创建安装目录 1.3 将解压后的目录移动到安装目录 1.4 配置环境变量 修改www.qixoo.qixoo.com/etc/profile文件 ...
- MVC区域 视图必须派生自 WebViewPage 或 WebViewPage<TModel>
http://blog.csdn.net/iack_ji/article/details/16965885 今天在学习 mvc区域时,将区域控制器类 外迁到其他的程序集的练习中出现了"视图必 ...
- Extjs Form用法详解(适用于Extjs5)
Extjs Form是一个比较常用的控件,主要用来显示和编辑数据的,今天这篇文章将介绍Extjs Form控件的详细用法,包括创建Form.添加子项.加载和更新数据.验证等. 本文的示例代码适用于Ex ...
- ios严格检验身份证号码有效性
+ (BOOL)checkIDCard:(NSString *)sPaperId { //判断位数 && sPaperId.length != ) { return NO; } NSS ...
- zabbix 汉化
zabbix2.x的版本自带汉化,3.x的版本也可以通过修改配置文件强制使用自带的汉化,但是不管哪种,翻译的精准度令人费解:偶然发现一个专门翻译zabbix的网站https://www.zabbix. ...
- PHP FORUM
1.index.php <html> <!--功能:php论坛标题部分--> <head><meta http-equiv="content-typ ...
- spring mvc文件上传和下载
首先要导入2个包(上传文件包和io的包)
- C++中 :: 的意思
表示作用域,和所属关系 ::是运算符中等级最高的,它分为三种:1)global scope(全局作用域符),用法(::name)2)class scope(类作用域符),用法(class::name) ...