1491A - K-th Largest Value - CodeForces Solution


brute force greedy implementation *800

Please click on ads to support us..

Python Code:

n,m=map(int,input().split())
l=list(map(int,input().split()))
c=l.count(1)
for pratyush in range(m):
    a,b=map(int,input().split())
    if a==1:
        l[b-1]=1-l[b-1]
        c+=(1 if l[b-1] else -1)
    else:
        print(1 if b<=c else 0)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin >> n >> q;
    int flag[2] = {0};
    int a[n];
    for(int i = 0; i < n; i++)
    {
         cin >> a[i];
         flag[a[i]]++;
    }
    while(q--)
    {
        int x,k;
        cin >> x >> k;
        if(x == 1)
        {
            if(a[k-1] == 1)
            {
                a[k-1] = 0;
                flag[0]++;
                flag[1]--;
            }
            else
            {
                a[k-1] = 1;
                flag[0]--;
                flag[1]++;
            }
        }
        else
         (k <= flag[1])? cout << 1 << endl : cout << 0 << endl;
    }
}


Comments

Submit
0 Comments
More Questions

Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes