QLU_002新生赛补题

A题-构造最长递增子串

  • 题目描述

Hkhv has a sequence a, consisting of n integers.
We’ll call a sequence ai,ai+1,…,aj (1<= i<= j<= n) a subsegment of the sequence a. The value (j-i+1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.

  • 输入

The first line contains integer n (1 <=n <= 10^5). The next line contains n integers a1,a2,…,an (1 <=ai <= 10^9).

  • 输出

In a single line print the answer to the problem — the maximum length of the required subsegment.

  • 样例输入

6
7 2 3 1 5 6

  • 样例输出

5

  • 解题思路

这题花了不少时间中间遇到了不少问题,期间也花时间再去acwing好好学了下dp的最长上升子序列问题,后来发现这题好像用不上QAQ。。
本题的关键点有以下两点
1.如何得到每个数字前缀后缀中最长上升子序列
2.如何判断前缀后缀最长上升子序列是否可以连接
第一个问题需要用到利用dp的思想考虑一下状态转移方程,其实也很好想,如果输入的数组长度为1,那么这个数本身就是最长上升子序列,也是就最长上升子序列为1,如果后一个数大于前一个数那么就是前一个数的最长上升子序列长度加1就可以了,也就是dp[i] = dp[i - 1] + 1,后缀最长上升子序列同理,只不过倒过来变成dp[i] = dp[i + 1] + 1。
第二个问题卡了我不少时间在1月9号的比赛上做F题的时候突然想到了,也就是考虑每个数前缀最长上升子序列最后一个数和后缀最长上升子序列的第一个数大小,但一开始的时候我忘了,都是整数。。所以也就是中间必须还得留个1的差能放一个数。
虽然理清了思路这题在做的时候还是遇到了问题,看了孙晨曦同学的代码发现几个数组开头前一位和结尾后一位必须得初始化为0,我开始觉得开了全局数组自动全初始化为0了,就不用初始化了。

  • 代码
#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

const int maxn = 1e5 + 10;

int n;
int a[maxn];
int b[maxn],e[maxn];
int res;

int main() {
	while(cin >> n) {
		a[0] = a[n + 1] = 0;
		b[0] = b[n + 1] = 0;
		e[0] = e[n + 1] = 0;
		res = 0;
		for(int i = 1;i <= n;i++) scanf("%d", &a[i]);
		for(int i = 1;i <= n;i++) {
			if(a[i] > a[i - 1]) {
				e[i] = e[i - 1] + 1;
			}
			else {
				e[i] = 1;
			}
		}
		for(int i = n;i > 0;i--) {
			if(a[i] < a[i + 1]) {
				b[i] = b[i + 1] + 1;
			}
			else {
				b[i] = 1;
			}
		}
		for(int i = 1;i <= n;i++) {
			if(a[i + 1] - a[i - 1] > 1) {
				res = max(res,b[i + 1] + e[i - 1] + 1);
			}
			else {
				res = max(res,max(b[i + 1] + 1,e[i - 1] + 1));
			}
		}
		cout << res << endl;
	}
	return 0;
}

E题-由你来决定怎么颁奖

  • 题目描述

So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved pi problems. Since the participants are primarily sorted by the number of solved problems, then p1≥p2≥⋯≥pn.
Help the jury distribute the gold, silver and bronze medals. Let their numbers be g, s and b, respectively. Here is a list of requirements from the rules, which all must be satisfied:
for each of the three types of medals, at least one medal must be awarded (that is, g>0, s>0 and b>0);
the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, g<s and g<b, but there are no requirements between s and b);
each gold medalist must solve strictly more problems than any awarded with a silver medal;
each silver medalist must solve strictly more problems than any awarded a bronze medal;
each bronze medalist must solve strictly more problems than any participant not awarded a medal;
the total number of medalists g+s+b should not exceed half of all participants (for example, if n=21, then you can award a maximum of 10 participants, and if n=26, then you can award a maximum of 13 participants).
The jury wants to reward with medals the total maximal number participants (i.e. to maximize g+s+b) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.

  • 输入

The first line of the input contains an integer t (1≤t≤10000) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains an integer n (1≤n≤4⋅105) — the number of BeRC participants. The second line of a test case contains integers p1,p2,…,pn (0≤pi≤106), where pi is equal to the number of problems solved by the i-th participant from the final standings. The values pi are sorted in non-increasing order, i.e. p1≥p2≥⋯≥pn.
The sum of n over all test cases in the input does not exceed 4⋅105.

  • 输出

