Posted: Wed Mar 02, 2005 8:04 am
There is no need for any sorting.
Use dynamic programming, optimizing with respect to favor index.
Use dynamic programming, optimizing with respect to favor index.
your program should produce0 0
0
Strange, my program's output is 1 and it was accepted.1900 3
2000 5
1950 1
101 1
output:
2
Code: Select all
removed, accept ^^
Code: Select all
edited..
Code: Select all
#include <stdio.h>
#include <algorithm>
using namespace std;
struct MoneyPoint{
int m,p;
};
int const max_v = 10001;
MoneyPoint mp[max_v];
int grid[101][max_v];
int max_f(int a,int b){
return a>b?a:b;
}
int knapsack01(int N,int L){
int i,j,tL;
for(i=0;i<=N;i++){
grid[i][0]=0;
}
tL=L;
if(L+200>2000)L+=200;
for(i=1;i<=N;i++){
for(j=1;j<=L;j++){
if(j==1 && j<mp[i].m)
grid[i][j]=0;
//problem may be in this part [][][][][][][]
else if(j==1 && j<=tL || j>2000)
grid[i][j]=mp[i].p;
else if(j<mp[i].m)
grid[i][j]=grid[i-1][j];
else{
grid[i][j]=max_f(grid[i-1][j],(j<=tL||j>2000)?mp[i].p+grid[i-1][j-mp[i].m]:grid[i-1][j]);
//[][][][][][][]
}
}
}
return grid[N][L];
}
bool comp(MoneyPoint a,MoneyPoint b){
if(a.m<b.m)return true;
if(a.m==b.m && a.p>b.p)return true;
return false;
}
int main(){
int n,m,i;
while(scanf("%d%d",&m,&n)==2){
for(i=1;i<=n;i++){
scanf("%d%d",&mp[i].m,&mp[i].p);
}
sort(mp+1,mp+n+1,comp);
printf("%d\n",knapsack01(n,m));
}
return 0;
}
Your solution's not working for:rube wrote:I cann't figure out how to solve it. I m getting WA all the time! Plz help me.
Code: Select all
8762 2
2458 4
308 1
Code: Select all
5
Code: Select all
1800 3
1950 1
2000 5
101 1
1801 3
2000 5
1955 1
101 1
1890 3
1955 1
2000 5
101 1
5000 4
1000 2
1000 2
1000 3
3200 5
0 0
1 1
1 1
5 5
1 9
2 9
3 9
1 9
2 9
Code: Select all
1
1
2
10
0
1
27
Code: Select all
1
1
1
10
0
1
27
The correct answer is:daveon wrote:Hi,
My AC code outputs:
Although I'm not sure if they are totally right.Code: Select all
1 1 1 ...
Code: Select all
1
1
2
10
0
1
27
Buying the first item for $1955 and the third one for $101 makes her bill $2056. Therefore she can apply the $200 refund and pay only $1856.problem description wrote:It is important to know that 13-Dots always uses her credit card to pay the bill, which offers her a 200-dollar refund if her total expense in the month exceeds $2000.
Code: Select all
removed
Your code isn't working for:DP wrote:What is the problem here?? Please tell me.
Here is my code:
Code: Select all
3123 2
3212 5
150 2
6733 6
969 3
2195 3
1565 2
233 5
1707 5
1702 3
3948 7
2151 4
3117 2
748 5
1463 5
1405 4
1204 5
3778 3
2957 4
362 5
1852 3
2979 2
323 3
7268 10
2846 4
2423 5
3585 5
678 1
3190 2
2078 1
3611 4
1340 2
801 1
2870 3
5890 9
1707 4
2140 2
2820 3
601 4
1195 5
1242 3
217 2
894 1
3879 2
1382 1
1791 1
565 0
3184 9
599 5
3359 1
2604 2
499 1
2103 5
261 5
3201 5
3204 5
1423 2
4678 10
1019 2
3103 1
3102 2
2675 3
23 5
3831 1
2199 1
2420 5
607 4
2249 5
Code: Select all
5
19
15
11
12
19
0
0
15
16
I agree, so therefore, this problem needs a rejudge as my solution should not get accepted.Martin Macko wrote: The correct answer is:Code: Select all
1 1 2 10 0 1 27