Problem IInstalling AppsTime limit: 2 seconds
Mobile radio telephone, public domainSandra recently bought her first smart phone. One ofher friends suggested a long list of applications (morecommonly known as “apps”) that she should install onthe phone. Sandra immediately started installing theapps from the list, but after installing a few, the phonedid not have enough disk space to install any more apps.Sometimes, the app installation failed because there wasnot even enough space to download the installation pack-age. Other apps could be downloaded just fine, but had insufficient space to store the installed app.Each app that Sandra installs has a download sized and a storage size s To download the app, Sandra’s phone must have at least d megabytes of free disk space. After the app has been installed, it then uses s megabytes of disk space on the phone. The download size may be smaller than the storage size (e.g., if the app data is heavily compressed) or larger than the storage size (e.g., if the download contains material that might not get used such as translations to different languages). The installer is very efficient and can transform the downloaded package to an installed app without using any extra disk space. Thus, to install an app, the phone must have at least
max(d, s)megabytes of freedisk space.Sandra quickly realised that she may have run out of space just because she installed apps in thewrong order. Thus, she decided to give the installation another try. She uninstalled all apps, andwill now choose an installation order that lets her install the largest number of apps from the list.
Sandra may not install any app more than once.Help her determine what apps on the list she should install, and in what order.
InputThe input consists of:•One line with two integersn,c(1≤n≤500,1≤c≤10 000), the number of availableapps and the availabledisk space of the phone in megabytes.
•
n lines, each with two integersd, s(1≤d, s≤10 000), the download size and storagesize of an app, in megabytes.
Output
Output one line with the maximum number of apps that can be installed. Then output one line
listing the numbers of those apps, in the order that Sandra should install them. In the case that
no apps can be installed, this line can be omitted.
The apps are numbered from1to n , in the order they are given in the input. If there are multiple
optimal solutions, output any one of them.
NWERC 2017 Problem I: Installing Apps
1 dp[j] j表示的是我们用包里面j的容量会产生最大的价值,这个时候,包内剩余的容量是c-j;
如果是我们想要拿第i个物品来更新j的状态的话我们会有,这个状态的最大dp[j-[i].v],并且对于这个
状态包内剩余的空间是c-(j-[i].v);
一般01背包的最大效益与物品排放的顺序是没有关系的,但是这里放东西的前提是我们必须先有足够大
的空间,那么我们放的顺序如果是不同的话就会出现不同的价值;
按照d-s值从大到小排序,我们可以这么想d-s很大的话,d一定很大,s很小如果我们前面不加入的话,后面它的可以用的体积减小了,就更不可能加上了;所以放的话一定是在前面放;
对于某一个体积下产生的最大值要放的物品数目是一定的,我们要记录状态的转移,这里我们可以用vector来记录一下;
#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5+10;
typedef long long ll;
struct node {
ll s,d,id;
}p[Max];
bool cmp(node a, node b){
return a.d-a.s>b.d-b.s;
}
ll dp[Max];
vector <int> pre[Max];
int main(){
int n,c;
scanf("%d %d",&n,&c);
for(int i=1;i<=n;i++){
scanf("%lld %lld",&p[i].d,&p[i].s);
p[i].id=i;
}
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n;i++){
for(int j=c;j>=p[i].s;j--){
if(c>=j-p[i].s+p[i].d){
if(dp[j]<dp[j-p[i].s]+1){
dp[j]=dp[j-p[i].s]+1;
pre[j]=pre[j-p[i].s];
pre[j].push_back(p[i].id);
}
}
}
}
int ans=0,id=0;
for(int i=1;i<=c;i++){
if(dp[i]>ans){
ans=dp[i];
id=i;
}
}
printf("%d\n",ans);
int len=pre[id].size();
for(int i=0;i<len;i++){
int to=pre[id][i];
printf("%d%c",to,(i==len-1)?'\n':' ');
}
return 0;
}