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,...)(在添 ...
随机推荐
- Hive 正则匹配函数 regexp_extract
regexp_extract 语法: regexp_extract(string subject, string pattern, int index) 返回值: string 说明: 将 ...
- 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 323 Solved: 218[Submit][Status][Discuss ...
- php网站验证码的生成
<?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/p ...
- PHP输出表格的方法
<?php function table($n) //几行几列表 { echo'<table border="1" width="500" heig ...
- 周期串(Periodic Strings,UVa455)
解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC A ...
- Hadoop的datanode无法启动
Hadoop的datanode无法启动 hdfs-site中配置的dfs.data.dir为/usr/local/hadoop/hdfs/data 用bin/hadoop start-all.sh启动 ...
- ARP欺骗病毒,网页“篡改”,注入iframe代码!
---------------权威资料看这里--------------- 清华大学信息网络工程研究中心-中国教育和科研计算机网应急响应组<ARP 欺骗网页劫持攻击分析>PDF文件,直接I ...
- Visual Studio Online Integrations-Sync and migration
原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs
- linux 的useradd 命令的p选项
linux 的useradd 命令的p选项 错误用法: #useradd gaojian -p gaojian # ...
- C++ 的语言杂谈(一)--C++不是新手友好的
C++的语言品味是独特的,喜欢的人特别喜欢,讨厌的人特别讨厌.虽然Bjane Stroustrup不断地宣称C++的发展方向是新手友好的,但实际上对新手来说,最重要的还是有强大方便的标准库可以使用(像 ...