首页 > 其他分享 >【2023年10月多校联训B层联赛2】 珠子 &&【October 2023 Multi-School League B Tier 2】 Beads

【2023年10月多校联训B层联赛2】 珠子 &&【October 2023 Multi-School League B Tier 2】 Beads

时间:2024-02-15 16:00:31浏览次数:23  
标签:10 means int data ll 联训 maxn 2023 pointer

第一次用英语,见谅。

为什么用英语? ``` Dev 里懒得换输入法。 ```

\(\textbf{gxyzoj \#3358}\)

\(\textbf{Luogu U406794}\)

Description

F has \(n\) beads arranged in a sequence, each of which has a color, and a total of \(m\) colors, numbered \(1, 2, 3, \cdots,m\).

She wants to take out a sequence of beads, and for each color \(i\), requires that the number of beads taken out be between \([L_i, R_i]\).

Find how many schemes for taking out beads there are.

Train of Thought

For a data range of \(2 \times 10^5\), we can just use time complexity \(O(n)\) with a constant methods for solving.

Obviously, we need to select a legal interval, which requires the use of the pointer. And as you see, two-pointer.

\(L_i\) means when left pointer is \(i\), all data minimum, \(R_i\) means when right pointer is \(i\), all data maximum.

The answer is the number of all legal numbers between \(L\) and \(R\) (include \(L\) and \(R\)).

See code for details.

Code

//For a data range of 2e5, we can just use time complexity O(n) with a big constant methods for solving.
//Obviously, we need to select a legal interval, which requires the use of the pointer. And as you see, two-pointer.
//L[i] means when left pointer is i, all data minimum, R[i] means when right pointer is i, all data maximum.
//The answer is the number of all legal numbers between L and R(include L and R).
#include <bits/stdc++.h>
#define maxn 200005
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
int n, m, c[maxn], ll[maxn], rr[maxn];
int t[maxn];
int L[maxn], R[maxn];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> c[i];
	memset(t, 0, sizeof(t));
	for (int i = 1; i <= m; i++) cin >> ll[i] >> rr[i];
	ll[0] = rr[0] = INF; //init
	int l = 1, r = 1, cnt = 0;
	for (int i = 1; i <= m; i++) //remember how many "0" there is
		if (ll[i] == 0) cnt++;
	t[c[1]] = 1; //init
//	if (t[c[1]] >= ll[c[1]] && ll[c[1]]) cnt++; //if ll[c[1]] == 1
	if (ll[c[1]] == 1) cnt++;
	bool flag = 0;
	memset(L, 0x3f, sizeof(L)); //memset L MAX
	while (r <= n + 1 && l <= n) {
		if (cnt == m) L[l] = min(L[l], r);
		//if the number of "allow" = M, that means all of M are OK. choose a smaller value
		if (flag) L[l] = 0; //some of "M" are not allow, we can't choose it
		if ((cnt == m && l < r) || flag || r == n + 1) {
		//if all "M" are OK and l is smaller than r OR some of "M" aren't allow OR r is the max value
			t[c[l]]--; //coler l's number get less
			if (t[c[l]] == ll[c[l]] - 1) cnt--; //color r isn't allow
			if (t[c[l]] == rr[c[l]] && flag) { //color r is allow
				cnt++;
				flag = 0;
			}
			l++; //get new value
		} else {
			r++;
			t[c[r]]++;
			if (t[c[r]] == rr[c[r]] + 1) { //color r's number bigger than max value color r can be
				cnt--;
				flag = 1; //color r isn't allow
			} else if (t[c[r]] == ll[c[r]]) cnt++; //color r's number is allow
		}
	}
	memset(t, 0, sizeof(t));
	l = 1, r = 1, cnt = 0, flag = 0;
	for (int i = 1; i <= m; i++)
		if (ll[i] == 0) cnt++;
	t[c[1]]++;
	if (t[c[1]] >= ll[c[1]] && ll[c[1]]) cnt++;
	while (l <= n && r <= n + 1) { //similarly, it is easy to prove. no prove again
		if (flag) R[l] = 0;
		if (cnt == m - 1 && t[c[r]] - 1 == rr[c[r]]) R[l] = max(R[l], r - 1);
		if (r == n + 1 && cnt == m) R[l] = max(R[l], r - 1);
		if ((t[c[r]] - 1 == rr[c[r]] && cnt == m - 1 && l < r) || flag || r == n + 1) {
			t[c[l]]--;
			if (t[c[l]] == rr[c[l]] && flag) {
				flag = 0;
				cnt++;
			}
			if (t[c[l]] == ll[c[l]] - 1) cnt--;
			l++;
		} else {
			r++;
			t[c[r]]++;
			if (t[c[r]] == ll[c[r]]) cnt++;
			if (t[c[r]] == rr[c[r]] + 1) {
				cnt--;
				flag = 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
		if (L[i] <= R[i] && L[i] && R[i]) //it's allow when L less than or equal to R and L and R aren't 0
			ans += R[i] - L[i] + 1;
	cout << ans << '\n';
	return 0;
}

End.

标签:10,means,int,data,ll,联训,maxn,2023,pointer
From: https://www.cnblogs.com/cloud-evecyx/p/18016304

相关文章

  • Go语言的100个错误使用场景(40-47)|字符串&函数&方法
    目录前言5.字符串5.5无用的字符串转换(#40)5.6获取子字符串操作和内存泄漏(#41)6.函数和方法6.1不知道选择哪种类型的方法接受者(#42)6.2从来不使用命名的返回值(#43)6.3使用命名返回值造成的意外副作用(#44)6.4返回一个nil接受者(#45)6.5使用文件名作为函数的输入(#46)6.6不理解de......
  • 2023.2.15 LGJ Round
    A\(n\)个点,有\(m\)种操作\((w,l,r)\),代表贡献是\(w\),并消除\([l,r]\)的所有点。操作的条件是必须消除一个点,问最多的贡献是多少。\(n,m\le300\).考虑从小区间开始操作,不难联想到区间dp。\(dp_{i,j}\)代表\([l,r]\)都被消除的最大贡献。对于\(dp_{i,j}\),我们枚举......
  • Go 100 mistakes - #21: Inefficient slice initialization
          ConvertingoneslicetypeintoanotherisafrequentoperationforGodevelopers.As wehaveseen,ifthelengthofthefuturesliceisalreadyknown,thereisnogoodreasontoallocateanemptyslicefirst.Ouroptionsaretoallocat......
  • P10111 [GESP202312 七级] 纸牌游戏
    原题链接思路1.任意一轮出牌,只有三种选择2.每一轮的得分只与当前一轮出的牌和上一轮出的牌相关由此我们可以设\(dp[i][j]\)为第\(i\)轮,出牌\(j\)的得分3.由于扣分机制,扣的分数与扣的次数有关,所以我们再加一层\(dp\)代表扣的次数code,注意细节#include<bits/stdc++.......
  • PAT乙级-1009(说反话)
    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。......
  • Win10任务栏图标居中
    win+q键搜索并打开字符映射表点击第五行的空白字符,然后先后点击下方的选择以及复制在桌面新建一个文件夹,然后重命名,将刚才复制的空白字符粘贴进去,如图,这样我们就拥有了一个空白名称的文件夹在任务栏右键→工具栏→新建工具栏→在弹出的对话框中选择刚才新建的空白文件夹,接......
  • 2023北京集训 做题笔记
    2023北京集训做题笔记动态规划CF1610HSquidGame钦定\(1\)为根,发现如果\(u,v\)不为祖先后代关系,则选择根能把它们全部消掉剩下的\(u,v\)均为祖先后代,假设\(u\)为祖先如果在一条链上,问题转化为选择尽量少的点覆盖所有线段,与端点重合不算常规做法是按右端点排序,每次......
  • NOIP2023 集训做题笔记
    杂项CF1181E2AStoryofOneCountry(Hard)启发式分裂发现如果当前矩形中有一整行或一整列没有穿过城堡内部,就可以分为\(2\)部分而且分开后相当于限制减少,每次贪心的能分就分,朴素实现复杂度为\(O(n^2\logn)\),可通过easyversion考虑优化每次找分割点的过程如果分割点......
  • P1012 [NOIP1998 提高组] 拼数
    [NOIP1998提高组]拼数题目描述设有\(n\)个正整数\(a_1\dotsa_n\),将它们联接成一排,相邻数字首尾相接,组成一个最大的整数。输入格式第一行有一个整数,表示数字个数\(n\)。第二行有\(n\)个整数,表示给出的\(n\)个整数\(a_i\)。输出格式一个正整数,表示最大的整数样......
  • P1068 [NOIP2009 普及组] 分数线划定
    [NOIP2009普及组]分数线划定题目描述世博会志愿者的选拔工作正在A市如火如荼的进行。为了选拔最合适的人才,A市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试。面试分数线根据计划录取人数的\(150\%\)划定,即如果计划录取\(m\)名志愿者,则面试分数......