1446A - Knapsack - CodeForces Solution


constructive algorithms greedy sortings *1300

Please click on ads to support us..

C++ Code:

//Jai Shree Ram
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>


using namespace std;
using namespace __gnu_pbds;

#define int long long int
#define pb push_back
#define vi vector<int>
#define endl "\n"
#define all(p) p.begin(), p.end()
#define rep(i,l,r) for(int i=l;i<r;i++)
#define pin pair<int,int>
#define vpin vector<pin>
#define yes() cout<<"YES"<<endl
#define no() cout<<"NO"<<endl
const int N = 1e9+7;
const int M = 1e6+1;
int is_prime[M];
void sieve()
{
  int maxN=1e6;
  for(int i=1;i<=maxN;i++) is_prime[i]=1;
  is_prime[0]=is_prime[1]=0;
  
  for(int i=2;i*i<=maxN;i++)
  {
    if(is_prime[i])
    {
      for(int j=i*i;j<=maxN;j+=i)
      is_prime[j]=0;
    }
  }
}

int power(int x,int n)
{
    int var = 1;
    while(n>0)
    {
        if(n&1)
        {
            var = var*x;
            n--;
        }
        else
        {
            x*=x;
            n/=2;
        }
        
    }
    return var;
    
}

int binary_search(vector<int>&v,int n,int x)
{
    int low = 0;
    int high = n-1;
    int mid;
    while(high-low>1)
    {
        mid = (low+high)/2;
        if(x==v[mid])
        return -1;
        else if(x>v[mid])
        {
            low = mid + 1;
        }
        else if(x<v[mid])
        {
            high = mid-1;
        }
    }
    if(x==v[high]||x==v[low])
    return -1;
    else
    {
        if(x>v[high])
        return high+1;
        else if(x<v[low])
        return low;
        else 
        return low+1;
    }
}


int powermod(int a, int b, int m)
{
    if (b == 0) return 1;
    int k = powermod(a, b / 2, m);
    k = k * k;
    k %= m;
    if (b & 1) k = (k * a) % m ;
    return k;
}
 
int inverse(int a, int mod) {
    return powermod(a, mod - 2, mod);
}

int kadane(vector<int>&v,int n)
{
  int current =0,maximum=0;
  for(int i=0;i<n;i++)
  {
    current+=v[i];
    if(current<0)
    current=0;
    maximum = max(maximum,current);
  }
  return maximum;
}



int32_t main() {
	// your code goes here
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
	
	  int n,wi;
	  cin>>n>>wi;
	  vi w(n);
	  rep(i,0,n)
	  cin>>w[i];
	  int index = -1;
	  rep(i,0,n)
	  {
	      if(w[i]<=wi&&w[i]>=(wi+1)/2)
	      {
	          index = i;
	          break;
	      }
	  }
	  if(index!=-1)
	  cout<<1<<endl<<index+1<<endl;
	  else
	  {
	      vpin weigh;
	      rep(i,0,n)
	      {
	          if(w[i]<(wi+1)/2)
	          {
	              weigh.pb({w[i],i});
	          }
	      }
	   //   sort(all(weigh));
	      int x = weigh.size();
	      int sum=0;
	      vi ind;
	      rep(i,0,x)
	      {
	          
	          sum+=weigh[i].first;
	          int num = weigh[i].second;
	          ind.pb(num);
	          if(sum>=(wi+1)/2)
	          break;
	          
	      }
	      if(sum>=(wi+1)/2)
	      {
	          cout<<ind.size()<<endl;
	          rep(i,0,ind.size())
	          cout<<ind[i]+1<<" ";
	         cout<<endl;
	      }
	      else
	      cout<<-1<<endl;
	  }
	    
	}
	return 0;
}




Comments

Submit
0 Comments
More Questions

1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh
1721E - Prefix Function Queries
977E - Cyclic Components
1140D - Minimum Triangulation
75C - Modified GCD
1722A - Spell Check
1722B - Colourblindness
1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets
933A - A Twisty Movement
1722F - L-shapes
1196B - Odd Sum Segments
1325D - Ehab the Xorcist
552B - Vanya and Books
1722E - Counting Rectangles
168A - Wizards and Demonstration
168B - Wizards and Minimal Spell
7A - Kalevitch and Chess
912B - New Year's Eve
1537C - Challenging Cliffs
879B - Table Tennis