• BOJ1495 Schoolbag


    题目大意

    Clair 有一个容量为V的书包,有N本体积为book[i]的书,还有M份体积为paper[i]的纸,如果把纸折叠,纸的体积就变为[paper[i]/2](不大于paper[i]/2的整数),问在不超过背包容量的情况下,怎么装更多数量的的物品

    输入第一行是case的数量,第二行分别是N,M,V,接下来的N行是书的体积,M行是纸的体积

    sample input

    1

    2 1 5

    10

    5

    2

    sample output

    1

    Note:在示例中Claire可以带体积为5的书,也可以带体积为2的纸

     1 #include <stdio.h>
     2 #include <memory.h>
     3 #define TEST
     4 int cas,n,m,v;
     5 int a[20010];
     6 void merge(int x,int mid,int y){//分治排序
     7     int d[20010];
     8     int h=x,r=mid+1,t=y;
     9     int k=0;
    10     while(h<=mid&&r<=y){
    11         if(a[h]<a[r]){
    12             d[k++]=a[h];
    13             h++;
    14         }
    15         else{
    16             d[k++]=a[r];
    17             r++;
    18         }
    19     }
    20     while(h<=mid){d[k++]=a[h++];}
    21     while(r<=y){d[k++]=a[r++];}
    22     int i=0;
    23     for (i=0;i<k;i++)
    24     {
    25         a[x+i]=d[i];
    26     }
    27 }
    28 void mergesort(int x,int y){//分治排序算法
    29     int mid;
    30     if (x>=y)
    31         return;
    32     mid=(x+y)/2;
    33     mergesort(x,mid);
    34     mergesort(mid+1,y);
    35     merge(x,mid,y);
    36 }
    37 
    38 int main(){
    39 #ifdef    TEST
    40     freopen("test.txt","r",stdin);
    41     freopen("testout.txt","w",stdout);
    42 #endif
    43     scanf("%d\n",&cas);
    44     int i,j,k;
    45     for (i=0;i<cas;i++)
    46     {
    47 
    48         int tmp;
    49         memset(a,0,sizeof(a));
    50         scanf("%d %d %d\n",&n,&m,&v);
    51         for (j=0;j<n;j++)
    52             scanf("%d\n",&a[j]);
    53         for(j=n;j<n+m;j++){
    54             scanf("%d\n",&tmp);
    55             a[j]=tmp/2;//读取的时候对纸进行折叠
    56         }
    57         mergesort(0,n+m-1);//把物品从小到大排序
    58         int weigh=0;
    59         j=0;
    60         while(j<n+m){//把排好的物品装入书包
    61             weigh+=a[j];
    62             if(weigh>v)
    63                 break;
    64             else
    65                 j++;
    66         }
    67         printf("%d\n",j);
    68 
    69 
    70     }
    71 }
  • 相关阅读:
    PHP 获取请求里的 header字段以及发送header
    宝塔链接数据库
    链表中头结点和头指针
    git How to resolve a Git “CONFLICT (modify/delete)”?
    yii 执行mysql:既要添加数据又要更新数据
    recv() failed (104: Connection reset by peer) while reading response header from upstream
    Linux磁盘空间满了,定位到mysql日志,删除
    mysql 字符乱码
    php message queue
    CGI 、fastcgi、php-cgi、php-fpm
  • 原文地址:https://www.cnblogs.com/txlbupt/p/2991165.html
Copyright © 2020-2023  润新知