結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー nebukuro09nebukuro09
提出日時 2018-10-05 21:48:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,012 ms / 2,500 ms
コード長 1,459 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 200,624 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-02 17:49:35
合計ジャッジ時間 4,329 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1,012 ms
4,380 KB
testcase_12 AC 429 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)
typedef long long ll;
typedef long double ld;

const ll MOD = 1000000007;
int N, K;
pair<int, int> LR[30303];
pair<int, int> RL[30303];
int same[30303];

void solve() {
    int N; cin >> N;
    int x = 0, y = 0, z = 0;
    REP2(i, 1, N+1) {
        int m; cin >> m;
        if (i < m) LR[x++] = make_pair(i, m);
        else if (m < i) RL[y++] = make_pair(m, i);
        else same[z++] = i;
    }

    int ans = 0;

    REP(i, x) REP2(j, i+1, x) {
        if (LR[i].first <= LR[j].first && LR[j].second <= LR[i].second)
            ans++;
        else if (LR[j].first <= LR[i].first && LR[i].second <= LR[j].second)
            ans++;
    }

    REP(i, y) REP2(j, i+1, y) {
        if (RL[i].first <= RL[j].first && RL[j].second <= RL[i].second)
            ans++;
        else if (RL[j].first <= RL[i].first && RL[i].second <= RL[j].second)
            ans++;
    }

    REP(i, x) REP(j, z) {
        if (LR[i].first <= same[j] && same[j] <= LR[i].second)
            ans++;
    }

    REP(i, y) REP(j, z) {
        if (RL[i].first <= same[j] && same[j] <= RL[i].second)
            ans++;
    }

    REP(i, x) REP(j, y) {
        if (LR[i].first <= RL[j].second && LR[i].second >= RL[j].first)
            ans++;
    }

    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    solve();
}
0