codeforcesRound#259(div2)B解題報(bào)告
來源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-09 08:01:18
codeforcesRound#259(div2)B解題報(bào)告
codeforcesRound#259(div2)B解題報(bào)告:B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a sequence of integers a 1 , a 2 。.。
導(dǎo)讀codeforcesRound#259(div2)B解題報(bào)告:B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a sequence of integers a 1 , a 2 。.。

解法:
也是一道很easy的編程基礎(chǔ)題,找出兩隊(duì)單調(diào)非遞減序列,分別為1~x 和 x+1~y,判斷這兩隊(duì)是否覆蓋整串?dāng)?shù)字,且a[n] <= a[1]。
更簡(jiǎn)單的一種做法就是,將a[1]~a[n]復(fù)制一遍,拓展到a[1]~a[2*n],然后在1 ~ 2*n里面找,是否有一串單調(diào)不遞減的個(gè)數(shù)為n的序列。
代碼:
#include
#define N_max 123456
int n, x, y, cnt;
int a[N_max];
void init() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
}
void solve() {
for (int i = 1; i <= n; i++)
if (a[i] > a[i+1]) {
x = i;
break;
}
if (x == n)
y = n;
else
for (int i = x+1; i <= n; i++)
if (a[i] > a[i+1]) {
y = i;
break;
}
if (x == n)
printf("0\n");
else if (y == n && a[y] <= a[1])
printf("%d\n", y-x);
else
printf("-1\n");
}
int main() {
init();
solve();
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
codeforcesRound#259(div2)B解題報(bào)告
codeforcesRound#259(div2)B解題報(bào)告:B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a sequence of integers a 1 , a 2 。.。