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

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

2022-07-08 01:41:00 Not long

Topic link :https://codeforces.com/problemset/problem/1364/A

Title Description : When only the beginning and end of the data string can be deleted , Find the longest , Can not be x The length of the substring divisible .

Their thinking : greedy ( During the input process , When sum cannot be x Divisible time , Take the length of the substring for comparison and update ( l = max( l,max( i,n - i )))i Is the first length on the left ( Similarly, the number on the right is deleted ),n-i Is the length of the right tail ( Similarly, the number on the left is deleted ).

Code :

#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;
}
原网站

版权声明
本文为[Not long]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207080001296893.html

随机推荐