首页 > 其他分享 >暑假牛客多校第五场 2023-7-31(G、D、H)

暑假牛客多校第五场 2023-7-31(G、D、H)

时间:2023-08-02 19:56:52浏览次数:42  
标签:typedef int 第五场 31 多校 long cnt pair include

未补完


G. Go to Play Maimai DX


算法:双指针

做法:从左到右用两个指针维护一段区间且右指针不断右移,当这个区间满足题目所给的性质,我们取出区间长度,然后再将左指针右移,直到右指针到边界且左指针指到不符合题目的性质的位置结束,期间不断对符合题目性质的区间长度取最小值。

code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <cmath>
#include <string>
#include <set>
#define x first
#define y second
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

const int N = 100010;

int n, k;
int w[N];

bool check(int cnt[])
{
	if (cnt[1] >= 1 && cnt[2] >= 1 && cnt[3] >= 1 && cnt[4] >= k)return true;
	return false;
}

void solve()
{
	cin >> n >> k;
	int cnt[5] = { 0 };
	int ans = 1e9;
	for (int i = 1; i <= n; i++)cin >> w[i];
	for (int i = 1, j = 1; j <= n; )
	{
		while (!check(cnt) && j <= n)cnt[w[j]]++, j++;
		while (check(cnt))ans = min(ans, j - i), cnt[w[i]]--, i++;
	}

	cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t = 1;
	while (t--)
	{
		solve();
	}

	return 0;
}

 

D. Cirno's Perfect Equation Class


算法:试除法求约数, gcd

做法:对于 \(k * a + b = c\),我们可以得到 \(k * a + c / x = c\),那么 \(c / x\) 必须为整数,即需要整除。那么我们求出 \(c\) 的约数,随后再用 \(c\) 除以约数可得 \(b\),再判断 \((c - b) / k\) 是否为整数,是则得到 \(a\),否则这个 \(b\) 不存在。最后我们判断一下 \(a\) 和 \(b\) 是否都大于等于1且 \(gcd(a, b) >= n\) 即可。

code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <cmath>
#include <string>
#include <set>
#define x first
#define y second
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

const int N = 200010, INF = 1e9;

int k, n, a, b, c;

vector<int> gt(int c)
{
	vector<int> ans;
	for (int i = 1; i <= c / i; i++)
	{
		if (c % i == 0)
		{
			ans.push_back(i);
			if (c / i != i)ans.push_back(c / i);
		}
	}
	return ans;
}

int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}

void solve()
{
	cin >> k >> c >> n;
	auto num = gt(c);

	int ans = 0;
	for (int i = 0; i < num.size(); i++)
	{
		b = c / num[i];
		if((c - b) % k == 0)
		{
			a = (c - b) / k;
			if (gcd(a, b) >= n && a >= 1 && b >= 1)ans++;
		}
	}

	cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}

 

H. Nazrin the Greeeeeedy Mouse


算法:dp

做法:我们先设有 \(f_{i, j, k}\),表示在第 \(i\) 到第 \(j\)个蛋糕中我们有一个大小为 \(k\) 的包对这 \(i - j + 1\) 个蛋糕的获取或打洞的集合。这就有点像 01 背包问题了。我们有如下方程: $$f_{i, j, k} = max(f_{i, j - 1, k} \ , f_{i, j - 1, k - a[j]} + b[j])$$ 即第 \(j\) 个蛋糕在从第 \(i\) 到第 \(j\)个蛋糕中且大小为 \(k\) 的包中到底取不取。

我们再取前缀有 \(f_{i, j, k} = max(f_{i, j, k - 1} \ , f_{i, j, k})\)。

我们再设 \(g_{i, j}\),表示第 \(i\) 个包已经取到了第 \(j\) 个蛋糕的集合。
那么我们有如下方程:$$g_{i, j} = max(g_{i - 1, j} \ , g_{i - 1, k} + f_{k + 1, j, a[i]})$$ 其中 \(k \in [0, j - 1]\)

code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <cmath>
#include <string>
#include <set>
#define x first
#define y second
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

const int N = 200, INF = 1e9, M = 100010;

int n, m;
int a[N], b[N];
int f[N + 5][N + 5][N + 5], g[N + 5][N + 5], bag[M], used[N];

void solve()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> a[i] >> b[i];

	for (int i = 1; i <= n; i++)
	{
		for (int j = i; j <= n; j++)
		{
			for (int w = 0; w <= N; w++)
			{
				if (w >= a[j])f[i][j][w] = max(f[i][j - 1][w], f[i][j - 1][w - a[j]] + b[j]);
				else f[i][j][w] = f[i][j - 1][w];
			}

			for (int w = 1; w <= N; w++)
				f[i][j][w] = max(f[i][j][w], f[i][j][w - 1]);
		}
	}

	int cnt = 0, ans = 0;
	for (int i = 1; i <= m; i++)cin >> bag[i];
	for (int i = max(1, m - n); i <= m; i++)used[++cnt] = bag[i];
	for (int i = 1; i <= cnt; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			for (int k = 0; k < j; k++)
				g[i][j] = max(g[i][j], g[i - 1][k] + f[k + 1][j][used[i]]);

			ans = max(ans, g[i][j]);
		}
	}

	cout << ans << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	solve();
	return 0;
}