Print t lines, the j-th line should contain the answer to the j-th test case.
The answer consists of three non-negative integers g,s,b.
Print g=s=b=0 if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time.
Otherwise, print three positive numbers g,s,b — the possible number of gold, silver and bronze medals, respectively. The sum of g+s+b should be the maximum possible. If there are several answers, print any of them.

  • 样例输入

5
12
5 4 4 3 2 2 1 1 1 1 1 1
4
4 3 2 1
1
1000000
20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
32
64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11

  • 样例输出

1 2 3
0 0 0
0 0 0
2 5 3
2 6 6

  • 解题思路

本题主要是利用贪心的思想去满足题目的条件,和H题一样也只输出一个正确答案,那问题的关键如何保证结果一定正确。
本题我自己敲了几次,结果都有一点小问题,后来参考了孙晨曦同学的代码和思路。
1.金牌数只选择最高成绩。
2.银牌先从金牌数后一个成绩开始计数,如果比金牌的数量少,再补足。
3.铜牌数一直记到奖牌数最大值。
4.最后比较奖牌数之间的关系满足金牌必须少于铜牌和银牌。

  • 代码
#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

const int maxn = 4e5 + 10;

int a[maxn];
int t,n;

int cmp(int x,int y) {
	return x > y;
}

int main() {
	cin >> t;
	while(t--) {
		scanf("%d", &n);
		int k = n / 2;
		for(int i = 1;i <= n;i++) scanf("%d", &a[i]);
		sort(a + 1,a + n + 1,cmp);
		int g = 0,s = 0,b = 0;
		int i = 1;
		while(a[i] == a[1] && i <= k) {
			g++;
			i++;
		}
		int temp = a[i];
		while(a[i] == temp && i <= k) {
			s++;
			i++;
		}
		while(s <= g && i <= k) {
			temp = a[i];
			while(a[i] == temp && i <= k) {
				s++;
				i++;
			}
		}
		while(a[i] != a[k + 1] && i <= k) {
			b++;
			i++;
		}
		if(g >= s || g >= b) {
			cout << "0 0 0" << endl;
			continue;
		}
		printf("%d %d %d\n", g,s,b);
	}
	return 0;
}

G题-0011

  • 题目描述

Alex likes to play with one and zero!One day he gets an empty string.So our cute boy wants to add one and zero in it. Every time he will add ‘01’in the string at any position and then get a new string.For example:if the string is “01” now ,he can get “0101” or “0011,Now give you a string that is Alex has get,you need to answer whether the string is legal?

  • 输入

First is a integer n(n<=100)
Next contains n lines .Every line is a string whose legth is no more than 1000.

  • 输出

For each case output “YES” in a single line if it’s legal.
Or you need to output “NO”;

  • 样例输入

3
0101
0110
0011

  • 样例输出

YES
NO
YES

  • 解题思路

本题我的思路是用string自带的find和erase两个函数对字符串进行处理,由题意可知如果输出“YES”必然整个字符串都是由01构成的,所以我们只需要不断地弹出01直到无法弹出为止,再将字符串与“01”作比较判断一下即可.需要注意的是样例容易给人产生误导以为必须是01或者10组成,但实际上例如01111这种输入也是存在的

  • 代码
#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main() {
	int p;
	int n;
	string str;
	cin >> n;
	while(n--) {
		cin >> str;
		while(1) {
			if(str.length() == 2) {
				break;
			}
			p = str.find("01");
			if(p == str.npos) {
				break;
			}
			else {
				str.erase(p,2);
			}
		}
		if(str == "01") {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}
	return 0;
}

H题-Perfect String

  • 题目描述

A string is called beautiful if no two consecutive characters are equal. For example, “ababcb”, “a” and “abab” are beautiful strings, while “aaaaaa”, “abaa” and “bb” are not.
Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’. Ahcl needs to replace each character ‘?’ with one of the three characters ‘a’, ‘b’ or ‘c’, such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters ‘?’, the condition si≠si+1 should be satisfied for all 1≤i≤|s|−1, where |s| is the length of the string s.

  • 输入

The first line contains positive integer t (1≤t≤1000) — the number of test cases. Next t lines contain the descriptions of test cases.
Each line contains a non-empty string s consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’.
It is guaranteed that in each test case a string s has at least one character ‘?’. The sum of lengths of strings s in all test cases does not exceed 105.

  • 输出

For each test case given in the input print the answer in the following format:
If it is impossible to create a beautiful string, print “-1” (without quotes);
Otherwise, print the resulting beautiful string after replacing all ‘?’ characters. If there are multiple answers, you can print any of them.

  • 样例输入

