标签:result where moment system str move min remove mini
Input
Output
Sample Input
5 2 1 3 a 2 a 3 r a 4 p 2 r a 5 r e
Sample Output
2 5
我不管怎么调都不能在UVALive、ZOJ上通过,UVALive是WA,ZOJ是PE,我估计是输出格式的问题,但我已经用完我的想象力了实在不知道该怎么输出,英语也不怎么好,不怎么读的懂。
这道题就是模拟,因为不知道会有多少个数据输进来,所以我存储移除列表和进程的数组都是用的动态分配内存,用完后在释放,然后在重新申请。存储进程的数组的大小可以根据进程的最大开销来分配,存储移除列表的数组根据题目给的数分配。
1 //removal list是输出第i个被移除的进程 2 //如1、3,输出第1个和第3个被移除的进程 3 #include <stdio.h> 4 #include <malloc.h> 5 #include <string.h> 6 7 int maxcost;//the maximum cost of the processes 8 int p=1;//policy 9 int rlength;//the length of the process 10 int *rlist=NULL;//用来存储移除列表的数据 11 char op;//操作 12 int *pro=NULL;//进程数组 13 int j,k=0;//j为第几次移除,k为是否成功移除的标志,0为否,1为是 14 15 void re(){ 16 int i,a; 17 if(p==1){ 18 for(i=0;i<maxcost;i++){ 19 if(pro[i]!=0){//队列非空 20 a=pro[i]; 21 pro[i]=0; 22 j++; 23 k=1; 24 } 25 } 26 } 27 else{ 28 for(i=maxcost-1;i>=0;i--){ 29 if(pro[i]!=0){//队列非空 30 a=pro[i]; 31 pro[i]=0; 32 j++; 33 k=1; 34 } 35 } 36 } 37 if(k==0) printf("-1\n");//空队列输出-1 38 else{ 39 for(i=0;i<rlength;i++){//在移除队列中查找有无当前的j值,有则输出,并将k置0 40 if(rlist[i]==j){ 41 printf("%d\n",a); 42 k=0; 43 } 44 } 45 } 46 } 47 48 int main(void){ 49 int i; 50 while(scanf("%d",&maxcost)!=EOF){ 51 j=0;//初始化移除次数为0 52 pro=(int*)malloc(maxcost*sizeof(int));//分配存储进程的数组需要的空间 53 memset(pro,0,maxcost*sizeof(int)); 54 scanf("%d",&rlength); 55 rlist=(int*)malloc(rlength*sizeof(int));//分配存储列表的数组需要的空间 56 if(rlist!=NULL){ 57 for(i=0;i<rlength;i++) scanf("%d",&rlist[i]);//读入removal ist 58 } 59 //开始操作 60 while(1){ 61 scanf("%c",&op);scanf("%c",&op);//读取操作符 62 if(op==‘a‘){ 63 scanf("%d",&i); 64 pro[i-1]=i; 65 }else if(op==‘r‘){ 66 re();//移除并输出 67 }else if(op==‘p‘){ 68 scanf("%d",&p);//更换policy 69 }else if(op==‘e‘){//退出 70 break; 71 }else break; 72 } 73 printf("\n"); 74 //释放空间 75 free(rlist); 76 free(pro); 77 } 78 return 0; 79 }
标签:result where moment system str move min remove mini
原文地址:https://www.cnblogs.com/20174317zhuyuan/p/9416704.html