diff options
Diffstat (limited to '1.4/barn1.c')
-rw-r--r-- | 1.4/barn1.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/1.4/barn1.c b/1.4/barn1.c new file mode 100644 index 0000000..65039b3 --- /dev/null +++ b/1.4/barn1.c @@ -0,0 +1,60 @@ +/* +ID: mytbk921 +LANG: C +TASK: barn1 +*/ + +#include <stdio.h> +#define MAXS 200 + +int main() +{ + int MBoards,nStalls,nCows; + int l,r,ngaps,occupied; + int stalls[MAXS+10]={},gaps[MAXS]; + int i,j; + + FILE *fin=fopen("barn1.in","r"); + FILE *fout=fopen("barn1.out","w"); + fscanf(fin,"%d%d%d",&MBoards,&nStalls,&nCows); + l=nStalls; r=0; //initial the left&right bound + for (i=0;i<nCows;++i){ + fscanf(fin,"%d",&j); + stalls[j]=1; + if (j<l) //update the bound + l=j; + if (j>r) + r=j; + } + fclose(fin); + + ngaps=0; + for (i=l;i<=r;i++){ + if (!stalls[i]){ //have a gap + for (j=i;!stalls[j];j++) + ; + gaps[ngaps++]=j-i; + i=j; //the i will continue + } + } + //selection sort + for (i=0;i<ngaps&&i<MBoards-1;i++){ //select the largest gaps + int max=i; //form gaps[i] to gaps[ngaps-1] + for (j=i+1;j<ngaps;j++){ + if (gaps[j]>gaps[max]) + max=j; + } + int tmp=gaps[max]; + gaps[max]=gaps[i]; + gaps[i]=tmp; //swap finished + } + + //calculate + occupied=r-l+1; + for (i=0;i<MBoards-1&&i<ngaps;i++) + occupied-=gaps[i]; + + fprintf(fout,"%d\n",occupied); + return 0; +} + |