Codeforces Global Round 13A. K-th Largest Value

A. K-th Largest Value

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers. Initially all elements of a are either 0 or 1. You need to process q queries of two kinds:

1 x : Assign to ax the value 1−ax.
2 k : Print the k-th largest value of the array.
As a reminder, k-th largest value of the array b is defined as following:

Sort the array in the non-increasing order, return k-th element from it.
For example, the second largest element in array [0,1,0,1] is 1, as after sorting in non-increasing order it becomes [1,1,0,0], and the second element in this array is equal to 1.
翻译:
为您提供了一个数组a包括n整数。最初所有元素a要么是0或1.您需要处理q次查询两种类型:

1 x : 分配给 ax 值 1+ax.
2 k : 打印第 k 大的数值。
作为提醒,K-阵列的最大值B定义如下:

用不递增的顺序对数组进行排序,返回第k大的元素。
例如,阵列中的第二大元素[0,1,0,1]是1,因为在非增加的顺序排序后,它成为[1,1,0,0],此阵列中的第二个元素等于1.

Input

The first line contains two integers n and q (1≤n,q≤105) — the length of the given array and the number of queries.

The second line contains n integers a1,a2,a3,…,an (0≤ai≤1) — elements of the initial array.

Each of the following q lines contains two integers. The first integer is t (1≤t≤2) — the type of query.

If t=1 the second integer is x (1≤x≤n) — the position of the modified number. You have to assign to ax the value 1−ax.
If t=2 the second integer is k (1≤k≤n) — you need to print the k-th largest value of the array.
It’s guaranteed that there will be at least one query of the second type (satisfying t=2).
翻译:
第一行包含两个整数n和q (1≤n,q≤105)-给定数组的长度和查询数量。

第二行包含n整数a1,a2,a3…,an (0≤ai≤1)-初始阵列的元素。

以下每个问行包含两个整数。第一个整数是t (1≤t≤2)-查询的类型。

扫描二维码关注公众号,回复: 12611593 查看本文章

如果t=1第二个整数是x (1≤x≤n)-修改后的号码的位置。您必须分配给 ax 值 1+ax.
如果t=2第二个整数是K (1≤k≤n)-您需要打印第 k 大的数值。
保证第二种类型至少有一个查询(满意 t=2).

Output

For each query of the second type, print a single integer — the answer to the query.

翻译:对于第二种类型的每个查询,打印一个整数-查询的答案

Example

inputCopy

5 5
1 1 0 1 0
2 3
1 2
2 3
2 1
2 5

outputCopy

1
0
1
0

Note

Initially a=[1,1,0,1,0].

The first operation is printing the third largest value, which is 1.

The second operation is assigning a2 the value 0, a becomes [1,0,0,1,0].

The third operation is printing the third largest value, it is 0.

The fourth operation is printing the first largest value, it is 1.

The last operation is printing the fifth largest value, it is 0.

解题思路

1、按题目输入各变量,特殊之处就是数组中的数值要么是 1 要么是 0 ,
2、用 num 来记录数组中 1 的个数,
当 t=1 时,只改变数组的数值,无输出;
当 t=2 s时,根据 num和x 的比较 分别进行不一样的输出
在这里插入图片描述

参考代码

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    
    
	int n,q;
	cin>>n>>q;
	
	int a[n+1],num=0;
	for(int i=1;i<=n;i++){
    
    
		cin>>a[i];
		if(a[i])
			num++;//num用来统计数组中1的个数
	}
			
	int t,x;
	for(int i=0;i<q;i++){
    
    
		cin>>t>>x;
		if(t==1){
    
    
			if(a[x]){
    
    
				a[x]=0;
				num--;
			}else{
    
    
				a[x]=1;
				num++;
			}
		}else{
    
    
			if(x<=num)
				cout<<"1"<<endl;
			else
				cout<<"0"<<endl;
		}
	}
	return 0;
}

没明白的话,欢迎来打扰;
学会了的话,留下一个赞,互相鼓励哦!!!

猜你喜欢

转载自blog.csdn.net/weixin_45950429/article/details/114259703