C++程序题-2021.08.22

题目描述

小美当上了会计。她现在拿到了n个开支数据a[1],a[2],…,a[n],现在她想稍微对这些数据做一些统计。小美有三种想统计的信息。第一种是她选择一个区间[L,R],希望知道a[L]+a[L+1]+…+a[R]等于多少,第二种是她选择一个区间[L,R],希望知道a[L],a[L+1],…,a[R]这些数据的有效值是多少,第三种是她选择一个区间[L,R],希望知道a[L],a[L+1],…,a[R]的最大值是多少。一组数据b[1],b[2],…,b[r]的有效值定义为:
∑ j = 1 r ( ∑ i = 1 r b [ i ] − b [ j ] ) 2 \sum_{j=1}^{r}(\sum_{i=1}^{r}b[i]-b[j])^2 j=1r(i=1rb[i]b[j])2

输入描述
第一行一个整数n,表示一共有n个整数。
第二行n个整数a[1],a[2],...,a[n],表示小美拿到的数据是哪些。
第三行一个整数m,表示一共有m个询问。
接下来m行,每行三个整数,optLR,当opt=1时表示是第一种询问,opt=2时表示第二种询问,opt=3时便是第三种询问。(1<=opt<=31<=L<=R<=n
数据保证每个收支数据a[i]满足-1000<=a[i]<=1000
统计次数m满足1<=m<=500
其中,对于60%的数据n满足1<=n<=1000
对于100%的数据n满足1<=n<=100000

输出描述
输出m行,每行一个数,表示改行对应查询的答案。

样例输入

4
1 1 2 3
3
1 1 3
2 2 4
3 1 4

样例输出

4
50
3

参考代码

#include<iostream>
#include<vector>
using namespace std;

vector<int> a;

int fun_opt1(int L, int R)
{
	int ret = 0;
	for (int i = L-1; i < R; ++i)
	{
		ret = ret + a[i];
	}

	return ret;
}

int fun_opt2(int L, int R)
{
	int ret = 0;
	int tmp_1 = fun_opt1(L, R);
	for (int i = L-1; i < R; ++i)
	{
		ret = ret + (tmp_1 - a[i]) * (tmp_1 - a[i]);
	}

	return ret;
}

int fun_opt3(int L, int R)
{
	int ret = 0;
	for (int i = L - 1; i < R; ++i)
	{
		if (ret < a[i])
		{
			ret = a[i];
		}
	}

	return ret;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;

	int a_tmp;
	for (int i = 0; i < n; ++i)
	{
		cin >> a_tmp;
		a.push_back(a_tmp);
	}

	int m;
	cin >> m;

	int opt_tmp, L_tmp, R_tmp;
	vector<int> opt, L, R;
	for (int i = 0; i < m; ++i)
	{
		cin >> opt_tmp >> L_tmp >> R_tmp;
		opt.push_back(opt_tmp);
		L.push_back(L_tmp);
		R.push_back(R_tmp);
	}

	int ret = 0;
	for (int i = 0; i < m; ++i)
	{
		if (opt[i] == 1)
		{
			ret = fun_opt1(L[i], R[i]);
			cout << ret << endl;

		}
		else if (opt[i] == 2)
		{
			ret = fun_opt2(L[i], R[i]);
			cout << ret << endl;

		}
		else if (opt[i] == 3)
		{
			ret = fun_opt3(L[i], R[i]);
			cout << ret << endl;

		}
	}
	
	return 0;
}

注:此代码仅通过45%的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值