POJ 3914 Dull windows中动态链接库DLL在内存中的加载 算法模拟
2011-10-04 23:52
309 查看
原题地址http://poj.org/problem?id=3914
DuLL
Description
In Windows, a DLL (or dynamic link library) is a file that contains a collection of pre-compiled functions that can be loaded into a program at runtime. The two primary benefits of DLLs
are (1) only one copy of a DLL is needed in memory, regardless of how many different programs are using it at the same time, and (2) since they are separate from programs, DLLs can be upgraded independently, without having to recompile the programs that use
them. (DLLs have their problems, too, but we’ll ignore those for now.) Your job is to calculate the maximum memory usage when running a series of programs together with the DLLs they need.
The DLLs in our system are not very exciting. These dull DLLs (or DuLLs) each require a fixed amount of memory which never changes as long as the DuLL is in memory. Similarly, each program has its own fixed memory requirements which never change as long as
the program is executing. Each program also requires certain DuLLs to be in memory the entire time the program is executing. Therefore, the only time the amount of memory required changes is when a new program is executed, or a currently running program exits.
When a new program begins execution, all DuLLs required by that program that must be loaded into memory if they are not there already. When a currently running program exits, all DuLLs that are no longer needed by any currently running programs are removed
from memory.
Remember, there will never be more than one copy of a specific DuLL in memory at any given time. However, it is possible for multiple instances of the same program to be running at the same time. In this case each instance of the program would require its own
memory; however, the instances still share DuLLs in the same way two unrelated programs would.
Input
The input consists of at least one data set, followed by a line containing only 0.
The first line of a data set contains three space separated integers N P S, where N is the number of DuLLs available, 1 ≤ N ≤ 20, P is the number of programs which can be executed, 1 ≤ P ≤ 9, and S is the number of state transitions recorded, 1 ≤ S ≤ 32.
The next line contains exactly N space separated integers representing the sizes in bytes of each of the DuLLs, 1 ≤ size ≤ 1,000. Each DuLL is implicitly labeled with a letter: ‘A’, ‘B’, ‘C’, ..., possibly extending to ‘T’. Therefore the first integer is the
size of ‘A’, the second integer is the size of ‘B’, and so on.
The next P lines contain information about each of the programs, one program per line. Each line contains a single integer representing the size of the program in bytes, 1 ≤ size ≤ 1,000, followed by 1 to N characters representing the DuLLs required by that
program. There will be a single space between the size of the program and the DuLL labels, but no spaces between the labels themselves. The order of the labels is insignificant and therefore undefined, but they will all be valid DuLL labels, and no label will
occur more than once. Each program is implicitly labeled with an integer: 1, 2, 3, ... possibly extending to 9.
The final line of the data set will contain S space separated integers. Each integer will either be a positive number q, 1 ≤ q ≤ P, indicating that a new execution of program q has begun, or else it will be a negative number –q, 1 ≤ q ≤ P, indicating that a
single execution of program q has completed. The transitions are given in the order they occurred. Each is a valid program number; if it is a negative number –q then there will always be at least one instance of program q running.
Output
There is one line of output for each data set, containing only the maximum amount of memory required throughout the execution of the data set.
Sample Input
5 4 8
100 400 200 500 300
250 AC
360 ACE
120 AB
40 DE
2 3 4 -3 1 2 -2 1
0
2110
DuLL
Time Limit: 1000MS | Memory Limit: 65536K | |
In Windows, a DLL (or dynamic link library) is a file that contains a collection of pre-compiled functions that can be loaded into a program at runtime. The two primary benefits of DLLs
are (1) only one copy of a DLL is needed in memory, regardless of how many different programs are using it at the same time, and (2) since they are separate from programs, DLLs can be upgraded independently, without having to recompile the programs that use
them. (DLLs have their problems, too, but we’ll ignore those for now.) Your job is to calculate the maximum memory usage when running a series of programs together with the DLLs they need.
The DLLs in our system are not very exciting. These dull DLLs (or DuLLs) each require a fixed amount of memory which never changes as long as the DuLL is in memory. Similarly, each program has its own fixed memory requirements which never change as long as
the program is executing. Each program also requires certain DuLLs to be in memory the entire time the program is executing. Therefore, the only time the amount of memory required changes is when a new program is executed, or a currently running program exits.
When a new program begins execution, all DuLLs required by that program that must be loaded into memory if they are not there already. When a currently running program exits, all DuLLs that are no longer needed by any currently running programs are removed
from memory.
Remember, there will never be more than one copy of a specific DuLL in memory at any given time. However, it is possible for multiple instances of the same program to be running at the same time. In this case each instance of the program would require its own
memory; however, the instances still share DuLLs in the same way two unrelated programs would.
Input
The input consists of at least one data set, followed by a line containing only 0.
The first line of a data set contains three space separated integers N P S, where N is the number of DuLLs available, 1 ≤ N ≤ 20, P is the number of programs which can be executed, 1 ≤ P ≤ 9, and S is the number of state transitions recorded, 1 ≤ S ≤ 32.
The next line contains exactly N space separated integers representing the sizes in bytes of each of the DuLLs, 1 ≤ size ≤ 1,000. Each DuLL is implicitly labeled with a letter: ‘A’, ‘B’, ‘C’, ..., possibly extending to ‘T’. Therefore the first integer is the
size of ‘A’, the second integer is the size of ‘B’, and so on.
The next P lines contain information about each of the programs, one program per line. Each line contains a single integer representing the size of the program in bytes, 1 ≤ size ≤ 1,000, followed by 1 to N characters representing the DuLLs required by that
program. There will be a single space between the size of the program and the DuLL labels, but no spaces between the labels themselves. The order of the labels is insignificant and therefore undefined, but they will all be valid DuLL labels, and no label will
occur more than once. Each program is implicitly labeled with an integer: 1, 2, 3, ... possibly extending to 9.
The final line of the data set will contain S space separated integers. Each integer will either be a positive number q, 1 ≤ q ≤ P, indicating that a new execution of program q has begun, or else it will be a negative number –q, 1 ≤ q ≤ P, indicating that a
single execution of program q has completed. The transitions are given in the order they occurred. Each is a valid program number; if it is a negative number –q then there will always be at least one instance of program q running.
Output
There is one line of output for each data set, containing only the maximum amount of memory required throughout the execution of the data set.
Sample Input
5 4 8
100 400 200 500 300
250 AC
360 ACE
120 AB
40 DE
2 3 4 -3 1 2 -2 1
0
Sample Output
16002110
/************************************************************************** * Problem: POJ 3914 Dull * Copyright 2011 by Yan * DATE: * E-Mail: yming0221@gmail.com ************************************************************************/ #include <stdio.h> int n,p,s; int dulls[21],programs[10]; int exist[21]; /*表示此dull加载进内存使用次数*/ char need[10][22]; int main() { int i,j,sum,tran,max; /*freopen("input","r",stdin);*/ while(1) { sum=0,max=-1; for(i=0;i<21;i++) exist[i]=0; scanf("%d",&n); if(n==0) break; scanf("%d %d",&p,&s); for(i=0;i<n;i++) scanf("%d",&dulls[i]); for(i=0;i<p;i++) scanf("%d %s",&programs[i],need[i]); for(i=0;i<s;i++) { scanf("%d",&tran); if(tran>0) { tran--; sum+=programs[tran]; max=max>sum?max:sum; for(j=0;j<strlen(need[tran]);j++) { if(exist[need[tran][j]-'A']==0) { sum+=dulls[need[tran][j]-'A']; max=max>sum?max:sum; } exist[need[tran][j]-'A']++; } } else { tran=-1-tran; sum-=programs[tran]; max=max>sum?max:sum; for(j=0;j<strlen(need[tran]);j++) { exist[need[tran][j]-'A']--;/*引用数减1*/ if(exist[need[tran][j]-'A']==0) { sum-=dulls[need[tran][j]-'A']; max=max>sum?max:sum; } } } } printf("%d\n",max); } }
相关文章推荐
- POJ 3914 Dull windows中动态链接库DLL在内存中的加载 算法模拟
- 从内存中加载DLL
- 动态链接库dll的使用方法——动态加载dll
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
- 一个内存加载dll的代码
- 在vs2005中win32控制台应用程序中加载动态链接库(.dll)文件
- 动态链接库知识点总结之三(如何以显示的方式加载DLL)
- DLL中用malloc分配了一块内存,但是在exe程序中释放引发的错误:其原因可能是堆被损坏,这也说明 **.exe 中或它所加载的任何 DLL 中有 bug。
- 动态链接库dll的静态加载与动态加载
- 工具:内存加载dll
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
- 静态链接库(LIB)和动态链接库(DLL),DLL的静态加载和动态加载,两种LIB文件。
- Delphi从内存加载dll,无需从资源中释放dll
- 动态链接库dll的 静态加载 与 动态加载
- 从内存资源中加载DLL 模拟PE加载器
- 动态链接库dll的 静态加载 与 动态加载
- 如何在C#中加载自己编写的动态链接库(DLL)
- [转载]a从内存中加载DLL
- windows服务器下IIS6/7下PHP 无法加载 php_curl.dll 等动态链接库