You are taking part in the development of a “New Generation” operating system and the NG file system. In this file system all disk space is divided into N clusters of the equal sizes, numbered by integers from 1 to N. Each file occupies one or more clusters in arbitrary areas of the disk. All clusters that are not occupied by files are considered to be free. A file can be read from the disk in the fastest way, if all its clusters are situated in the successive disk clusters in the natural order.
Rotation of the disk with constant speed implies that various amounts of time are needed for accessing its clusters. Therefore, reading of clusters located near the beginning of the disk performs faster than reading of the ones located near its ending. Thus, all files are numbered beforehand by integers from 1 to K in the order of descending frequency of access. Under the optimal placing of the files on the disk the file number 1 will occupy clusters 1, 2, . . . , S1, the file number 2 will occupy clusters S1 + 1, S1 + 2, . . . , S1 + S2 and so on (here Si is the number of clusters which the i-th file occupies).
    In order to place the files on the disk in the optimal way cluster-moving operations are executed. One cluster-moving operation includes reading of one occupied cluster from the disk to the memory and writing its contents to some free cluster. After that the first of them is declared free, and the second one is declared occupied.
    Your goal is to place the files on the disk in the optimal way by executing the minimal possible number of cluster-moving operations.
Input
The first line of the input is an integer M, then a blank line followed by M datasets. There is a blank line between datasets.
    The first line of each dataset contains two integers N and K separated by a space (1 ≤ K < N ≤ 10000). Then K lines follow, each of them describes one file. The description of the i-th file starts with the integer Si that represents the number of clusters in the i-th file (1 ≤ Si < N). Then Si integers follow separated by spaces, which indicate the cluster numbers of this file on the disk in the natural order.
    All cluster numbers in the input file are different and there is always at least one free cluster on the disk.
Output
For each dataset, your program should write to the output file any sequence of cluster-moving operations that are needed in order to place the files on the disk in the optimal way. Two integers Pj and Qj separated by a single space should represent each cluster-moving operation. Pj gives the cluster number that the data should be moved FROM and Qj gives the cluster number that this data should be moved TO.
    The number of cluster-moving operations executed should be as small as possible. If the files on the disk are already placed in the optimal way the output should contain only the string ‘No optimization needed’.
    Print a blank line between datasets.
Sample Input
2
20 3
4 2 3 11 12
1 7
3 18 5 10
30 4
2 1 2
3 3 4 5
2 6 7
8 8 9 10 11 12 13 14 15
Sample Output
2 1
3 2
11 3
12 4
18 6
10 8
5 20
7 5
20 7
No optimization needed

问题链接:UVA669 LA5662 Defragment
问题简述:给定n个簇(cluster),其标号1到n;每个簇给定k个文件;每个文件给出要占几个空间,并且给出占的空间有哪些;把这些文件排序,问需要有哪些移动?
问题分析:给代码,暂时不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA669 LA5662 Defragment */#include <bits/stdc++.h>using namespace std;const int N = 10000;
int n, k, pos[N + 1];void solve()
{bool flag = true;int i, end;for(;;) {for(i = 1, end = 0; i <= n; i++) {if(pos[i] == 0 || pos[i] == i) continue;if(pos[pos[i]] == 0) {printf("%d %d\n", i, pos[i]);pos[pos[i]] = pos[i];pos[i] = 0;flag = false;end = -1;}if(end == 0) end = i;}if(i > n && end == 0) break;if(end < 0) continue;for(i = n; k > 0 && pos[i]; i--);printf("%d %d\n", end, i);pos[i] = pos[end];pos[end] = 0;flag = false;}if(flag) printf("No optimization needed\n");
}int main()
{int t, k;scanf("%d", &t);while(t--) {memset(pos, 0, sizeof(pos));scanf("%d%d", &n, &k);int cnt = 0, s, b;for(int i = 0; i < k; i++) {scanf("%d", &s);for(int j = 0; j < s; j++) {scanf("%d", &b);pos[b] = ++cnt;}}solve();if(t) printf("\n");}return 0;
}

