POJ 1033

来源:互联网 发布:windows 2000系统下载 编辑:程序博客网 时间:2024/04/30 15:58
Defragment
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 3286 Accepted: 1144Case Time Limit: 1000MS Special Judge

Description

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 file 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

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". 

Sample Input

20 34 2 3 11 121 73 18 5 10

Sample Output

2 13 211 312 418 610 85 207 520 7

Source

Northeastern Europe 1998
这题做的相当无语,数组多开了10倍,结果memset慢成狗了,疯狂的TLE。。。
一道小模拟,思路来自http://blog.csdn.net/kindlucy/article/details/7447855

a[i] 代表在i位置放了应该放在a[i]位置的节点,b[i] 代表应该放在i位置的节点现在在b[i]位置

搜索的时候从i=1开始,如果a[i] == i,代表节点位置正确,不用移动

如果a[i] != i,代表位置摆放不正确

这里,又分两种情况:

1) 如果a[i] == 0,说明 i位置现在为空,只要简单的将节点从b[i] 移到 i 就可以了。

2) 如果a[i] != 0,说明i位置现在有节点占着,要先把i位置上的节点放到a[i] 位置,而如果a[a[i]] != 0,则意味着a[i]位置上也有节点占着,要先将a[i]位置上的节点移到正确的位置。于是,我取了一个栈,把这些搜索过程中遇到的节点放入栈中,直到搜索到为空的位置,然后再一次把栈中的节点移到他们正确的位置。当然,也有可能最后出现一个循环,所以,我用了c[i]来记录栈中已有的节点,c[i]=1代表此次搜索,i位置已被访问过,如果出现了循环,则直接从最后的节点往前搜一个空位置,然后把栈顶的节点先移到空位置,等其余节点都在正确的位置时,再把那个节点从空位置已到它应在的正确位置。

#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , m  , pos << 1#define rson m + 1 , r , pos << 1|1#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 10000 + 100;const int maxw = 10000000 + 20;const int MAXNNODE = 10000 +10;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e-8#define mod 10007typedef long long LL;const double PI = acos(-1.0);typedef double D;int a[MAXN] , b[MAXN] , c[MAXN] , Stack[MAXN] , top = 0;int n , k;int main(){    //ios::sync_with_stdio(false);#ifdef Online_Judge    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif // Online_Judge    memset(a , 0 , sizeof(a));memset(b , 0 , sizeof(b));memset(c , 0 , sizeof(c));    cin >> n >> k;    int s , m = 0;    FORR(i , 1 , k)    {        cin >> s;        FORR(j , 1 , s)        {            int tmp ;            cin >> tmp;            a[tmp] = ++m;            b[m] = tmp;        }    }    bool need = false;    FORR(i , 1 , m )    {        int t = i;        while(a[t] != t)        {            need = true;            if(c[t] == 1)            {                REP(j , n , 0)                {                    if(a[j] == 0)                    {                        cout << b[Stack[top]] << ' ' << j << endl;                        b[Stack[top]] = j;                        a[j] = Stack[top];                        top--;                        break;                    }                }                break;            }            Stack[++top] = t;            if(a[t] == 0)            {                break;            }            else            {                c[t] = 1;                t = a[t];            }    }            while(top)            {                int Top = Stack[top];                cout << b[Top] << ' '<<Top << endl;                a[Top] = Top;                a[b[Top]] = 0;                b[Top] = top;                top--;            }            memset(c , 0 , sizeof(c));    }    if(!need)    {        puts("No optimization needed") ;    }    return 0;}


原创粉丝点击