Codeforces 347B Fixed Points (water problem)

Source: Internet
Author: User

Test instructions: Given n number, let you swap up to 1 times to satisfy the number of elements of AI = i.

Analysis: Very simple, as long as the violence once again OK, first to match the sweep out, and then think, at most can only exchange once, that is, the maximum is added two, and then a sentence, notice the array out of bounds.

The code is as follows:

#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;}

Codeforces 347B Fixed Points (water problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.