Codeforces Round #616 (Div. 2)

A:
對一個大數進行操作,使得不被2整除,各位之和被2整除
先操作最後一位,最後一位必須爲單數,之後各位只保留一個單數即可,其他單數都刪掉。

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		string s;
		cin >> s;
		while(n > 1)
		{
			if(s[n-1] == '0' || s[n-1] == '2'||s[n-1] == '4'||s[n-1] == '6' ||s[n-1] == '8')
			{	
				s[n-1]='#';//把最後一位刪去 
				n--;
			}
			else
			break;//把最後一位處理完 
		}
		if(s[n-1] == '0' || s[n-1] == '2'||s[n-1] == '4'||s[n-1] == '6' ||s[n-1] == '8')
		{	
			cout << -1 << endl;
			continue;
		}
		int flag = 0;
		for(int i = 0; i < n - 1; i++)
		{
			if(!flag)
			{
				if(s[i] == '1'||s[i] == '3'||s[i] == '5'||s[i] == '7'||s[i] == '9')
				{
					flag = 1;
				}
			}
			else
			{
				if(s[i] == '1'||s[i] == '3'||s[i] == '5'||s[i] == '7'||s[i] == '9')
				{
					s[i] = '#';
				}
			}
		}
		if(flag == 0)
		{
			cout << -1 << endl;
			continue;
		}
		for(int i = 0; i < n; i++)
		{
			if(s[i] != '#')
			cout << s[i];
		}
		cout << endl;
		
	}
	return 0;
}

B:
給你一個數組,要求你選定一個數,他左邊遞增,右邊遞減
可以對任意一個數進行操作,使其-1,可以操作任意次。
可以這樣認爲,從左到中間的max,操作之後,最壞情況就是0,1,2,3,…max,max-1…3,2,1,0
即我先選定從左到右第一個a[i] < i的爲max,他左邊都滿足a[i] > i
看他的右邊,是否都滿足遞減。
之後就是判斷max中間這兩個數會不會相等:就是說max左邊是一個遞增到i-1的序列,從max開始向右是一個遞減的序列,
max與max左邊一個數有可能相等,max從右往左的n-i-1有可能與max左側從左往右的i-1相等。

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 3e5 + 50;
int a[maxn];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		for(int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		int ans = 0;//默認可以 
		int t = -1;
		for(int i = 0; i < n; i++)
		{
			if(a[i] < i)//不滿足的 
			{
				t = i;
				break;
			}
		}
		if(t > 0)
		{
			for(int i = t; i < n; i++)
			{	
				if(a[i] < n - i - 1)
				{
					ans = 1;
				//	cout << "右邊不滿足"; 
				}
			}
			if(t-1 == n-t-1 && a[t] >= a[t-1])
			{
				ans = 1;
			//	cout << "相等";
			}
		}
		if(!ans)
		{
			cout << "Yes" << endl;
		}
		else
		{
			cout << "No" << endl;
		}
	}
	return 0;
}
發佈了11 篇原創文章 · 獲贊 0 · 訪問量 1735
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章