A. Digits Sequence Dividing---Educational Codeforces Round 59 (Rated for Div. 2)

Digits Sequence Dividing

time limit per tes 1 second
memory limit per test 256 megabytes

题目链接http://codeforces.com/problemset/problem/1107/A

A. Digits Sequence Dividing---Educational Codeforces Round 59 (Rated for Div. 2)
A. Digits Sequence Dividing---Educational Codeforces Round 59 (Rated for Div. 2)


题目大意:给你n个询问,问你是否能将一个数分成n段,对于每段i<j,ti<tj,如果能输出YES,并输出其划分段数和划分的内容。

emmmm,就是个字符串的操作,不过这题用string写起来比较放便,我们可以先取第一个数为第一段,只有当n=2,且第二个数<=第一个数的时候是NO的。接下来我们直接模拟,每次加上一个数之后判断是否大于前面的数,如果大于则划分下一段,否则继续添加数。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;
const int mac=2e5+10;
#define ll long long 
int a[mac];
int judge(string s1,string s2)
{
	if (s1.size()>s2.size()) return 0;
	if (s1.size()<s2.size()) return 1;
	else {
		if (s1<=s2) return 1;
		else return 0;
	}
}
int main()
{
	int q,n;
	scanf ("%d",&q);
	while (q--){
	    string s,ans[310];
		scanf ("%d",&n);
		cin>>s;
		int num=1;
		if (s.size()==2 && s[0]>=s[1]) {
			printf ("NO\n");
			continue;
		}
		else {
			ans[0]=s[0];
			for (int i=1; i<s.size(); i++){
				if (judge(ans[num],ans[num-1]))
				  ans[num]+=s[i];
				else ans[++num]=s[i];
			}
		}
		if (judge(ans[num],ans[num-1])) ans[num-1]+=ans[num],num--;
		cout<<"YES"<<endl;
		cout<<num+1<<endl;
		for (int i=0; i<=num; i++){
			cout<<ans[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
 } 
上一篇:2020牛客暑期多校训练营(第七场) H Dividing


下一篇:CF1538D Another Problem About Dividing Numbers题解