1855A - Dalton the Teacher - CodeForces Solution


math

Please click on ads to support us..

Python Code:

t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    s=0
    for i in range(n):
        if l[i]==i+1:
            s+=1
    if s%2==0:
        print(s//2)
    else:
        print((s//2)+1)

C++ Code:

// Author: Tushar Khanduri
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cmath>
#include <string>
#include <bitset>
#include <algorithm>
#define int long long
#define endl "\n"
#define pb push_back
#define vec vector
using namespace std;

//==============================================================================
class Math
{
public:
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    int lcm(int a, int b)
    {
        return a * (b / gcd(a, b));
    }

    void set_sieve(vector<bool> &sieve, int n)
    {
        for (int i = 2; i * i <= n; i++)
            if (sieve[i])
                for (int j = i * i; j <= n; j += i)
                    sieve[j] = false;
    }

    int pow(int x, int n)
    {
        int ans = 1;
        while (n > 0)
        {
            if (n & 1)
                ans *= x;
            x *= x;
            n = n >> 1;
        }
        return ans;
    }

    int pow(int x, int n, int m)
    {
        int ans = 1;
        while (n > 0)
        {
            if (n & 1)
                ans = (ans * x) % m;
            x = (x * x) % m;
            n = n >> 1;
        }
        return ans;
    }

    int modinv(int x, int p)
    {
        return pow(x, p - 2);
    }

} Math;
//=============================================================================

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        cnt += (a[i] == (i + 1));
    }
    cout << (cnt + 1) / 2 << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
1 Comments
  • 12/8/2023 12:23 - Asia/Shanghai

#include <iostream>
using namespace std;

int main(){
    int t,n,x,y = 0;
    cin >> t;
    while(t--){
        cin >> n;
        for(int j = 1;j <= n;j++){
            cin >> x;
            if(x == j) y++;
        }
        cout << (y + 1) / 2 << endl;
    }
    return 0;
}


More Questions

709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers