题目链接: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 解题报告的更多相关文章

  1. codeforces C1. The Great Julya Calendar 解题报告

    题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...

  2. codeforces B. Eugeny and Play List 解题报告

    题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...

  3. codeforces 433C. Ryouko's Memory Note 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...

  4. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  5. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

  6. codeforces 505A. Mr. Kitayuta's Gift 解题报告

    题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...

  7. codeforces 499A.Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...

  8. codeforces 374A Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行  m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...

  9. codeforces B. Making Sequences is Fun 解题报告

    题目链接:http://codeforces.com/problemset/problem/373/B 题目意思:给出w,m和k,需要找出从m开始,可以有多少个连续的数(m+1,m+2,...)(在添 ...

随机推荐

  1. SourceTree&Git部分名词解释

    SourceTree&Git部分名词解释 克隆(clone):从远程仓库URL加载创建一个与远程仓库一样的本地仓库 提交(commit):将暂存文件上传到本地仓库(我们在Finder中对本地仓 ...

  2. 34.Android之资源文件res里drawable学习

    我们经常看到android工程资源文件res下drawable如ldpi.mdpi.hdpi.xhdpi.xxhdpi文件,今天我们学习了解下. (1)drawable-hdpi里面存放高分辨率的图片 ...

  3. 求第N数大问题

    问题: InputThe first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of ...

  4. ISO 基础之 (十二) 文件管理

    一 文件管理 沙盒:让每个APP应用在手机上有一个独立的文件夹,相互之间不能访问. 沙盒目录:NSHomeDirectory() library: 库文件 tmp: 临时文件 1.NSData 也是一 ...

  5. myBatis 实现用户表增操作(复杂型)

    增加 @Test public void addTest(){ String resource = "mybatis-config.xml"; SqlSession sqlSess ...

  6. C++ 四种强制类型转换

    来自csdn:http://blog.csdn.net/hgl868/article/details/46619399 C风格的强制类转换(Type Cast)很简单,不管什么类型的转换统统是: TY ...

  7. PhpStorm 设置php代码格式

    phpstorm 代码格式化方法: 快捷键:Ctrl + Alt + L 设置代码样式:File -> Settings -> Code Style ->PHP 根据个人php代码规 ...

  8. SpringMVC利用拦截器防止SQL注入

    引言 随着互联网的发展,人们在享受互联网带来的便捷的服务的时候,也面临着个人的隐私泄漏的问题.小到一个拥有用户系统的小型论坛,大到各个大型的银行机构,互联网安全问题都显得格外重要.而这些网站的背后,则 ...

  9. android webview删除缓存

    [1].[代码] 删除保存于手机上的缓存. 跳至 [1] [2] [3] 01 // clear the cache before time numDays     02 private int cl ...

  10. Centos优化Hadoop

    导读 Hadoop是一个能够让用户轻松架构和使用的分布式计算平台,用户可以轻松地在Hadoop上开发和运行处理海量数据的应用程序,本节讲安装并且优化centos 6.7 系统下的Supper Hado ...