题目链接:http://codeforces.com/problemset/problem/384/B

题目意思:给出n个数组,每个数组包括m个数字,当k = 0 时,需要把n个数组都按照从小到大的顺序排列,k = 1则把n个数组里面的数字按照从大到小的顺序排列。

  直接模拟即可,不过有个地方注意下是可以减少工作量的,当处理第 i 行的时候,不再需要移动前 i - 1 行的数组下标。因为前 i - 1行的数组都排好序了。

Time Memory
46 ms 500 KB
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; const int maxn = + ;
const int maxm = + ; int a[maxn][maxm];
int ans[][]; int main()
{
int i, j, l, n, m, k, tj, cnt;
while (scanf("%d%d%d", &n, &m, &k) != EOF)
{
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
scanf("%d", &a[i][j]);
}
cnt = ;
for (i = ; i <= n; i++) // n 行
{
for (j = ; j < m; j++) // m 列
{
tj = j;
int tmp = a[i][j];
for (l = j+; l <= m; l++)
{
if (tmp > a[i][l] && k == || tmp < a[i][l] && k == )
{
tmp = a[i][l]; // 找出第 i 行中第 j 个最少的数
tj = l;
}
}
if (j != tj)
{
swap(a[i][tj], a[i][j]); // 找完之后要交换
if (k == )
{
ans[cnt][] = j;
ans[cnt][] = tj;
}
else
{
ans[cnt][] = tj;
ans[cnt][] = j;
}
cnt++;
for (l = i+; l <= n; l++) // 处理第i+1 ~ n 行的数组
{
if (a[l][j] > a[l][tj] && k == || a[l][j] < a[l][tj] && k == )
swap(a[l][j], a[l][tj]);
}
}
}
}
printf("%d\n", cnt);
for (i = ; i < cnt; i++)
printf("%d %d\n", ans[i][], ans[i][]);
}
return ;
}

解法二:堪称暴力中的暴力!!内存都省了

Time Memory
78 ms 0 KB

k = 0:从小到大排列。意味着所有数组中的第一个数是最小的!这个最小的数如何找?无非就在该行中的某一个数里面。由于不确定在哪里,但用两重循环势必能找出,于是就有了以下简单的方法:对于i = 1,a[i] 可能不是最小的数,于是不断地跟a[i+1], a[i+2], ..., a[m] 比较,即找出排在第一个的数输出 1, 2;  1, 3;  ...; 1, m 即可,这样能能保证每个数组都能找出最小的数。第二个最小的数就是2, 3;  2,  4; ...;  2, m了,后面的依次类推。但主要题目中说的,当第 i  个位置的 value  > 第 j 个位置的 value 才能交换这个条件。k = 1 则是2, 1; 3, 1; ...; m 1 输出 。

 #include <iostream>
using namespace std; int main()
{
int n, m, k, i, j;
while (cin >> n >> m >> k)
{
for (i = ; i < n * m; i++)
cin >> j;
cout << m * (m - ) / << endl;
for (i = ; i < m; i++)
{
for (j = i+; j <= m; j++)
{
if (!k)
cout << i << " " << j << endl;
else
cout << j << " " << i << endl;
}
}
}
return ;
}

codeforces B. Multitasking 解题报告的更多相关文章

  1. codeforces 31C Schedule 解题报告

    题目链接:http://codeforces.com/problemset/problem/31/C 题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间.问通过删除 ...

  2. codeforces 499B.Lecture 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...

  3. codeforces 495C. Treasure 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...

  4. codeforces 490B.Queue 解题报告

    题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...

  5. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...

  6. codeforces 489A.SwapSort 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...

  7. codeforces 485A.Factory 解题报告

    题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...

  8. codeforces 483A. Counterexample 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...

  9. codeforces 479C Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...

随机推荐

  1. ARM 浮点运算

    转载: http://www.embedu.org/Column/Column821.htm http://blog.sina.com.cn/s/blog_602f87700100r5xe.html ...

  2. android特效集合

    https://github.com/Trinea/android-open-project http://www.cnblogs.com/hawkon/p/3593709.html http://i ...

  3. Build FTP Server on Windows

    1. Use the self-ftp component service with windows control panel / program / start or close windows ...

  4. jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径

    遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...

  5. vue 避免渲染时闪烁

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  6. 零基础学python-3.7 还有一个程序 python读写文本

    今天我们引入另外一个程序,文件的读写 我们先把简单的程序代码贴上.然后通过我们多次的改进.希望最后可以变成一个简单的文本编辑器 以下是我们最简单的代码: 'crudfile--读写文件' def re ...

  7. ffmpeg H264 编解码配置

    ffmpeg H264编解码前面有文章介绍下,本文主要介绍一些参数配置. 编码: int InitEncoderCodec( int iWidth, int iHeight) { AVCodec * ...

  8. Linux面试必问-对照目录内容的命令“Diff”具体解释

    dir1下有个log_1.log dir2下有个log_2.log 两个文件例如以下: p_ylwu@VM_194_111_sles10_64:/home/jdxochen/exercise> ...

  9. Kubernetes对象之Pod

    系列目录 Pod是Kubernetes调度的最小单元.一个Pod可以包含一个或多个容器,因此它可以被看作是内部容器的逻辑宿主机.Pod的设计理念是为了支持多个容器在一个Pod中共享网络和文件系统 因此 ...

  10. 科学计算 | Matlab 使用 GPU 并行计算

    科学计算 | Matlab 使用 GPU 并行计算 本文转载自:  https://sanwen8.cn/p/14bJc10.html       Matlab下直接使用GPU并行计算(预告)< ...