Educational Codeforces Round 69 (Rated for Div. 2)

A水题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)scanf("%d",&a[i]);
		sort(a,a+n);
		int h = min(a[n-1],a[n-2]);
		if(h<2)
		{
			printf("0\n");
			continue;
		}
		else printf("%d\n",min(h-1,n-2));
	}
}

B题 思维题
观察cf的给的两个样例,他只能向临近的转移,第一个样例把大的放中间,小的始终有地方放置,而第二样例刚好相反,大胆猜测一波然后证明,发现这个需要先单增后单间(严格),那么就能转移。

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
	char ch = getchar(); int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
int n;
const int N = 2e5 + 10;
int a[N];
int maxn = 0;
int main()
{
	n = read();
	int pos = 0;
	up(i, 0, n)
	{
		a[i] = read();
		if (a[i] > maxn)
		{
			maxn = a[i]; pos = i;
		}
	}
	upd(i, 1, pos)
	{
		if (a[i] < a[i - 1])
		{
			cout << "NO" << endl;
			return 0;
		}
	}
	up(i, pos,n-1 )
	{
		if (a[i] < a[i + 1])
		{
			cout << "NO" << endl;
			return 0;
		}
	}
	
	cout << "YES" << endl;
	return 0;
}

C题 差分数组
题目的解为max-min,又因为题目的序列是单调递增的,那么这个值就等于差分数组的和。
所以所有区间的和我们都可以用差分数组求解。k个区间和,我们需要把区间切成k-1个,差分数组的每一个项对应了相邻两个数的差值,如果不要,就可以看成切了一刀,因为我们统计sum值是差分的前缀和,所以我们排序并且不要最后面的k-1个,即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+10;
int a[maxn],b[maxn];
int cmp(int x,int y)
{
	return x>y;
}
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++)scanf("%d",&a[i]);
	for(int i=1;i<n;i++)b[i]=a[i]-a[i-1];
	sort(b+1,b+n,cmp);
	long long ans = 0;
	for(int i=k;i<n;i++)
	{
		ans+=b[i];
	}
	cout<<ans<<endl;
}

D题
一开始用分治写的,被hack掉了,赛后发现用dp递推即可。
dp[i][j]表示第i个数为结尾的时候,他的序列长度模值为j的时候,最大区间和。
就可以写出转移方程。
注意这里j==1需要特判,因为每一个都需要-k而且dp数组需要初始化为负无穷,不然会出现
n<m的情况,然后递推式会因为长度不够模值的情况。

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
	char ch = getchar(); int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
const int N = 3e5 + 10;
ll n, m, k;
ll dp[N][20];
ll a[N];
ll res;
ll maxn = -(1e15 + 10);
int main()
{
	n = read(); m = read(); k = read();
	upd(i,1, n)
		a[i] = read();
	upd(i, 0, n)
		up(j, 0, m)
		dp[i][j] = maxn;
	//cout << maxn << endl;
	upd(i, 1, n)
	{
		up(j, 0, m)
		{
			if (j == 1)dp[i][j] = max(dp[i - 1][0] + a[i] - k, a[i] - k);//特判
			else if (m == 1)dp[i][0] = max(dp[i - 1][0] + a[i] - k, a[i] - k);//表示出现了新的一个长度为m的序列
			else if(j>1)dp[i][j] = dp[i - 1][j - 1] + a[i];//不够长度m
			else dp[i][0] = dp[i - 1][m - 1] + a[i];//长度为m的数组的末尾。
		//	cout << dp[i][j] << " ";
			res = max(res, dp[i][j]);//每次更新,res初始值为0
		}
		//cout << endl;
	}
	cout << res << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值