題目大意算法
Clair 有一個容量爲V的書包,有N本體積爲book[i]的書,還有M份體積爲paper[i]的紙,若是把紙摺疊,紙的體積就變爲[paper[i]/2](不大於paper[i]/2的整數),問在不超過揹包容量的狀況下,怎麼裝更多數量的的物品markdown
輸入第一行是case的數量,第二行分別是N,M,V,接下來的N行是書的體積,M行是紙的體積spa
sample inputcode
1orm
2 1 5排序
10input
5it
2io
sample outputtable
1
Note:在示例中Claire能夠帶體積爲5的書,也能夠帶體積爲2的紙
#include <stdio.h> #include <memory.h> #define TEST int cas,n,m,v; int a[20010]; void merge(int x,int mid,int y){//分治排序 int d[20010]; int h=x,r=mid+1,t=y; int k=0; while(h<=mid&&r<=y){ if(a[h]<a[r]){ d[k++]=a[h]; h++; } else{ d[k++]=a[r]; r++; } } while(h<=mid){d[k++]=a[h++];} while(r<=y){d[k++]=a[r++];} int i=0; for (i=0;i<k;i++) { a[x+i]=d[i]; } } void mergesort(int x,int y){//分治排序算法 int mid; if (x>=y) return; mid=(x+y)/2; mergesort(x,mid); mergesort(mid+1,y); merge(x,mid,y); } int main(){ #ifdef TEST freopen("test.txt","r",stdin); freopen("testout.txt","w",stdout); #endif scanf("%d\n",&cas); int i,j,k; for (i=0;i<cas;i++) { int tmp; memset(a,0,sizeof(a)); scanf("%d %d %d\n",&n,&m,&v); for (j=0;j<n;j++) scanf("%d\n",&a[j]); for(j=n;j<n+m;j++){ scanf("%d\n",&tmp); a[j]=tmp/2;//讀取的時候對紙進行摺疊 } mergesort(0,n+m-1);//把物品從小到大排序 int weigh=0; j=0; while(j<n+m){//把排好的物品裝入書包 weigh+=a[j]; if(weigh>v) break; else j++; } printf("%d\n",j); } }複製代碼