codeforcesRound#259(div2)B解題報告
來源:懂視網
責編:小采
時間:2020-11-09 08:01:18
codeforcesRound#259(div2)B解題報告
codeforcesRound#259(div2)B解題報告: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 。.。
導讀codeforcesRound#259(div2)B解題報告: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的編程基礎題,找出兩隊單調非遞減序列,分別為1~x 和 x+1~y,判斷這兩隊是否覆蓋整串數字,且a[n] <= a[1]。
更簡單的一種做法就是,將a[1]~a[n]復制一遍,拓展到a[1]~a[2*n],然后在1 ~ 2*n里面找,是否有一串單調不遞減的個數為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();
}
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
codeforcesRound#259(div2)B解題報告
codeforcesRound#259(div2)B解題報告: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 。.。