当前位置:网站首页>Codeforces Round #649 (Div. 2)——A. XXXXX

Codeforces Round #649 (Div. 2)——A. XXXXX

2022-07-08 00:02:00 非长

题目链接:https://codeforces.com/problemset/problem/1364/A

题目描述: 在只能删除数据串首尾的情况下,求最长的,不能被x整除的子串的长度。

解题思路: 贪心(在输入过程中,当总和不能被x整除时,取子串长度进行比较更新( l = max( l,max( i,n - i )))i为左边首长度(类似删除了右边数字),n-i为右边尾长度(类似删除了左边数字)。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int num[100005];
int main()
{
    
	int t;
	cin >> t;
	while (t--)
	{
    
		int n, x;
		cin >> n >> x;
		int l = -1, sum = 0;
		for (int i = 1; i <= n; i++)
		{
    
			cin >> num[i];
			sum += num[i];
			if (sum % x)
				l = max(l, max(i, n - i));
		}
		cout << l << endl;
	}
	return 0;
}
原网站

版权声明
本文为[非长]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_52930538/article/details/125636189