3
a???cb
a??bbc
a?b?c

  • 样例输出

ababcb
-1
acbac

  • 提示

In the first test case, all possible correct answers are “ababcb”, “abcacb”, “abcbcb”, “acabcb” and “acbacb”. The two answers “abcbab” and “abaabc” are incorrect, because you can replace only ‘?’ characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 4-th and 5-th characters will be always equal.
In the third test case, the only answer is “acbac”.

  • 解题思路

这种只用输出一个正确结果的题目还是第一次做,比赛的时候很懵圈,不知道怎么下手。
回去之后好好想了下,本题关键点有以下两点:
1.字符串如果开头是?应该单独处理。
2.没有符合条件的结果输出-1。
所以我这里单独把这两种情况拿出来处理了一下:
1.对字符串开头单独处理一下看看字符串下标1位置的字母,判断一下即可。
2.设置一个flag进行标记,在未对字符串中的?处理前,单独判断下如果有重复的字母把flag变为false。
最后处理剩下的字符串中的?判断下?前一位置和后一位置是什么字符,再更改当前位置的?,再用if判断下flag为真输出字符串s,反之输出-1.

  • 代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>

using namespace std;

int n;
string s;
bool flag;

int main() {
	cin >> n;
	while(n--) {
		flag = true;
		cin >> s;
		for(int i = 0;i < s.length();i++) {
			if(s[i] == s[i + 1] && s[i] != '?') {
				flag = false;
				break;
			}
		}
		if(s[0] == '?') {
			if(s[1] != 'a') {
				s[0] = 'a';
			}
			else if(s[1] != 'b') {
				s[0] = 'b';
			}
			else if(s[1] != 'c') {
				s[0] = 'c';
			}
		}
		for(int i = 0;i < s.length();i++) {
			if(s[i] == '?') {
				if(s[i + 1] != 'a' && s[i - 1] != 'a') {
					s[i] = 'a';
				}
				else if(s[i + 1] != 'b' && s[i - 1] != 'b') {
					s[i] = 'b';
				}
				else if(s[i + 1] != 'c' && s[i - 1] != 'c') {
					s[i] = 'c';
				}
			}
		}
		if(flag) {
			cout << s << endl;
		}
		else {
			cout << -1 << endl;
		}
	}
	return 0;
}

I题-十进制中的二进制

  • 题目描述

hkhv学长最近对二进制数很感兴趣,喜欢一切0和1组成的数。现在有一个十进制整数n,问你1到n之间有多少个数是只有0和1组成的类似二进制的数,输出他们的个数。

  • 输入

输入数据包含一个数n (1 <= n <=10^9).

  • 输出

输出1到n中类似二进制的数的个数.

  • 样例输入

10

  • 样例输出

2

  • 解题思路

本题通过观察可发现一些数学规律,可通过打表预处理出每一位对应的二进制数字数量,也就是dp[i] = dp[i - 1] + 2^(n - 1)。然后我们开始对数字进行处理,为了避免pow的一些精度问题,所以没用pow所以代码复杂了一点,也不知道为啥int被爆了,所以干脆全换成long long了,把这个数每一位的数字全部存进数组里判断就行,>1肯定就包括后面位的所有情况弹出即可,==1的话继续往后循环累加就行。

  • 代码
#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

long long dp[20];
long long count_num[20];

int main() {
	long long ans,sum;
	long long temp;
	long long n;
	long long t;
	long long num = 2;
	long long p;
	dp[1] = 1;
	for (int i = 2; i <= 11; i++) {
		dp[i] = dp[i - 1] + num;
		num *= 2;
	}
	while (cin >> n) {
		temp = n;
		sum = 0;
		ans = 0;
		p = 1;
		while (temp > 0) {
			temp /= 10;
			ans++;
			p *= 10;
		}
		p /= 10;
		temp = n;
		for (int i = 0; i < ans; i++) {
			count_num[i] = temp / p;
			temp %= p;
			p /= 10;
		}
		t = ans;
		for (int i = 0; i < ans; i++) {
			if (count_num[i] >= 2) {
				sum += dp[t];
				break;
			}
			else if (count_num[i] == 1) {
				sum += dp[t - 1] + 1;
			}
			t--;
		}
		cout << sum << endl;
	}
}

J题-新年快乐

  • 题目描述

Frog Wa has many frogs, so he can hear GuaGuaGua every day. But one day a flappy bird ate his frogs, so Frog Wa decided to build a fence to protect his precious frogs. Frog Wa told you four points (x, 0), (x, y), (z, 0), (z, t) which represent for the vertex of the fence. he want to know the area of the fence, can you tell him?

  • 输入

