• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
CodeForces 347B Fixed Points (水题)

题意:给定 n 数,让你交换最多1次,求满足 ai = i的元素个数。

析:很简单么,只要暴力一遍就OK了,先把符合的扫出来,然后再想,最多只能交换一次,也就是说最多也就是加两个,然后一个的判,注意数组越界。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];

int main(){
    int n;
    cin >> n;
    int ans = 0;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        if(a[i] == i)  ++ans;
    }
    int cnt = 0;
    for(int i = 0; i < n; ++i){
        if(a[i] != i){
            if(a[i] < n && a[a[i]] == i){  cnt = 2;  break; }
            else if(a[i] < n)  cnt = 1;
        }
    }
    cout << ans + cnt << endl;
    return 0;
}

 

posted on 2016-07-18 23:30  dwtfukgv  阅读(203)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3