题目链接: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. 【CodeForces 625C】K-special Tables

    题意 把1到n*n填在n*n的格子里.要求每一行都是递增的,使第k列的和最大. 分析 第k列前的格子1 2 .. 按要求填到满格,然后第k列及后面的格子,都从左到右填递增1的数. 第k列的和再加起来, ...

  2. Day4_计算器

    read me 1.构造三个函数,乘除(mad),加减(aas),去括号(par): 2.获取表达式字符串之后,判断是否包含“+-*/()”等字符,包含则下一步3:不包含,返回字符串: 3.par 函 ...

  3. Day1 三级目录

    d_city = { "河南" : {"郑州" : ["二七区","中原区","回族管城区",&qu ...

  4. Spring的辅助类

    http://www.cnblogs.com/maoan/p/3446224.html spring获取ApplicationContext对象的方法——ApplicationContextAware

  5. SQL增加、删除、更改表中的字段名

    1. 向表中添加新的字段 ) not null 2. 删除表中的一个字段 delete table table_name column column_name 3. 修改表中的一个字段名 alter ...

  6. HDU 1060 Left-most Digit

    传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. sql注入实例分析

    什么是SQL注入攻击?引用百度百科的解释: sql注入_百度百科: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具 ...

  8. Memcache和Redis

  9. C++标准转换运算符reinterpret_cast

    C++标准转换运算符reinterpret_cast reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关 ...

  10. QQ Auto Login Visual Basic Script

    QQ_Auto_Login.vbs: Dim QQPath QQPath="C:\Program Files (x86)\Tencent\QQ\Bin\QQ.exe" Set ba ...