The input consist of four integers x, y, z and t. 0 <= x, y, z, t <= 10000, x < z and y < t.

  • 输出

For each test case, print the area, numbers should accurate to one decimal places.

  • 样例输入

1 1 2 2
0 2 6 6
0 0 2 10

  • 样例输出

1.5
24.0
10.0

  • 解题思路

本题就是计算一个梯形的面积,套用矩形的面积公式,并注意保留一位小数即可。

  • 代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>

using namespace std;

double x, y, z, t;
double ans;

int main() {
	while (cin >> x >> y >> z >> t) {
		ans = 0;
		ans += (y + t) * (z - x) * 0.5;
		printf("%.1lf\n", ans);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
同样的,需要先解析JWT token,获取其中的payload部分。使用第三方库github.com/dgrijalva/jwt-go来进行解析,具体代码如下: ```go import ( "fmt" "github.com/dgrijalva/jwt-go" ) func main() { tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjMsImV4cCI6MTY4NTc3OTYwOSwiaWF0IjoxNjg1MTc0ODA5LCJpc3MiOiJDYXJnbyIsInN1YiI6InVzZXIgdG9rZW4ifQ.t7OVtwRE1IobIYRjsaJgJCZDHp4jBlO3VZc9Dsi-t5E 7436229=skey%3D%40Uyta2flI3%3Buin%3Do0007436229%3Bp_skey%3Dqm4lF-L8Gunzmcu3w9VAz1mMYy5jGuP72ecdQ-pzdc8_ 1023250836=skey%3D%40HTv3Ryui3%3Buin%3Do1023250836%3Bp_skey%3DS5zhIlqb2Y-mvHo377gfE9CFHC45BP-FaYudutu2QLU_" token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { return []byte("secret"), nil // 这里可以替换成自己的加密密钥 }) if err != nil { fmt.Println("Error parsing JWT token:", err) return } payload := token.Claims.(jwt.MapClaims) fmt.Println(payload) } ``` 解析出来的payload是一个map,其中包含了UserId、exp、iss和sub等键值对,以及还原的skey、uin和p_skey键值对。需要注意的是,1023250836=skey%3D%40HTv3Ryui3%3Buin%3Do1023250836%3Bp_skey%3DS5zhIlqb2Y-mvHo377gfE9CFHC45BP-FaYudutu2QLU_是一个整体,需要再进行一次解码才能得到其中的键值对。具体代码如下: ```go import ( "fmt" "net/url" "strings" ) func main() { tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjMsImV4cCI6MTY4NTc3OTYwOSwiaWF0IjoxNjg1MTc0ODA5LCJpc3MiOiJDYXJnbyIsInN1YiI6InVzZXIgdG9rZW4ifQ.t7OVtwRE1IobIYRjsaJgJCZDHp4jBlO3VZc9Dsi-t5E 7436229=skey%3D%40Uyta2flI3%3Buin%3Do0007436229%3Bp_skey%3Dqm4lF-L8Gunzmcu3w9VAz1mMYy5jGuP72ecdQ-pzdc8_ 1023250836=skey%3D%40HTv3Ryui3%3Buin%3Do1023250836%3Bp_skey%3DS5zhIlqb2Y-mvHo377gfE9CFHC45BP-FaYudutu2QLU_" tokenParts := strings.Split(tokenString, " ") // 解析JWT token token, err := jwt.Parse(tokenParts[0], func(token *jwt.Token) (interface{}, error) { return []byte("secret"), nil // 这里可以替换成自己的加密密钥 }) if err != nil { fmt.Println("Error parsing JWT token:", err) return } payload := token.Claims.(jwt.MapClaims) fmt.Println(payload) // 解码1023250836=skey%3D%40HTv3Ryui3%3Buin%3Do1023250836%3Bp_skey%3DS5zhIlqb2Y-mvHo377gfE9CFHC45BP-FaYudutu2QLU_,获取键值对 kvString := strings.Split(tokenParts[2], "=")[1] kvString, _ = url.QueryUnescape(kvString) kvParts := strings.Split(kvString, ";") skey := strings.Split(kvParts[0], "=")[1] uin := strings.Split(kvParts[1], "=")[1] p_skey := strings.Split(kvParts[2], "=")[1] fmt.Println(skey, uin, p_skey) } ``` 这样就可以获取到所有的键值对了,需要注意的是,skey、uin和p_skey中包含了特殊字符,需要进行解码才能得到真正的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值