UVA669 LA5662 Defragment【暴力】相关推荐

  1. 喵哈哈村的魔法考试 Round #1 (Div.2) 题解源码(A.水+暴力,B.dp+栈)

    A.喵哈哈村的魔法石 发布时间: 2017年2月21日 20:05   最后更新: 2017年2月21日 20:06   时间限制: 1000ms   内存限制: 128M 描述 传说喵哈哈村有三种神 ...

  2. 哑谜,回文和暴力之美

    暴力搜索是一个有趣的东西.至少刘汝佳是这么认为的.编程之美的4.10节就是典型的暴力题.虽然作者将其难度定义为一颗星,但却不能因此认为这个类型的问题就是那么容易的,很多可能需要一些有创造力的想法. 不 ...

  3. Leetcode 15.三数之和 双指针 or 暴力哈希

    题目链接:传送门 题目:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组. 暴力+ ...

  4. HDU - 5875 2016 ACM/ICPC 大连网络赛 H题 暴力

    题目链接 题意:给你一个区间l,r一直将val[l]模上val[l+1],val[l+2]...val[r],因为一个模上比前一个数小数是没有意义的,所以需要将每一个点找到右边第一个小于他的点就行. ...

  5. 2015湖南省省赛 阶乘除法 暴力

    阶乘除法 Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: NBUT 1643 Description 输入两个正整数 n ...

  6. 入门系列之使用fail2ban防御SSH服务器的暴力破解攻击

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由SQL GM 发表于云+社区专栏 介绍 对于SSH服务的常见的攻击就是暴力破解攻击--远程攻击者通过不同的密码来无限次地进行登录尝试. ...

  7. UVALive 7070 The E-pang Palace 暴力

    The E-pang Palace Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem ...

  8. POJ 3174 暴力枚举

    思路: 暴力枚举三个点 判一判 搞定 (x1*y1=x2*y2) x1.y1.x2.y2为他们两两的差 //By SiriusRen #include <cstdio> using nam ...

  9. 2016 多校赛3 A 水 B 期望,规律 C 各种博弈 J 物理题,积分 K 暴力,水

    2016 Multi-University Training Contest 3 A - Sqrt Bo 题意:给一个数 n,问n要多少次平方后化为1,如果超过5次输出"TAT". ...

最新文章

  1. 【转】开源机器学习之RWeka
  2. EnvironmentError: [WinError 5] 拒绝访问
  3. 射灯安装方法图解_射灯更换安装方法图解
  4. 2019牛客暑期多校训练营(第十场)C - Gifted Composer (二分+哈希)
  5. 客户端程序自动更新(升级)的方式
  6. java arraylist add时默认调用tostring_java中如何调用toString命名的方法?
  7. AliSQL主从复制
  8. 《Photoshop Lightroom4 经典教程》目录—导读
  9. 读取xlsx,根据模板图片批量添加文字生成相关图片,如证书,奖状,名片等
  10. 基于BC95之AT命令的学习
  11. 获取chrome的network内容并选择下载其中的资源
  12. Excel常用函数公式20例
  13. 什么样的人适合3D打印培训课程——宁波清车3D打印培训
  14. Java中NIO详解
  15. 论文笔记-Understanding Convolution for Semantic Segmentation
  16. 【每日新闻】三星电子市值跌至全球第18位,落后中国阿里腾讯 | 中国AI领袖人物吴甘沙:无人驾驶技术距离我们已经不再遥远
  17. ARCore:ARCore开发的起步
  18. 组播路由协议基础——PIM-SM静态RP
  19. 【购房必备知识】成都落户政策调研(主要介绍——研究生落户)
  20. 晶体封装越小esr越大_晶振与晶体的参数详细介绍

热门文章

  1. [转] openssl dgst命令完成SHA256哈希校验和RSA数字签名
  2. C# MessageBox 使用总结
  3. 动态创建Sql Server用户及其权限
  4. C++小白课本练习4
  5. SQL 基础面试题(四)
  6. 大数据开发之Hadoop---初始Hadoop
  7. android中注册代码怎么写,Android P HIDL demo代码编写 (原创)
  8. python selenium自动化获取oracle_Python+selenium2 自动化测试登录
  9. python 全局变量使用报错没有定义_python跨文件使用全局变量的实现
  10. 线上环境websocket连接地址_WebSocket:沙盒里的TCP