蓝桥杯--2013年第四届C/C++B组省赛

版权声明:Please work hard for your dreams. https://blog.csdn.net/calculate23/article/details/87986066
题目标题: 高斯日记

    大数学家高斯有个好习惯:无论如何都要记日记。

    他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210

    后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?

    高斯出生于:1777年4月30日。
    
    在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。

    高斯获得博士学位的那天日记上标着:8113   

    请你算出高斯获得博士学位的年月日。

提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21

请严格按照格式,通过浏览器提交答案。
注意:只提交这个日期,不要写其它附加内容,比如:说明性的文字。

解题思路:可以利用Excel,不过编个程序也不难,注意第一天是出生那天

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
int mt[2][13] = {0,31,28,31,30,31,30,31,31,30,31,30,31,
0,31,29,31,30,31,30,31,31,30,31,30,31}; //0 平年   1  闰年

int judge (int y) {
	if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
		return 1;
	}
	return 0;
}

void solve (int n) {
	//born in 1777-4-30
	int y = 1777, m = 4, d = 29;  //出生那天算第一天
	while (n + d > mt[judge(y)][m]) {
		n -= mt[judge(y)][m];
		m++;
		if (m > 12) {
			m = 1;
			y++;
		}
	}
	d += n;
	cout << y << '-' << m << '-' << d << '\n';
}


int main() {
	solve(8113);  //答案:1799-07-16
	return 0;
}

答案:1799-07-16


标题: 马虎的算式


    小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

    有一次,老师出的题目是:36 x 495 = ?

    他却给抄成了:396 x 45 = ?

    但结果却很戏剧性,他的答案竟然是对的!!

    因为 36 * 495 = 396 * 45 = 17820

    类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54

    假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

    能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?


请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。


答案直接通过浏览器提交。
注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。

解题思路:暴力破解的题,利用深搜或者5个for循环解决,注意暴力的话其实已经考虑了乘法交换律的种类,不需要再乘2,对于这种情况不确定就打表观察一下!有奇效

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;

typedef long long LL;

LL ans = 0;

int main() {
	int a,b,c,d,e;
	for (a = 1; a <= 9; a++) {
		for (b = 1; b <= 9; b++) {
			for (c = 1;b != a && c <= 9; c++) {
				for (d = 1;c != b && c != a && d <= 9; d++) {
					for (e = 1;d != c && d != b && d != a && e <= 9; e++) {
						if (e == a || e == b || e == c || e == d) continue;
						if ((a*10+b) * (c*100 + d*10 + e) == (a*100 + d*10 + b) * (c*10 + e)) {
							printf("%d%d * %d%d%d == %d%d%d * %d%d\n",a,b,c,d,e,a,d,b,c,e);
							ans ++;
						}
					}
				}
			}
		}
	}
	cout << ans << '\n';
	return 0;
}

答案:142


题目标题: 第39级台阶

    小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!

    站在台阶前,他突然又想着一个问题:

    如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?


    请你利用计算机的优势,帮助小明寻找答案。

要求提交的是一个整数。
注意:不要提交解答过程,或其它的辅助说明文字。

解题思路:编程题的话就是一个二维的递推了是吗?感觉像,爆搜吧,水过,注意步数为偶数的前提!

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;

typedef long long LL;

LL ans = 0;

void DFS (int dis, int cnt) {
	if (dis > 39) {
		return ;
	}
	if (dis == 39) {
		if (cnt % 2 == 0) {
			ans ++;
		}
		return ;
	}
	DFS (dis + 1, cnt + 1);
	DFS (dis + 2, cnt + 1);
	return ;
}

int main() {
	DFS (0, 0);
	cout << ans << '\n';  //51167078
	return 0;
}

答案:51167078


标题: 黄金连分数


    黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现。有时需要把这个数字求得很精确。

    对于某些精密工程,常数的精度很重要。也许你听说过哈勃太空望远镜,它首次升空后就发现了一处人工加工错误,对那样一个庞然大物,其实只是镜面加工时有比头发丝还细许多倍的一处错误而已,却使它成了“近视眼”!!


    言归正传,我们如何求得黄金分割数的尽可能精确的值呢?有许多方法。

    比较简单的一种是用连分数:

                  1
    黄金数 = ---------------------
                        1
             1 + -----------------
                          1
                 1 + -------------
                            1
                     1 + ---------
                          1 + ...

                           

    这个连分数计算的“层数”越多,它的值越接近黄金分割数。

    请你利用这一特性,求出黄金分割数的足够精确值,要求四舍五入到小数点后100位。

    小数点后3位的值为:0.618
    小数点后4位的值为:0.6180
    小数点后5位的值为:0.61803
    小数点后7位的值为:0.6180340
   (注意尾部的0,不能忽略)

你的任务是:写出精确到小数点后100位精度的黄金分割值。

注意:尾数的四舍五入! 尾数是0也要保留!

显然答案是一个小数,其小数点后有100位数字,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。

做的时候没做出来,待补…(应该是类似与搜索的东西,终点是第100层


题目标题:前缀判断

    如下的代码判断 needle_start指向的串是否为haystack_start指向的串的前缀,如不是,则返回NULL。

    比如:"abcd1234" 就包含了 "abc" 为前缀

char* prefix(char* haystack_start, char* needle_start)
{
	char* haystack = haystack_start;
	char* needle = needle_start;

	
	while(*haystack && *needle){
		if(______________________________) return NULL;  //填空位置
	}
	
	if(*needle) return NULL;
	
	return haystack_start;
}


请分析代码逻辑,并推测划线处的代码,通过网页提交。
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!

解题思路:水题,指针以及自增运算符优先级,

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;

char* prefix(char* haystack_start, char* needle_start)
{
	char* haystack = haystack_start;
	char* needle = needle_start;


	while(*haystack && *needle){
		if(*haystack++ != *needle++) return NULL;  //填空位置
	}

	if(*needle) return NULL;

	return haystack_start;
}

int main() {
	if (prefix("abcd123","abc") != NULL) {
		puts("YES1");
	}
	if (prefix("abc","abc") != NULL) {
		puts("YES2");
	}
	if (prefix("abc","abcd") != NULL) {
		puts("YES3");
	}
	return 0;
}

答案:*haystack++ != *needle++


标题:三部排序

    一般的排序有许多经典算法,如快速排序、希尔排序等。

    但实际应用时,经常会或多或少有一些特殊的要求。我们没必要套用那些经典算法,可以根据实际情况建立更好的解法。

    比如,对一个整型数组中的数字进行分类排序:

    使得负数都靠左端,正数都靠右端,0在中部。注意问题的特点是:负数区域和正数区域内并不要求有序。可以利用这个特点通过1次线性扫描就结束战斗!!

    以下的程序实现了该目标。

    其中x指向待排序的整型数组,len是数组的长度。

void sort3p(int* x, int len)
{
	int p = 0;
	int left = 0;
	int right = len-1;
	
	while(p<=right){
		if(x[p]<0){
			int t = x[left];
			x[left] = x[p];
			x[p] = t;
			left++;
			p++;
		}
		else if(x[p]>0){
			int t = x[right];
			x[right] = x[p];
			x[p] = t;
			right--;			
		}
		else{
			__________________________;  //填空位置
		}
	}
	
}

   如果给定数组:
   25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0
   则排序后为:
   -3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25
	


请分析代码逻辑,并推测划线处的代码,通过网页提交
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!

解题思路:意思就是类似于快排那样找一个中间标志作为左右两块数据的分水岭。这里更加简单,不用有序,所以遇到负数抓到前面,遇到正数抓到后面,遇到0直接跳过,为什么?因为p之前的一定都为负数,将来遇到负数时就会把前面的0和这个负数交换,0就往后移动了。(Ps:不知道这一行能不能写多句代码,可以的话把0和right的交换其实也可以

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;

void sort3p(int* x, int len)
{
	int p = 0;
	int left = 0;
	int right = len-1;

	while(p<=right){
		if(x[p]<0){
			int t = x[left];
			x[left] = x[p];
			x[p] = t;
			left++;
			p++;
		}
		else if(x[p]>0){
			int t = x[right];
			x[right] = x[p];
			x[p] = t;
			right--;
		}
		else{
			p++;  //填空位置
		}
	}

}

int main() {
//	int val[10] = {-1,2,3,-2,0,-3,1,4,-5,0};
	int val[14] = {25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0};
	sort3p(val, 14);
	for (int i = 0; i < 14; i++) {
		cout << val[i] << ' ';
	}
	return 0;
}

答案:p++



标题:错误票据

    某涉密单位下发了某种票据,并要在年终全部收回。

    每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。

    因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。

    你的任务是通过编程,找出断号的ID和重号的ID。

    假设断号不可能发生在最大和最小号。

要求程序首先输入一个整数N(N<100)表示后面数据行数。
接着读入N行数据。
每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000)
每个整数代表一个ID号。

要求程序输出1行,含两个整数m n,用空格分隔。
其中,m表示断号ID,n表示重号ID

例如:
用户输入:
2
5 6 8 11 9 
10 12 9

则程序输出:
7 9


再例如:
用户输入:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158 
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

则程序输出:
105 120
   

资源约定:
峰值内存消耗 < 64M
CPU消耗  < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。  

解题思路:这题主要是输入的每一行作为字符串的处理一下,剩下的就是排序的事情了(Ps:其实不需要这样,傻了…直接存在数组里面就行了啊)由于数据规模的原因,直接桶排序

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)1e5+5;
int vis[maxn],N,first = maxn;
pair<int, int > tp;
char _data[800];

void change() {
	int num = 0, len = strlen(_data),head = 0;
	while (_data[len - 1] == ' ') len--;
	while (_data[head] == ' ') head++;
	_data[len++] = ' ';
	_data[len] = 0;
	for (int i = head; i < len; i++) {
		if (_data[i] == ' ') {
			if (num < first) first = num;
			vis[num]++;
			num = 0;
		}else {
			num = num*10 - '0' + _data[i];
		}
	}
}

void solve() {
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < N; i++) {
		gets(_data);
		change();
	}
	int tj = 0;
	for (int i = first; i <= 100000; i++) {
		if (vis[i] == 2) {
			tp.second = i;
			tj++;
		}else if (vis[i] == 0) {
			tp.first = i;
			tj++;
		}
		if (tj == 2) return ;
	}
}

int main() {
	scanf("%d",&N);
	getchar();
	solve();
	cout << tp.first << ' ' << tp.second << '\n';
	return 0;
}

题目描述:

小明正在玩一个“翻硬币”的游戏。

桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。

比如,可能情形是:**oo***oooo

如果同时翻转左边的两个硬币,则变为:oooo***oooo

现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:? 

程序输入:

两行等长的字符串,分别表示初始状态和要达到的目标状态。

每行的长度<1000
程序输出:

一个整数,表示最小操作步数


例如:

用户输入:

**********

o****o****

程序应该输出:

5


再例如:
用户输入:

*o**o***o***

*o***o**o***

程序应该输出:

1


资源约定:

峰值内存消耗 < 64M
CPU消耗? < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,
不要调用依赖于编译环境或操作系统的特殊函数。

注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 
不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。

解题思路:简单的模拟题,利用到一点贪心的思想:每发现一个位置不一样一定连同后面的一起反转,达到前缀的最优,这里根据结果看,评测姬输入的数据一定有解!

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)1e3+5;

int ans = 0;
char _now[maxn],_result[maxn];

void solve () {
	int len = strlen(_now);
	for (int i = 0; i < len; i++) {
		if (_now[i] != _result[i]) {
			ans ++;
			if (i < len - 1) {
				if (_now[i + 1] != _result[i + 1]) {
					i++; //同时翻转
				}else {
					if (_result[i + 1] == '*') _result[i + 1] = 'o';
					else _result[i + 1] = '*';
				}
			}
		}
	}
}

int main() {
	gets(_now);
	gets(_result);
	solve();
	printf("%d\n", ans);
	return 0;
}


标题:带分数

    100 可以表示为带分数的形式:100 = 3 + 69258 / 714

    还可以表示为:100 = 82 + 3546 / 197

    注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。

    类似这样的带分数,100 有 11 种表示法。

题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!


例如:
用户输入:
100
程序输出:
11

再例如:
用户输入:
105
程序输出:
6


资源约定:
峰值内存消耗 < 64M
CPU消耗  < 3000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。  

解题思路:是一个搜索题,写的时候当填空题的100来写了23333,不知道脑子在想啥…待补!
脑抽代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
typedef long long LL;

int vis[10] = {0}, result;
int num[10];
LL ans;

void DFS_1 (int index) {
	if (index == 10) {
		if ((num[index - 1] + num[index - 2]*10 + num[index - 3]*100) * result == num[index - 4] + num[index - 5]*10 + num[index - 6]*100 + num[index - 7]*1000 + num[index - 8]*10000) {
			ans ++;
		}
		return ;
	}
	for (int i = 1; i <= 9; i++) {
		if (vis[i] == 0) {
			vis[i] = 1;
			num[index] = i;
			DFS_1(index + 1);
			vis[i] = 0;
		}
	}
	return ;
}

void DFS_2 (int index) {
	if (index == 10) {
		if ((num[index - 1] + num[index - 2]*10 + num[index - 3]*100) * result == num[index - 4] + num[index - 5]*10 + num[index - 6]*100 + num[index - 7]*1000) {
			ans ++;
		}
		return ;
	}
	for (int i = 1; i <= 9; i++) {
		if (vis[i] == 0) {
			vis[i] = 1;
			num[index] = i;
			DFS_2(index + 1);
			vis[i] = 0;
		}
	}
	return ;
}

int main() {
	scanf("%d",&result);
	memset(vis,0,sizeof(vis));
	ans = 0;
	
	for (int i = 1; i <= 9; i++) {
		vis[i] = 1;
		result -= i;
		DFS_1(2);
		result = 100;
		vis[i] = 0;
	}
	
	for (int i = 1; i <= 9; i++) {
		vis[i] = 1;
		for (int j = 1; j <= 9 && j != i; j++) {
			vis[j] = 1;
			result -= i*10 + j;
			DFS_2(3);
			result = 100;
			vis[j] = 0;
		}
		vis[i] = 0;
	}
	
	cout << ans << endl;
	return 0;
}

标题:连号区间数

    小明这些天一直在思考这样一个奇怪而有趣的问题:

    在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是:

    如果区间[L, R] 里的所有元素(即此排列的第L个到第R个元素)递增排序后能得到一个长度为R-L+1的“连续”数列,则称这个区间连号区间。

    当N很小的时候,小明可以很快地算出答案,但是当N变大的时候,问题就不是那么简单了,现在小明需要你的帮助。

输入格式:
第一行是一个正整数N (1 <= N <= 50000), 表示全排列的规模。
第二行是N个不同的数字Pi(1 <= Pi <= N), 表示这N个数字的某一全排列。

输出格式:
输出一个整数,表示不同连号区间的数目。

示例:
用户输入:
4
3 2 4 1

程序应输出:
7

用户输入:
5
3 4 2 5 1

程序应输出:
9

解释:
第一个用例中,有7个连号区间分别是:[1,1], [1,2], [1,3], [1,4], [2,2], [3,3], [4,4]
第二个用例中,有9个连号区间分别是:[1,1], [1,2], [1,3], [1,4], [1,5], [2,2], [3,3], [4,4], [5,5]


资源约定:
峰值内存消耗 < 64M
CPU消耗  < 5000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。  

解题思路:这题应该是当年的压轴了吧?发现是个区间最值问题(最大值和最小值的差是否等于区间长度-1),直接上线段树维护区间最大值和最小值,然后枚举区间,数据过了80%,复杂度是O(n^22logn)。转为RMQ可以降低复杂度或者ST表(动态规划的一种做法O(n)-O(1)),正解是并查集。待补!
线段树做法(80%)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)5e4+5;

struct Node {
	int l,r;
	int weihu;
};

int val[maxn],n,ans;
Node LTree[maxn << 2];
Node LTree_2[maxn << 2];

void pushUp (int idx) {
	LTree[idx].weihu = max(LTree[idx << 1].weihu, LTree[idx << 1 | 1].weihu);
}

void pushUp_2 (int idx) {
	LTree_2[idx].weihu = min(LTree_2[idx << 1].weihu, LTree_2[idx << 1 | 1].weihu);
}

void Build (int l, int r, int idx) {
	LTree[idx].l = l;
	LTree[idx].r = r;
	
	if (l == r) {
		LTree[idx].weihu = val[l];
		return ;
	}
	int mid = l + ((r - l) >> 1);
	Build(l, mid, idx << 1);
	Build(mid + 1, r, idx << 1 | 1);
	pushUp(idx);
}

int Query (int l, int r, int idx) {
	if (l <= LTree[idx].l && r >= LTree[idx].r) {
		return LTree[idx].weihu;
	}
	int mid = LTree[idx].l + ((LTree[idx].r - LTree[idx].l) >> 1);  //很容易出错!!!不是把查询的区间二分,而是对于节点区间二分
	if (r <= mid) {
		return Query(l, r, idx << 1);
	}else if(l > mid) {
		return Query(l, r, idx << 1 | 1);
	}else {
		return max(Query(l, mid, idx << 1), Query(mid + 1, r, idx << 1 | 1));
	}
}

void Build_2 (int l, int r, int idx) {
	LTree_2[idx].l = l;
	LTree_2[idx].r = r;

	if (l == r) {
		LTree_2[idx].weihu = val[l];
		return ;
	}
	int mid = l + ((r - l) >> 1);
	Build_2(l, mid, idx << 1);
	Build_2(mid + 1, r, idx << 1 | 1);
	pushUp_2(idx);
}

int Query_2 (int l, int r, int idx) {
	if (l <= LTree_2[idx].l && r >= LTree_2[idx].r) {
		return LTree_2[idx].weihu;
	}
	int mid = LTree_2[idx].l + ((LTree_2[idx].r - LTree_2[idx].l) >> 1);
	if (r <= mid) {
		return Query_2(l, r, idx << 1);
	}else if(l > mid) {
		return Query_2(l, r, idx << 1 | 1);
	}else {
		return min(Query_2(l, mid, idx << 1), Query_2(mid + 1, r, idx << 1 | 1));
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &val[i]);
	}
	Build(1, n, 1); //max
	Build_2(1, n, 1); //min
	ans = 0;
	int A,B;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j < i; j++) {
			A = Query(j, i, 1);
			B = Query_2(j, i, 1);
			if (A - B == i - j) {
				ans ++;
			}
		}
	}
	cout << (ans + n) << '\n';
	return 0;
}

补:10.
思路:枚举起点,把同一个起点的区间都考虑完毕,子区间的区间最大值能被同一起点的更大区间利用。直接O(n^2)枚举就行了,不用非力的线段树+枚举区间,优化logn

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)5e4+5;

int val[maxn],n,ans;

int main() {
	scanf("%d",&n);
	for (int i = 1; i <= n; i++) {
		scanf("%d",&val[i]);
	}
	ans = 0;
	for (int i = 1; i <= n; i++) {
		int Max = val[i], Min = val[i];
		for (int j = i; j <= n; j++) {
			if (val[j] > Max) {
				Max = val[j];
			}else if (val[j] < Min) {
				Min = val[j];
			}
			if (Max - Min == j - i) {
				ans ++;
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

总结

搜索还是学不会…

猜你喜欢

转载自blog.csdn.net/calculate23/article/details/87986066