Codeforces Round #476 (Div. 2) C - Greedy Arkady

Examples 1:
input:
20 4 5 2
output:
8

Examples 2:
input:
30 9 4 1
output:
4

  

题意:

    n,k,m,d分别表示n颗糖,k个人,一次分给一个人的糖果数不能大于m,
但是不能有一个人收到糖果的次数大于d,问你一次给每人发几颗糖,才能
让Arkady得到的糖最多

菜鸡分析:

    第一我是想直接二分糖的数目,可是看了样例一的note,发现不能直
接二分糖的数目,应该在n颗糖可以分几轮的基础上进行二分,找出Arkady
最多可以得到的糖,也就是说应该先枚举1->d分糖的轮数,每一种情况在
进行二分找最大,最后再从所有的可能性中选取最大的颗数

个人代码:

#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define pb push_back
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>PLL;
typedef pair<int,ll>Pil;
const ll INF = 0x3f3f3f3f;
const double inf=1e8+100;
const double eps=1e-8;
const int maxn =5e5;
const int N = 510;
const ll mod=1e9+7;
const ll MOD=1e9;
//------
//define
ll n,k,m,d,ans=-1,tmp=0;
//geti
ll geti(ll i) {
	ll eh=n/i;
	ll circle=(eh-1)/k+1;
	return circle;
}
//solve
void solve() {
	while(cin>>n>>k>>m>>d) {
		ans=-1;
		for(int i=1; i<=d; i++) {
			ll l=1,r=m;
			tmp=0;
			while(l<=r) {
				ll m=l+(r-l)/2;
				ll tt=geti(m);
				if(tt==i) {
					tmp=m*i;
					l=m+1;
				} else if(tt>i) {
					l=m+1;
				} else {
					r=m-1;
				}
			}
			ans=max(ans,tmp);
		}
		cout<<ans<<endl;
	}
}
//main
int main() {
	ios_base::sync_with_stdio(false);
#ifdef debug
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	cin.tie(0);
	cout.tie(0);
	solve();
	/*
		#ifdef debug
			fclose(stdin);
			fclose(stdout);
			system("out.txt");
		#endif
	*/
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/visualVK/p/8992587.html