【CodeForces - 599C 】Day at the Beach(思维)
生活随笔
收集整理的這篇文章主要介紹了
【CodeForces - 599C 】Day at the Beach(思维)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題干:
給定一個數(shù)列A,要求你將這個數(shù)列劃分成幾個連續(xù)的部分,使得每部分分別從小到大排序后整個數(shù)列有序。
問最多可以劃分成幾個部分。
Input
第一行包含一個整數(shù)?n?(1?≤?n?≤?100?000)?— 表示數(shù)列的長度
之后一行?n?個整數(shù)?hi?(1?≤?hi?≤?109). 描述這個數(shù)列。
Output
輸出最多能劃分出的部分數(shù)。
Example
Input
3 1 2 3Output
3Input
4 2 1 3 2Output
2解題報告:
? 做法很多,標解是用一個排序,然后對排序前后的數(shù)組的差做前綴和,如果前綴和是0,則說明可以斷開,反之則不能斷開。
?這題也可以貪心考慮:能分區(qū)間則分一段,所以對于每一個數(shù),先找到比他小的最右邊的數(shù)(這個可以map維護最右下標,然后二分來得到)然后對這中間的數(shù),也找比他小的最右下標,在維護一個最大值,那么就在之類分段。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; ll sum,ans,n,a[MAX],b[MAX]; int main() {cin>>n;for(int i = 1; i<=n; i++) {scanf("%lld",a+i);b[i]=a[i];}sort(b+1,b+n+1);for(int i = 1; i<=n; i++) {sum += a[i]-b[i];if(sum == 0) ans++;}cout << ans << endl;return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 599C 】Day at the Beach(思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 申请信用卡需要什么条件 办理信用卡需要什
- 下一篇: 【POJ - 1050】To the M