首页 > 其他分享 >Codeforces Round #804 (Div. 2) C D

Codeforces Round #804 (Div. 2) C D

时间:2022-11-23 21:22:57浏览次数:61  
标签:int Codeforces long cc 804 sc Div include define

C 1700
D 2300
所以 我并没有做水题。

考虑0的位置 一定不动 再考虑1的位置 也不动

考虑2的位置 不妨设0的位置为L 1的位置为R 那么若2的位置在L~R之间那么2就可以随便放了。

若不在则2的位置唯一再更新LR即可 之后不断的继续放就行了。

code
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<queue>
#include<deque>
#include<stack>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 2000000000
#define inf 100000000000000000ll
#define ldb long double
#define pb push_back
#define put_(x) printf("%d ",x);
#define get(x) x=read()
#define putl(x) printf("%lld\n",x)
#define rep(p,n,i) for(int i=p;i<=n;++i)
#define go(x) for(int i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define pii pair<int,int>
#define mk make_pair
#define P 1000000007ll
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define uint unsigned long long
#define ui unsigned
#define sq sqrt
#define y(w) t[w].y
#define x(w) t[w].x
#define z(w) t[w].z
#define id(cc) s[cc].id
#define w(cc) s[cc].w
#define S second
#define mod 1000000007
#define sc(A) scanf("%d",&A)
#define scs(A) scanf("%s",A);
#define put(A) printf("%d\n",A)
#define min(x,y) (x>=y?y:x)
#define max(x,y) (x>=y?x:y)
using namespace std;
const int MAXN=100010;
int T;
int n;
int a[MAXN],b[MAXN];
int main()
{
	//freopen("1.in","r",stdin);
	sc(T);
	while(T--)
	{
		sc(n);
		rep(1,n,i)
		{
			sc(a[i]);
			b[a[i]]=i;
		}
		int L=b[0],R=b[1];
		if(L>R)swap(L,R);
		int ans=1;
		rep(2,n-1,i)
		{
			if(b[i]<L)
			{
				L=b[i];continue;
			}
			if(b[i]>R)
			{
				R=b[i];continue;
			}
			ans=(ll)ans*(R-L+1-i)%mod;
		}
		put(ans);
	}
}

注意到n只有5000 直接贪心非常难做。考虑dp 容易想到这个序列可以dp出来。

设f[i]表示到达i且i必选的最大值。i之后合法不合法不知道但i之前的那段序列必合法。

这样 枚举了j 那么i-j-1这个区间一定为偶数 a[j]==a[i] 什么时候这个区间完全消完?

最大值小于等于区间一半 递归的可以发现成立。

注意如果\(f_i\)为答案注意检查i+1~n这一段是否合法。

code
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<queue>
#include<deque>
#include<stack>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 2000000000
#define inf 100000000000000000ll
#define ldb long double
#define pb push_back
#define put_(x) printf("%d ",x);
#define get(x) x=read()
#define putl(x) printf("%lld\n",x)
#define rep(p,n,i) for(int i=p;i<=n;++i)
#define go(x) for(int i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define pii pair<int,int>
#define mk make_pair
#define P 1000000007ll
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define uint unsigned long long
#define ui unsigned
#define sq sqrt
#define y(w) t[w].y
#define x(w) t[w].x
#define z(w) t[w].z
#define id(cc) s[cc].id
#define w(cc) s[cc].w
#define S second
#define mod 1000000007
#define sc(A) scanf("%d",&A)
#define scs(A) scanf("%s",A);
#define put(A) printf("%d\n",A)
#define min(x,y) (x>=y?y:x)
#define max(x,y) (x>=y?x:y)
using namespace std;
const int MAXN=5010;
int T;
int n;
int a[MAXN],b[MAXN];
int f[MAXN];
int main()
{
	freopen("1.in","r",stdin);
	sc(T);
	while(T--)
	{
		sc(n);
		rep(1,n,i)sc(a[i]);
		f[0]=0;
		rep(1,n,i)
		{
			f[i]=-1;int mx=0;
			for(int j=i-1;j>=0;--j)
			{
				if(!((i-j-1)&1)&&(a[j]==a[i]||j==0)&&mx<=(i-j-1)/2)
				{
					if(f[j]!=-1)f[i]=max(f[i],f[j]+1);
				}
				++b[a[j]];
				mx=max(mx,b[a[j]]);
			}
			for(int j=i-1;j>=0;--j)--b[a[j]];
		}
		int ans=f[n]==-1?0:f[n],mx=0;
		for(int i=n;i>=2;--i)
		{
			++b[a[i]];
			mx=max(mx,b[a[i]]);
			if(!((n-i+1)&1)&&mx<=(n-i+1)/2)ans=max(ans,f[i-1]);
		}
		rep(2,n,i)--b[a[i]];
		put(ans);
	}
	return 0;
}

标签:int,Codeforces,long,cc,804,sc,Div,include,define
From: https://www.cnblogs.com/chdy/p/16920153.html

相关文章

  • Public NOIP Round #3(Div. 1) 题解
    T2:先判\(1,n\)有连边的情况,也就是说明最短路一定是\(1\)直接走到\(n\)。特判掉\(k=1,n=2\)的情况,这是无解的。那么如果\(k\ge2\)就令\(1,n\)都为\(U\),其余随......
  • Codeforces Round #835 (Div. 4) A-G完全题解
    比赛链接A、点击查看代码#include<bits/stdc++.h>usingnamespacestd;intmain(){intT;cin>>T;while(T--){vector<int>G;for(inti=1;......
  • Codeforces Round #831 (Div. 1 + Div. 2)
    C核心思路:这个题目其实是一个规律题,它有一个很重要的前提,那就是在分配方案最优的情况下,要不然我们直接选几个差值最大的就ok,那他这句话,其实我们结合几个样例就知道,有一个......
  • Codeforces Round #790 (Div.4) A-H2题解
    原题链接:https://codeforces.com/contest/1676A.Lucky?题意:给定长度为6由数字组成的字符串问前三个数字的和是否等后三个数字的和。题解:直接相加比较即可。#include<......
  • Codeforces Round #835 (Div.4) A-G题解
    原题链接:https://codeforces.com/contest/1744A.MediumNumber题意:给定三个数,求中间那个数(倒数第二小or倒数第二大)。题解:直接用数组存,sort一下输出中间的数即可。#in......
  • Educational Codeforces Round 36 (Rated for Div. 2) E Physical Education Lessons
    PhysicalEducationLessons珂朵莉树模板#include<iostream>#include<cstdio>#include<set>usingnamespacestd;#defineItset<ran>::iteratorstructran{......
  • Codeforces Round #449 (Div. 1) C Willem, Chtholly and Seniorious
    Willem,ChthollyandSeniorious珂朵莉树慕名而来操作\(3\)直接排序是我没想到的,因为随机数据所以才能过吧\(split\)操作中忘了开\(longlong\),\(wa3\)#include<......
  • Codeforces Global Round 23 D
    D.PathsontheTree思考问题我们发现我们路径总是可以走到底的而不会中途中断而且对于每一个分叉点也就是每个儿子至少都会有当前还剩的k/儿子数取余剩下的我们可以......
  • Codeforces Round #835 (Div4)
    A.MediumNumber题意:输入三个不同的数字,输出中位数思路:没啥说的,比较一下大小即可代码:<bits/stdc++.h>usingnamespacestd;intmain(){int_;cin>>......
  • CodeForces - 320E Kalila and Dimna in the Logging Industry
    题意:你有要拿一把锯子砍树。锯子有有电和没电两个状态,只有在有电的时候才能工作,每次工作都可以砍1单位高度的树,然后就会没电。没电后要充电才能工作。充电有代价,代价为,当前......