标签:
3 112 233 1
22 35 UncertainHintIn the first example, it is optimal to split $ 112 $ into $ 21 $ and $ 1 $, and their sum is $ 21 + 1 = 22 $. In the second example, it is optimal to split $ 233 $ into $ 2 $ and $ 33 $, and their sum is $ 2 + 33 = 35 $. In the third example, it is impossible to split single digit $ 1 $ into two parts.
曾经有一位国王,统治着一片未名之地。他膝下有三个女儿。 三个女儿中最年轻漂亮的当属Psyche。她的父亲不确定她未来的命运,于是他来到Delphi神庙求神谕。 神谕可以看作一个不含前导零的正整数n。 为了得到真正的预言,他可以将n的各个数位重新排列,并将其分成两个不含前导零的正整数。 请你帮助他求出这两个正整数最大的和。如果不存在这样的两个正整数,输出"Uncertain".
第一行一个整数T (1≤T≤10),代表数据组数。 接下来T行,每行一个正整数n (1≤n<10?10000000??)。
对于每组数据,输出一个整数表示最大的和。若不存在一种方案,输出"Uncertain".
3 112 233 1
22 35 Uncertain
对于第一组数据,最优方案是将112分成21和1,最大的和为21+1=22。 对于第二组数据,最优方案是将233分成2和33,最大的和为2+33=35。 对于第三组数据,显然无法将一个数位分成两部分。 建议使用效率较高的读入方式。
代码:
#include<cstdio>
#include<cstring>
char shu[10000100];
int zui[10000100];
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%s",shu);
int ll=strlen(shu);
int ge[10]={0};
memset(zui,0,sizeof(zui));
for (int i=0;i<ll;i++)
ge[shu[i]-'0']++;
if (ge[0]>ll-2)
{
printf("Uncertain\n");
continue;
}
int kp;
for (int i=1;i<10;i++)
{
if (ge[i])
{
kp=i;
ge[i]--;
break;
}
}
int llp=0;
for (int i=0;i<10;i++)
{
if (ge[i])
{
while (ge[i]--)
zui[llp++]=i;
}
}
zui[0]+=kp;
llp=ll-2;
for (int i=0;i<=llp;i++)
{
if (zui[i]>9)
{
zui[i]-=10;
zui[i+1]++;
if (i==llp)
llp++;
}
}
for (int i=llp;i>=0;i--)
printf("%d",zui[i]);
printf("\n");
}
return 0;
}
2 3 2 1 1 2 2 3 5 5 4 3 2 1 1 2 3 4 5
1 0HintIn the first example, there is only one valid permutation (2,1,3) . In the second example, it is obvious that there is no valid permutation.
Cupid一不小心将爱情之箭射到了自己,他爱上了Psyche。 这引起了他的母亲Venus的注意。Venus将Psyche带到了一堆打乱的谷堆旁。 这儿共有n堆稻谷,编号为1到n。Psyche需要将这些谷堆以某种顺序排列,设最终排在第i位的谷堆是A?i??。 她得知了一些该排列的要求: 1. 对于任意整数i∈[1,n],A?1??,A?2??,...,A?i??的最小值为B?i??。 2. 对于任意整数i∈[1,n],A?1??,A?2??,...,A?i??的最大值为C?i??。 现在Psyche想知道,共有多少种合法的排列。由于答案可能很大,输出时对998244353取模。
第一行,一个整数T (1≤T≤15),代表数据组数。 对于每组数据,第一行有一个整数n (1≤n≤10?5??),代表排列大小。 第二行,n个整数,第i个整数为B?i?? (1≤B?i??≤n)。 第三行,n个整数,第i个整数为C?i?? (1≤C?i??≤n)。
输出T行,对于每组数据输出答案对998244353取模的结果。
2 3 2 1 1 2 2 3 5 5 4 3 2 1 1 2 3 4 5
1 0
对于第一组数据,只有一种合法的排列(2,1,3)。
对于第二组数据,没有合法的排列。
代码:
#include<cstdio>
#include<cstring>
int n,mi,ma,bi[100100],ci[100100];
__int64 s,cha;
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
scanf("%d",&bi[i]);
for (int i=0;i<n;i++)
scanf("%d",&ci[i]);
bool fafe=true;
if (ci[0]!=bi[0])
{
printf("0\n");
continue;
}
mi=ma=bi[0];cha=0;s=1;
for (int i=1;i<n;i++)
{
if (bi[i]!=mi&&ci[i]!=ma)
{
fafe=false;
break;
}
if (bi[i]>mi||ci[i]<ma)
{
fafe=false;
break;
}
if (bi[i]==mi&&ci[i]==ma)
{
if (cha<1)
{
fafe=false;
break;
}
// printf("%I64d 66\n",cha);
s*=cha;
cha--;
s%=998244353;
}
else if (bi[i]!=mi)
{
cha+=mi-bi[i]-1;
mi=bi[i];
}
else
{
cha+=ci[i]-ma-1;
ma=ci[i];
}
}
if (fafe)
printf("%I64d\n",s);
else
printf("0\n");
}
return 0;
}
2 2 1 3 1 1 4 3 10 1 1 2 4
2 5HintIn the first example, $ 2, 3 $ are available. In the second example, $ 6, 7, 8, 9, 10 $ are available.
黎明时,Venus为Psyche定下了第二个任务。她要渡过河,收集对岸绵羊身上的金羊毛。 那些绵羊狂野不驯,所以Psyche一直往地上丢树枝来把它们吓走。地上现在有n根树枝,第i根树枝的长度是a?i??. 如果她丢的下一根树枝可以和某两根树枝形成三角形,绵羊就会被激怒而袭击她。 现在Psyche手中只有长度不小于L且不大于R的树枝。请你帮忙计算,她下一根可以丢多少种不同长度的树枝而不会把绵羊激怒呢?
第一行,一个整数T(1≤T≤10),代表数据组数。 对于每组数据,第一行有三个整数n,L,R (2≤n≤10?5??,1≤L≤R≤10?18??)。 第二行,n个整数,第i个整数为a?i?? (1≤a?i??≤10?18??),代表第i根树枝的长度。
输出T行,对于每组数据,输出选取方式总数。
2 2 1 3 1 1 4 3 10 1 1 2 4
2 5
对于第一组数据,可以选用长度为2,3的树枝。 对于第二组数据,可以选用长度为6,7,8,9,10的树枝。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
__int64 L,R,s,shu[100100],mi,ma;
int n;
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d%I64d%I64d",&n,&L,&R);
for (int i=0;i<n;i++)
scanf("%I64d",&shu[i]);
sort(shu,shu+n);
mi=0;
ma=R;s=0;
for (int i=n-1;i>0;i--)
{
mi=shu[i]+shu[i-1];
if (mi<=ma)
{
if (mi<=L)
{
s+=ma-L+1;
ma=L-1;
break;
}
else
s+=ma-mi+1;
}
ma=min(ma,shu[i]-shu[i-1]);
if (ma<L)
break;
}
if (ma>=L)
s+=ma-L+1;
printf("%I64d\n",s);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/leibniz_zhang/article/details/51938878