首页 > 其他分享 >Codeforces:B. New Year and Ascent Sequence[模拟]

Codeforces:B. New Year and Ascent Sequence[模拟]

时间:2022-10-09 21:44:42浏览次数:54  
标签:数列 Sequence Year sequence concatenation Codeforces si sequences 升序

题面

A sequence a=[a1,a2,…,al] of length l has an ascent if there exists a pair of indices (i,j) such that 1≤i<j≤l and ai<aj. For example, the sequence [0,2,0,2,0] has an ascent because of the pair (1,4), but the sequence [4,3,3,3,1] doesn't have an ascent.
Let's call a concatenation of sequences p and q the sequence that is obtained by writing down sequences p and q one right after another without changing the order. For example, the concatenation of the [0,2,0,2,0] and [4,3,3,3,1] is the sequence [0,2,0,2,0,4,3,3,3,1]. The concatenation of sequences p and q is denoted as p+q.
Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has n sequences s1,s2,…,sn which may have different lengths.
Gyeonggeun will consider all n2 pairs of sequences sx and sy (1≤x,y≤n), and will check if its concatenation sx+sy has an ascent. Note that he may select the same sequence twice, and the order of selection matters.
Please count the number of pairs (x,y) of sequences s1,s2,…,sn whose concatenation sx+sy contains an ascent.
Input
The first line contains the number n (1≤n≤100000) denoting the number of sequences.
The next n lines contain the number li (1≤li) denoting the length of si, followed by li integers si,1,si,2,…,si,li (0≤si,j≤106) denoting the sequence si.
It is guaranteed that the sum of all li does not exceed 100000.
Output
Print a single integer, the number of pairs of sequences whose concatenation has an ascent.

题意

给定n个数列,统计其中任意两个数列组成的长数列中满足其中存在升序子列的长数列个数。

分析

答案统计分两种
第一种是某个数列自身满足存在升序子列,则与其搭配的任意数列都满足条件
第二种是两个数列都不存在升序子列,但是组合后存在
其中存在的条件是第一个数列的最小值小于第二个数列的最大值(即有升序子列)

合起来,对于某一个数列,只需统计其他数列中最小值小于该数列最大值的数列的个数即可

代码

#include <iostream>
#include <cstdio>
using namespace std;
inline int read() {
	register int x = 0, f = 1;
	register char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0',c = getchar();
	return x * f;
}
const int MAXN = 1e6 + 1;
int n, l;
int maxs[MAXN], pre[MAXN], mins;
bool flag;
void init() {
	n = read();
	for (int i(1); i <= n; i++){
		l = read();
		mins = MAXN;
		flag = false;
		for (int j(1); j <= l; j++){
			int now = read();
			if (now > mins) flag = true;//如果满足自身存在升序子列,打标记
			maxs[i] = max(maxs[i], now);
			mins = min(mins, now);
		}
		if (flag){
			maxs[i] = 1e6;//
			mins = -1;
		}
		pre[mins + 1]++;//pre[i]记录小于i的数列最小值的个数
	}
	for (int i(1); i <= 1e6; i++){
		pre[i] += pre[i - 1];//记录前缀和
	}
}

void doit() {
	long long ans = 0;
	for (int i(1); i <= n; i++)
		ans += pre[maxs[i]];//统计最小值小于该数列最大值的数列个数
	printf("%lld\n", ans);
}

int main() {
	init();
	doit();
	return 0;
}

标签:数列,Sequence,Year,sequence,concatenation,Codeforces,si,sequences,升序
From: https://www.cnblogs.com/cancers/p/16773800.html

相关文章

  • Codeforces Round #800 div1 B
    B.FakePlasticTrees看了第三个样例发现一个节点可以由他的几个子节点一起构成我们首先自下而上的看肯定叶子节点越大越好这样我们选择的空间就会越多再者要是我们......
  • 「题解」Codeforces 1572B Xor of 3
    我知道你很急,但你先别急。我急了我急了我急了。如果直接上去莽构造的话,很可能就是像我一样大分讨的后果。先特判掉全是1,或者1的个数是奇数的情况。我的做法是考虑所......
  • Codeforces Round #822 (Div. 2)
    A题意给一个长为n的数组,每次可以对其中某个数做+1或-1的操作。求最小的操作次数,使得可以从数组中选出三个相同的数。思路很容易可以想到选三个最接近的数然后操作。也......
  • 【题解】sequence
    题意定义一个满足下列两个条件之一的序列\(a\)为单谷序列:\(\exists1\leqk\leqn\),有\(a_1<...<a_k>a_{k+1}>...>a_n\)\(\exists1\leqk\leq......
  • Subsequence Path(图论,DP)
    题意给定\(N\)个点,\(M\)条边的无向图,边权为\(C_i\)。给定一个序列\(E=(E_1,E_2,\dots,E_K)\),其中\(E_i\)表示边的编号。路径是“好路径”当且仅当边的编号按照经过......
  • Codeforces.1305B Kuroni and Simple Strings[模拟]
    题面NowthatKuronihasreached10yearsold,heisabigboyanddoesn'tlikearraysofintegersaspresentsanymore.ThisyearhewantsaBracketsequencea......
  • Codeforces补入门题
    CodeforcesRound#620(Div.2)LongestPalindrome题意给定n个长度为m的字符串,使用其中尽可能多的字符串构成一个回文串输出回文串长度及该回文串(顺序可以乱)分析由于......
  • [题解] Codeforces Dytechlab Cup 2022 1737 A B C D E 题解
    傻*Dytechlab还我rating!(不过目前rating还没加上去,据说E是偷的说不定要unrated)实在没预料到会打成这样。。。求点赞点我看题A.ElaSortingBooks从前往后一位一位确......
  • [CodeForces-1198E] Rectangle Painting 2
    题解:朴素做法,是最小点覆盖点数是n*n,考虑离散化后,把每个矩形块看作点,跑最小点权覆盖。将矩形:左下角(x1,y1)到右上角(x2,y2)的x2++,y2++,那么这样离散化后每个x1<=x<x2,y1......
  • Codeforces Round #570 (Div. 3) C. Computer Game(二分)
    https://codeforces.com/contest/1183/problem/C题目大意:给定k的总时间,必须玩n局,每一局正常消耗a时间,插电玩消耗b时间问我们在能>0的剩余时间内玩完这n局下的可以最大......