首页 > 其他分享 >Educational Codeforces Round 135 (Rated for Div. 2) - E. Red-Black Pepper

Educational Codeforces Round 135 (Rated for Div. 2) - E. Red-Black Pepper

时间:2022-09-28 20:37:36浏览次数:75  
标签:Educational Rated return bb int ll Pepper exgcd yy

exgcd

Problem - E - Codeforces

题意

给 \(n\;(n<=3*10^5)\) 个菜,每个菜可以加红辣椒或黑辣椒,分别可以获得 \(c[i],d[i]\) 分;

有 \(m\;(m<=3*10^5)\) 个商店,第 i 个商店包含 \(x_i,y_i\), 表示只打包卖红辣椒 \(a\) 个,黑辣椒 \(b\) 个,即必须有 \(x,y>=0\), 且 \(a*x+b*y=n\)

求若分别去这 \(m\) 个商店买辣椒,分别最多能获得多少分

思路

  1. 假设全部加黑辣椒,求出当前的答案 \(ans=\sum d[i]\) (不用管是否满足有解)
  2. 令 \(e[i]=c[i]-d[i]\), 从大到小排序,求出前缀和 \(s[i]\),若能加 k 个红辣椒,则就取前 k 个
  3. 用 exgcd 解出一组解 \(x_0,y_0\), 且 \(x_0\) 为最小非负整数解,此时若 \(y_0<0\) 则返回 -1
  4. 令 \(d=\gcd(a,b)\), 通解为 \(x=x_0+t*\frac bd\), 并找出满足 \(y>=0\) 的最大的 \(x_{mx}\)
  5. 由于 \(s[i]\) 是单峰的,三分即可
  6. 也可以找到峰,求峰两侧的 x 对应的最大值即可(详细见代码)

代码

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"

typedef long long ll;
typedef pair<int, int> PII;


const int N = 3e5 + 10;
int n;
ll c[N], s[N];
ll ans;
int idx;
ll exgcd(ll a, ll b, ll &x, ll &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll xx, yy;
	ll d = exgcd(b, a % b, xx, yy);
	x = yy, y = xx - a / b * yy;
	return d;
}

ll solve(ll a, ll b)
{
	ll x, y;
	ll d = exgcd(a, b, x, y);
	if (n % d)
		return -1;
	x *= n / d, y *= n / d;
	ll bb = b / d;
	x %= bb;
	if (x < 0)
		x += bb;
	y = (n - x * a) / b;
	if (y < 0)
		return -1;
	int l = x, r = (n - l * a) / (bb * a) * bb + l;
	if (l * a >= idx)
		return s[l*a] + ans;
	if (r * a <= idx)
		return s[r*a] + ans;
	int lim = (idx - l * a) / (bb * a) * bb + l;
	return max(s[lim * a], s[(lim + bb) * a]) + ans;
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int x, y;
		cin >> x >> y;
		c[i] = x - y;
		ans += y;
	}
	sort(c + 1, c + n + 1, greater<ll>());
	for (int i = 1; i <= n; i++)
	{	
		s[i] = s[i-1] + c[i];
		if (c[i] >= 0)
			idx = i;
	}
	int q;
	cin >> q;
	while(q--)
	{
		int a, b;
		cin >> a >> b;
		cout << solve(a, b) << endl;
	}
    return 0;
}

标签:Educational,Rated,return,bb,int,ll,Pepper,exgcd,yy
From: https://www.cnblogs.com/hzy717zsy/p/16739458.html

相关文章

  • Java使用ProtoBuffer3时报错: Cannot resolve method 'isStringEmpty' in 'GeneratedM
    错误描述我的机器是MacM1,项目中使用了ProtoBuffer3。使用protoc程序,根据proto文件生成了Java代码。在编译Java项目的时候,报错:cannotresolvemethod'isstringempty'in......
  • 【每日一句sdc】create_gnerated_clock
    用途:当clk信号穿过触发器时,dc会把其当成普通信号处理,若果仍想其作为时钟信号往下传播,则需要将其声明成generated_clock, 是generate_clock的场景??todo协议:create_genera......
  • Educational Codeforces Round 119 E
    E.ReplacetheNumbers开始想到了一个二分的做法好像是O(nlog)的后来才想了一下可以在两个数组之间反复横跳那我是不是像记忆化搜索一样记录一个路径即可我们记录f[i]......
  • Educational Codeforces Round 40 (Rated for Div. 2) 补题
    E.WaterTaps题意:每个水龙头有一个流量限制\(a_i\),温度\(t_i\),现在让你控制每个水龙头的出水量\(x_i\),使得最终的水温为\(T\),水温的公式为$\frac{\sum\limits_{i=1}^{......
  • Educational Codeforces Round 134 D
    D.MaximumAND可以很轻松通过^和&两个操作看出我们要求的两个序列每一位上的1加起来必须等于n才行多一个少一个都不行然后1加起来等于n0自然加起来也等于n0和1的数......
  • Educational Codeforces Round 122 E
    E.SpanningTreeQueries纯暴力做法t了我们考虑如何优化我们可以发现要是所有绝对值曲线单调性不变我们MST的答案是可以O(1)转移的res+=(x-prex)*(num1-num2)单调性改变......
  • Educational Codeforces Round 123 D
    D.CrossColoring很容易想到的就是分成几个块有几个就是k多少次幂但是显然暴力的做法是n2的我们考虑如何优化我们考虑对每一行这个x[i]能成立的条件是啥那就是y[i]......
  • Educational Codeforces Round 123 E
    E.ExpandthePath我们画出一个合法的一般性的来研究比如RDRDR我们可以将其任意一个往下往右延长但是这个图形获得的面积是不规则的但是我们知道合法的序列肯定是......
  • Educational Codeforces Round 134 (Rated for Div. 2) D Maximum AND
    MaximumAND贪心从高位开始,尽可能地让\(a\)中该位为\(0\)的和\(b\)中该位为\(1\)的配对在一起,换句话说,可以让\(a\)由小到大排序,\(b\)由大到小排序如果当......
  • Educational Codeforces Round 134 (Rated for Div. 2)
    DMaxiumAnd贪心,优先满足高位,每次判断是否能够满足这一位答案为1,如果能则更新答案,这样显然是优的EPrefixFunction模仿求前缀函数的算法,先预处理求出\(|S|\)的前缀函......