标签:typedef,int,第五场,31,多校,long,cnt,pair,include
From: https://www.cnblogs.com/dkdklcx/p/17595103.html

相关文章

  • 逆向——字符与字符串,中文字符GB2312编码由来
    字符与字符串在之前的课程中我们了解到变量的定义决定两个事情,第一是决定存储的数据宽度,第二是决定了存储的数据格式,那么我们来看下下面的代码:inta=123;//变量x,数据宽度为4个字节,里面存储的是补码(在计算机系统中,数值一律用补码来存储)intfloatb=123.4F;//IEEE编码(浮点)......
  • 2023牛客暑期多校训练营5
    之前落下的每一场比赛都是要补回来的。。。GGotoPlayMaimaiDX题解的想法比较简单,由于找到满足1,2,3出现至少一次,4出现至少k次的最短区间,所以可以双指针,双指针用于这种长度最短,长度越长越容易满足条件的题就很恰当。我没想到双指针,就写的比较麻烦,预处理每个数后一个1,2,3的位置......
  • 2023年多校联训NOIP层测试2
    2023年多校联训NOIP层测试2爆零了T1HDU4786FibonacciTree\(0pts\)@wangyunbiao:不可以,总司令我:不,可以,总司令T2期末考试\(0pts\)T3麻烦的工作\(0pts\)@wangyunbiao:不可以,总司令我:不,可以,总司令T4小X的Galgame\(0pts\)......
  • 【题解】HDOJ 7329 [2023杭电多校] Touhou Red Red Blue
    题目传送门:HDOJ7329[2023杭电多校]TouhouRedRedBlue题意有两个口袋(口袋容量为1,初始均为空),有若干个UFO按顺序来到你的面前,每个UFO有一个颜色(GRB),你可以选择扔掉它或者把它装进口袋中,如果口袋1空着必须装进口袋1;如果口袋都满了,算上面前的UFO共有三个,有如下三种情况:如......
  • 牛客暑假多校 2023 第五场
    目录写在前面GDHCEI写在最后写在前面战犯在此。我宣布二周年这首是神。以下按个人向难度排序。G尺取法,右端点单调右移过程中右移调整左端点,使区间内每种数都至少出现1次,4出现至少\(k\)次即可。///*By:Luckyblock*/#include<cmath>#include<cstdio>#include<......
  • ABC311E 题解
    看到官方题解是\(O(n^2)\)的dp。提供一个\(O(n^2\log_2n)\)的做法,考场思路,大概比较简单。Description给一个\(H\)行\(W\)列的网格,其中一些点被涂成黑色,求整个正方形内都未被涂黑的正方形的个数。Solution考场上首先想到的简单暴力做法,即枚举正方形左上角端点,然......
  • 不忘初心 Windows11 22H2 22621.2070 x64 无更新 精简 游戏 2023.07.31 集成最新版任
    注意此版不能更新补丁,而且非纯净版,此版为游戏版,为游戏稳定而生也可以用于办公,保留Hyper和linux,体积和稳定性介于可更新版和无更新版之间,集成任务栏透明软件,独家4K全新高清壁纸,增加右键一些功能,以及离线集成了运行库,绝对给你带来不一样的视觉体验,不一样的美!为了保证稳定初心的系统......
  • HDU7331 另解
    \[\begin{aligned}ANS&=\sum_{i=1}^n\binom{n}{i}p^i(1-p)^{n-i}\left(\sum_{j=1}^ij^m\right)&p=\frac{a}{b}\\&=\sum_{j=1}^nj^m\sum_{i=j}^n\binom{n}{i}p^i(1-p)^{n-i}\\&=\sum_{j=1}^n\sum_{k=0}^m{m\bracek}\binom{j}{k}k!\su......
  • 7-31打卡
    矩阵快速幂求斐波那契数列快速幂将指数n表示成二进制形式。从二进制的最低位开始遍历,如果当前位为1,则累乘底数x;否则,不进行任何操作。将底数x不断平方,并更新指数n为n的一半。重复步骤2和步骤3,直到遍历完整个二进制表示。publicclassFibonacciMatrix{publicstaticvo......
  • css的inline-block布局方式对齐问题 —— 转载自 article/2023/7/31 16:26:21
    css的inline-block布局方式对齐问题今天在实现百度前端技术学院的如下案例时遇到了div上下对齐问题。针对如下左右两栏布局,本来使用将两栏各自div的display设置为inline-block方式来实现,为了左边高度与右边对齐,直接量出右边div按照像素高度赋给左边。但是左边元素竟然出现在了......