codeforces 670D2 - Magic Powder - 2

和前面的那道题一样,就只改了数组的大小和数据类型。

#include <bits/stdc++.h>

using namespace std;
const int MAX = 1e5 + 5;
//int a[MAX], b[MAX], c[MAX];
typedef __int64 ll;
struct NODE
{
	ll a, b, c;
}node[MAX];
ll sum[MAX];
bool comp(NODE a, NODE b)
{
	return a.c < b.c;
}

const int INF = 0x3f3f3f;
int main()
{
	ll n, k;
	scanf("%I64d%I64d", &n, &k);
	int tot = 0;
	for (int i = 0; i < n; ++i)
	{
		scanf("%I64d", &node[i].a);
	//	tot += node[i].a;
	}
	for (int i = 0; i < n; ++i)
	{
		scanf("%I64d", &node[i].b);
		//k += node[i].b;
	}
	//int smallest = INF;
	for (int i = 0; i < n; ++i)
	{
		node[i].c = node[i].b / node[i].a;
		//if (smallest > c[i])
		//	smallest = c[i];
	}
	sort(node, node + n, comp);
	sum[0] = node[0].a;
	for (int i = 1; i < n; ++i)
	{
		sum[i] = sum[i - 1] + node[i].a;
	}
	//for (int i = 0; i < n; ++i)
	//	cout << node[i].c << endl;
	ll i = 0; 
	ll res = node[0].c;
	while (i <= n - 1 && k > 0)
	{	
		//cout << k << endl;
		k += (node[i].b % node[i].a);
		//cout << "ss" << k << endl;
		if (k >= (sum[i]* (node[i + 1].c - node[i].c)) && node[i + 1].c != 0 && node[i + 1].c > node[i].c)
			{
				k -= (sum[i] * (node[i + 1].c - node[i].c)); 
				res += node[i + 1].c - node[i].c;
				//cout << "k = " << k << endl;
			}
		else if (k < (sum[i]* (node[i + 1].c - node[i].c)) && node[i + 1].c != 0 && node[i + 1].c > node[i].c)
		{
			//if (node[i + 1].c > node[i].c)
			//{
				res += k / (sum[i]);
			break;
			//}
		}
		else if (node[i + 1].a == 0)
		{
			res += k / (sum[i]);
			//cout << "here" << endl;
			break;
		}
		//else
		//	break;
		i++;
		//cout << "res" << res << endl;
	}
	cout << res << endl;
	return 0;
}


你可能感兴趣的:(贪心)