UVa 669 - Defragment 解题报告(暴力)


  Defragment 

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, \dots, S_1$, the file number 2 will occupy clusters $S_1 +1, S_1 +2,\dots, S_1 +S_2$ 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 file contains two integers  N  and  K  separated by a space (  $1 \le K < N \le 10000$ ). Then  K  lines follow, each of them describes one file. The description of the  i -th file starts with the integer  S i  that represents the number of clusters in the  i -th file (  $1 \le S_i < N$ ). Then  S i  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  P j  and  Q j  separated by a single space should represent each cluster-moving operation.  P j  gives the cluster number that the data should be moved FROM and  Q j  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 

1

20 3
4 2 3 11 12
1 7
3 18 5 10

Sample Output 

2 1
3 2
11 3
12 4
18 6
10 8
5 20
7 5
20 7

    解题报告:这种题目都是属于不是太好写的容易题。直接贴个代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

int pos[11111];
int num[11111];
bool suc[11111];

struct Node
{
    int p;

    bool operator<(const Node & cmp) const
    {
        return pos[p] < pos[cmp.p];
    }
} x;

void work()
{
    int T;
    scanf("%d", &T);
    fff(cas, 1, T)
    {
        memset(pos, 0, sizeof(pos));
        memset(num, 0, sizeof(num));
        memset(suc, 0, sizeof(suc));

        int n, k;
        scanf("%d%d", &n, &k);

        int tot = 1;
        fff(i, 1, k)
        {
            int m;
            scanf("%d", &m);

            ff(j, m)
            {
                int tmp;
                scanf("%d", &tmp);
                num[tmp] = tot;
                pos[tot++] = tmp;
            }
        }

        priority_queue<Node> que;
        fff(i, 1, n) if(num[i] == 0)
        {
            x.p = i;
            que.push(x);
        }

        bool noMove = true;
        int sta = 1;
        while(que.size() && sta < tot)
        {
            x = que.top();
            que.pop();

            if(pos[x.p])
            {
                printf("%d %d\n", pos[x.p], x.p);
                noMove = false;
                num[pos[x.p]] = 0, num[x.p] = x.p;
                x.p = pos[x.p];
                que.push(x);
            }
            else
            {
                while(num[sta] == sta && sta < tot)
                    sta++;
                if(sta >= tot) break;

                printf("%d %d\n", sta, x.p);
                noMove = false;
                num[x.p] = num[sta], pos[num[sta]] = x.p, num[sta] = 0;
                x.p = sta;
                que.push(x);
            }
        }

        if(noMove)
            puts("No optimization needed");

        if(T-cas) puts("");
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值