1701C - Schedule Management - CodeForces Solution


binary search greedy implementation two pointers *1400

Please click on ads to support us..

Python Code:

t=int(input())

for _ in range(t):
    n,m =[int(i) for i in input().split()]
    a=[int(i) for i in input().split()]
    worker=[0]*n
    for x in a:
        worker[x-1]+=1
    
    high=max(worker)
    low=0
    
    while low<=high:
        mid=low+(high-low)//2
        tasks=0
        for x in worker:
            if x>=mid:
                tasks+=mid
            else:
                tasks+=x+(mid-x)//2
        
        if tasks>=m:
            high=mid-1
        else:
            low=mid+1
    
    print(low)
        

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define max1 300021
#define gcd __gcd
const ll mod=1e9+7;
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
const ll INF = 0x3f3f3f3f;
#define nx x+fx[i][0]
#define ny y+fx[i][1]
#define pb push_back
#define pii pair<int,int>
const ll N=3e5+10;
ll m,ans,k,d,t,c,cnt,n,l,r,i,j;	
string strr,str;
ll len=0,gs=0,min_=INF,sum=0;
int max_;
ll fx[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int t1[max1];
inline ll lowbit(ll x){return x&(-x);}
inline ll ls(ll x){return x<<1;}
inline ll rs(ll x){return x<<1|1;}
void update(ll x,ll d){while(x<=n){t1[x]+=d;x+=lowbit(x);}}
inline ll qsum(ll x){ll ans=0;while(x>0){ans+=t1[x];x-=lowbit(x);}return ans;}
struct edge{
       int u,v,w,nex;
}e[max1];
struct node{
	int u,val;
};
ll head[max1],a[max1],s[max1],in[max1],dp[205][205];
int st,ed;
void add(int x,int y,int z)
{
    e[++cnt].u=x;
    e[cnt].v=y;
    e[cnt].w=z;
    e[cnt].nex=head[x];
    head[x]=cnt;
}
inline void solve() {
       cin>>n>>m;ans=1;
       fill(a+1,a+n+1,0);
       for(int i=1;i<=m;i++) cin>>c,a[c]++;
//       sort(s+1,s+n+1);
       int l=1,r=m*2;
 //      cout<<r<<"\n";
  auto check = [&](int t) {
    int64_t r = m;
    for (int i = 1;i <= n;i++) {
      if (a[i] < t) r -= a[i] + (t - a[i]) / 2;
      else r -= t;
    }
    return r <= 0;
  }; 
       while(l<r){
	   	int mid=(l+r)/2;
	   	if(check(mid)) r=mid;
	   	else l=mid+1;
//	   	cout<<r<<endl;
	   }
	   cout<<l<<"\n";
}  
int main() {
     cin.tie(nullptr) -> sync_with_stdio(false); 
	 cout.tie(0);cout.setf(ios::fixed);

   	 int __=1;
    cin>>__;	  
    while(__--)
	{
	  solve();
	} 	
      return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST