CCF-CSP认证(202109)题目

202109-1(数组推导)

题目链接:

计算机软件能力认证考试系统http://118.190.20.162/view.page?gpid=T129题意:

        需要构造A数组,给一个数组B,B[i]代表在A数组的前 i 个数中,最大的是B[i],要求我们求出所有A数组的可能情况中,A数组的和的最大值和最小值。

思路:
        对于最小值,只有B[i]变化的时候才加进去,而最大值对于每个B[i]直接都加就行了。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<cmath>
#include<unordered_set>
#define int long long

using namespace std;

const int N = 110;

int n, x;

void solve()
{
	cin >> n;
	int mi = 0, ma = 0, now = -1;
	for (int i = 0; i < n; ++ i)
	{
		cin >> x;
		if(now == -1) mi += x, ma += x, now = x;
		else
		{
			if(x != now) mi += x, now = x;
			ma += x;
		}
	}
	
	cout << ma << endl;
	cout << mi << endl;
}

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

202109-2(非零段划分)

题目链接:

计算机软件能力认证考试系统icon-default.png?t=N6B9http://118.190.20.162/view.page?gpid=T130题意:

        给一串数组A,我们可以另其中小于p的数变为0,而我们要求这样的操作最好可以使得A数组中最多有多少非零串。

思路:
        我们暴力的做法是使用set,来去重排序,然后枚举每一种可能作为p值,然后求出非零串数并且更新,这样只能过70%,我们可以用vector存放set中去重后的数的所有位置,从而在枚举时更快定位而AC。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<cmath>
#include<unordered_set>

#define int long long

using namespace std;

const int N = 5e5 + 10;

int n, res;
int A[N];
set<int> s;
vector<int> vc[N];

void solve()
{
	cin >> n;
	int ans = 0;
	
	for (int i = 1; i <= n; ++ i) 
	{
		cin >> A[i];
		s.insert(A[i]);
		vc[A[i]].push_back(i);
		if(!A[i - 1] && A[i]) res ++;
	}
	
	ans = res;
	
	for (auto i = s.begin(); i != s.end(); ++ i)
	{
		for (auto j = vc[*i].begin(); j != vc[*i].end(); ++ j)
		{
			if(A[*j - 1] && A[*j + 1] && A[*j]) res ++;
			if(!A[*j - 1] && !A[*j + 1] && A[*j]) res --;
			
			A[*j] = 0;
		}
		
		ans = max(res, ans);
	}
	
	cout << ans << endl;
}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dawpro_加薪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值