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

  

Title:

    n, k, m, d respectively represent n candies, k people, the number of candies distributed to one person at a time cannot be greater than m, 
but no one person can receive more than d candies, how many candies do you give each person at a time ,
so that Arkady gets the most sugar

Chicken analysis:

    First, I want to directly divide the number of sugars, but after reading the note of Example 1, I found that the number of sugars cannot be 
directly divided into two. It should be divided on the basis that n sugars can be divided into several rounds. Find out that Arkady
can get the most. In other words, the number of rounds of 1->d sugar should be enumerated first.
In , the largest number of sugars should be found by dichotomizing, and finally the largest number of particles should be selected from all possibilities.

Personal code:

#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;
// get
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) {
		years=-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;
}

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325280455&siteId=291194637