1498B - Box Fitting - CodeForces Solution


binary search bitmasks data structures greedy *1300

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
  n, w = map(int,input().split())
  
  l_input = list(map(int,input().split()))
  l = sorted(l_input, reverse=True)
  
  d = {}
  for e in l:
    if e not in d:
      d[e] = 1
    else:
      d[e] += 1
  
  w_copy = w
  count = 0
  while n > 0:
    for e in d:
      while e <= w_copy and d[e] > 0:
        w_copy -= e
        d[e] -= 1
        n -= 1
    count += 1
    w_copy = w
  print(count)
    	 	 												 		 	  	

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll          long long
#define lli         long long int
#define vl          vector<ll>
#define vi          vector<int>
#define vs          vector<string>
#define pi          pair<int,int>
#define pl          pair<ll,ll>
#define all(a)      a.begin(),a.end()
#define mem(a,x)    memset(a,x,sizeof(a))
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define f(a,b) for(int i=a;i<b;i++)
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n,w;cin>>n>>w;
		multiset<int>nums;
		for(int i=0;i<n;i++){
			int x;cin>>x;
			nums.insert(x);
		}
		ll ans=1,sl=w;
		while (!nums.empty())
		{
			auto it=nums.upper_bound(sl);
			if(it!=nums.begin())
			{
				it--;
                int val = *it;
                sl -= val;
                nums.erase(it);
			}else{
				sl=w;
				ans++;
			}
		}
		cout<<ans<<endl;
		
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game