You are not logged in. Please login at www.codechef.com to post your questions!

×

Fill the bags

Please tell me the exact logic to solve this question. I've tried a lot but always end up with WA. Here is the link of the question - http://www.codechef.com/CDSU2015/problems/MHTBAG

asked 31 Jan '15, 12:16

ar56's gravatar image

3★ar56
-32
accept rate: 0%


It w1 is odd, the we need one n1 object to fill the last one space. If n1==0, then the point is wasted. so, if w1 is odd, then subtract 1 from it (the last space), if n1>0, add 1 to answer and reduce one from n1. Do the same for w2.

Now you have two bags each with even number weight capacity. Now, each object in n2 carries 2 units of weight. So total weight is 2*n2.

Let, n2=2*n2; and W=w1+w2;

So now, n2 and n1 are total weight available and W is the total weight capacity.

If W<=n2, it means we can fill all the weight in W by n2.

So, apply
{
ans+=W; //As W weight is available
n2-=W;
W=0;
}

else if(W>n2) // this means W can accommodate all n2 weight and will have spare space.
{
W-=n2;
ans+=n2;
n2=0;
}

Do the exact same for n1 and print the value in ans.

p.s. In solving with n2, if W becomes 0, it doesn't cause any problem with n1 part as we are adding and subtracting 0 from any value.

link

answered 31 Jan '15, 15:05

dragonemperor's gravatar image

3★dragonemperor
89321135
accept rate: 10%

Easy to understand Solution

#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long

int main(){
    ll t, w1,w2,n1,n2,W;
    cin>>t;
    while(t--){
        cin>>w1>>w2>>n1>>n2;
        ll ans = 0;
        ll temp = min(w1/2+w2/2,n2);
        ans=2*temp;
        ans += min(w1+w2-2*temp, n1);
        cout<<ans;
    }
    return 0;
}
link

answered 06 Jun '17, 21:22

rkm123's gravatar image

2★rkm123
11
accept rate: 0%

edited 06 Jun '17, 21:26

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×4
×1

question asked: 31 Jan '15, 12:16

question was seen: 429 times

last updated: 06 Jun '17, 21:26