引言:ZZU的朋友们请不要直接抄,我只是记录每次做题的成果,别查重完之后大家一起GG。
程序存储问题
设有n 个程序{1,2,…, n }要存放在长度为L的磁带上。程序i存放在磁带上的长度是 li,1≤i≤n。 程序存储问题要求确定这n 个程序在磁带上的一个存储方案, 使得能够在磁带上存储尽可能多的程序。 对于给定的n个程序存放在磁带上的长度,计算磁带上最多可以存储的程序数。
输入格式:
第一行是2 个正整数,分别表示文件个数n和磁带的长度L。接下来的1行中,有n个正整数,表示程序存放在磁带上的长度。
输出格式:
输出最多可以存储的程序数。
输入样例:
在这里给出一组输入。例如:
6 50
2 3 13 8 80 20
输出样例:
在这里给出相应的输出。例如:
5
Code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
using namespace std;
#define ARR_SIZE 10001
#define Num 500005
int main(){
int n,L;
cin>>n>>L;
int cx[n];
for(int i = 0; i < n; i++){
cin>>cx[i];
}
for(int i = 0; i < n-1; i++){
for(int j = n-1; j >= i; j--){
if(cx[j] < cx[j-1]){
int m = cx[j];
cx[j] = cx[j-1];
cx[j-1] = m;
}
}
}
int res = 0,temp = 0;
for(res; res < n; res++){
if(temp+= cx[res] > L) break;
}
cout<<res;
}