升序操作 (二分 贪心)
2017-11-07 17:29
127 查看
11.7
也可以不用判环,暴力check,按照顺序暴力跑(看代码吧)
也可以不用判环,暴力check,按照顺序暴力跑(看代码吧)
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define LL long long #define N 400010 using namespace std; int n, cnt; int a , b ; inline int read(){ int x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int calc(){ cnt = 0; for(register int i=0; i<n; i++){ while (b[i] != i){ cnt++; swap(b[i], b[b[i]]); } } return cnt; } bool judge(int i){ if(i >= cnt) return 1; return 0; } struct AA{ int x, y; }aa ; int check(int x){ for(register int i=0; i<n; i++) b[i] = a[i]; for(int i=1; i<=x; i++){ swap(b[aa[i].x], b[aa[i].y]); } return calc(); } int main(){ freopen ("c.in", "r", stdin); freopen ("c.out", "w", stdout); scanf("%d", &n); for(register int i=0; i<n; i++) a[i] = read(), b[i] = a[i]; cnt = calc(); if( !cnt ){ printf("0\n"); return 0;} int ans = 0; for(register int i=1; i<=2*n; i++) aa[i].x = read(), aa[i].y = read(); int l = 0, r = n; while ( l <= r ){ int mid = (l + r) >> 1; int cc = check( mid ); if(cc <= mid) ans = mid, r = mid - 1; else l = mid + 1; } cout << ans << endl; }
相关文章推荐
- NYOJ 914 Yougth的最大化 【贪心】+【二分】
- 二分查找binsearch: 在v[0]&lt;=v[1]&lt;=v[2]&lt;=......&lt;=v[n-1]中查找x,数组v的元素必须以升序排列
- codeforces #262 DIV2 C题Present(二分+贪心)
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 前缀最小值+贪心+二分搜索
- codeforces round #347 div2 B rebus 贪心 + 二分
- Codeforces 589F Gourmet and Banquet(二分+贪心)
- 湖南NOIP集训模拟题DAY1 BY ExfJOE [贪心][DP][二分]
- CodeForces 460C--- Present(二分+贪心)
- 【HPU】神奇的操作 【vector +二分 】
- UVALIVE 3971 Assemble 二分+贪心
- Codeforces 626C Block Towers「贪心」「二分」「数学规律」
- UVALive 2949 Elevator Stopping Plan(二分 + 贪心)
- hdu 4004 The Frog's Games (二分+贪心)
- nyoj914Yougth的最大化(二分搜索 + 贪心)
- HDU1257 最少拦截系统 (贪心+STL+二分)
- UVa 714 Copying Books 二分 + 贪心 (最大值最小化问题)
- CS R20 C(贪心+二分) D(套路(n后第k个合法数)二分+数位DP.) E(好题:回文,字符串哈希)
- 关于二分+贪心的体会
- TOJ 1644: Cow Acrobats -- 二分,贪心
- 【NOIP2015】跳石头 (二分+贪心判定)