Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
3
Indexable
Never
#include <iostream>
#include <set>
#include <unordered_map>
#include <vector>
#include <cstring>
using namespace std;


struct cmp{
	bool operator()(pair<int, int> a, pair<int, int> b) {
		if(a.second == b.second) return a.first > b.first;
		return a.second > b.second;
	}
};

unordered_map<int, int> mp;  
set<pair<int, int>, cmp> se;  

void init(int N, int mId[], int mTime[]) {
	mp.clear();
	se.clear();
	for(int i = 0; i < N; i++) {
		se.insert(make_pair(mId[i], mTime[i]));
		mp[mId[i]] = mTime[i];
	}
}

int add(int mId, int mTime) {
	if(mp.find(mId) != mp.end()) {
		int time_tmp = mp[mId];
		se.erase(make_pair(mId, time_tmp)); 
	}

	mp[mId] = mTime;
	se.insert(make_pair(mId, mTime)); 
	return mp.size();
}

int remove(int K) {
	for(int i = 0; i < K; i++) {
		auto it = se.begin(); 
		int key = it->first;
		se.erase(it);          
		mp.erase(key);        
	}
	return se.begin()->first;  
}

int produce(int M) {
	int ans = 9999999;
	auto minMachine = se.rbegin();
	//cout<<"Min time"<< " "<<minMachine->second<<endl;
	int lTime = 0, rTime = minMachine->second * M, mid;
	//cout<<"rTime"<<" "<<rTime<<endl;
	while(lTime <= rTime) {
		mid = lTime + (rTime - lTime) / 2;
		//cout<<"Mid"<<" "<<mid<<endl;
		int curTotal = 0;
		for(auto it = se.begin(); it != se.end(); it++) {
			curTotal += (mid / it->second);
		}
		if(curTotal == M) {
			lTime =1, rTime = 0;
			ans = mid;
			for(int j=1; j<minMachine->second; j++) {
				int tempMid = mid, tempCnt = 0;
				tempMid-=j;
				//cout<<tempMid<<endl;
				for(auto it = se.begin(); it != se.end(); it++) {
					tempCnt += (tempMid / it->second);
				}
				//cout<<tempCnt<<endl;
				if(tempCnt == M) {
					ans = tempMid;
				}
			}
		}
		else if(curTotal > M) rTime = mid;
		else if(curTotal < M) lTime = mid;
	}
	
	return ans;  
}
Leave a Comment