首页 > 其他分享 >Codeforces Round 983 (div 2)

Codeforces Round 983 (div 2)

时间:2024-11-16 09:18:10浏览次数:1  
标签:node le int 983 Codeforces ldots test div cases

A. Circuit

Alice has just crafted a circuit with \(n\) lights and \(2n\) switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:

  • Each light is connected to exactly two switches.
  • Each switch is connected to exactly one light. It's unknown which light each switch is connected to.
  • When all switches are off, all lights are also off.
  • If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.

Alice brings the circuit, which shows only the states of the \(2n\) switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?

Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) — the number of lights in the circuit.

The second line of each test case contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(0 \le a_i \le 1\)) — the states of the switches in the circuit. \(a_i = 0\) means the \(i\)-th switch is off, and \(a_i = 1\) means the \(i\)-th switch is on.

Output

For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.

大致思路: 由题意知开关状态为 \([0,1]\) 时,灯的状态为 \(on\),开关为 \([1,1] , [0,0]\) 时,灯的状态为 \(off\) . 所以统计数组中 0 和 1 的个数,记作 \((a, b)\). 则若求最小值,则尽可能地让 \([0,0],[1,1]\) 配对,若求最大值,则尽可能让 \([0,1]\) 配对。则可以得到如下判断:

①若 \(cnt0\) 为奇数,则 \(res_{min} = 1, res_{max} = min(a,b)\)
②若 \(cnt0\) 为偶数,则 \(res_{min} = 0, res_{max} = min(a,b)\)

代码如下

#include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5+5;
int n;
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
int main()
{
	fio();
	int t; cin >> t;
	while(t--)
	{
		int a=0,b=0;
		cin >> n;
		for(int i=1;i<=2*n;i++)
		{
			int num;
			cin >> num;
			if(num)a ++;
			else b ++;
		}
		if(a % 2 == 0)cout << 0 << " "<< min(a,b)<<endl;
		else cout << 1 <<" "<<min(a,b) << endl;
	}
}

B. Medians

You are given an array \(a = [1, 2, \ldots, n]\), where \(n\) is odd, and an integer \(k\).

Your task is to choose an odd positive integer \(m\) and to split \(a\) into \(m\) subarrays\(^{\dagger}\) \(b_1, b_2, \ldots, b_m\) such that:

  • Each element of the array \(a\) belongs to exactly one subarray.
  • For all \(1 \le i \le m\), \(|b_i|\) is odd, i.e., the length of each subarray is odd.
  • \(\operatorname{median}([\operatorname{median}(b_1), \operatorname{median}(b_2), \ldots, \operatorname{median}(b_m)]) = k\), i.e., the median\(^{\ddagger}\) of the array of medians of all subarrays must equal \(k\). \(\operatorname{median}(c)\) denotes the median of the array \(c\).

\(^{\dagger}\)A subarray of the array \(a\) of length \(n\) is the array \([a_l, a_{l + 1}, \ldots, a_r]\) for some integers \(1 \le l \le r \le n\).

\(^{\ddagger}\)A median of the array of odd length is the middle element after the array is sorted in non-decreasing order. For example: \(\operatorname{median}([1,2,5,4,3]) = 3\), \(\operatorname{median}([3,2,1]) = 2\), \(\operatorname{median}([2,1,2,1,2,2,2]) = 2\).

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n < 2 \cdot 10^5\), \(n\) is odd) — the length of array \(a\) and the desired median of the array of medians of all subarrays.

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case:

  • If there is no suitable partition, output \(-1\) in a single line.
  • Otherwise, in the first line, output an odd integer \(m\) (\(1 \le m \le n\)), and in the second line, output \(m\) distinct integers \(p_1, p_2 , p_3 , ..., p_m\) (\(1 = p_1 <p_2 < p_3 < ... < p_m \le n\)) — denoting the left borders of each subarray.

In detail, for a valid answer \([p_1, p_2, \ldots, p_m]\):

  • \(b_1 = \left[ a_{p_1}, a_{p_1 + 1}, \ldots, a_{p_2 - 1} \right]\)
  • \(b_2 = \left[ a_{p_2}, a_{p_2 + 1}, \ldots, a_{p_3 - 1} \right]\)
  • \(\ldots\)
  • \(b_m = \left[ a_{p_m}, a_{p_m + 1}, \ldots, a_n \right]\).

If there are multiple solutions, you can output any of them.

大致思路:易知,我们可以只用三个子数列便可以得到最终的答案,最简单的答案便是\([1,k-1]\) , \([k]\) , \([k+1,n]\) . 但是由于子数列的个数必须满足是奇数,而 \(n\) 也为奇数。故需要讨论 \(k\) 的奇偶性。

若 \(k\) 为偶数,则子数列 \([1,k-1]\) , \([k+1,n]\) 的元素个数都为奇数,故输出$ 1,k,k+1 $.
若 \(k\) 为奇数,则子数列 \([1,k-1]\) , \([k+1,n]\) 的元素个数都为偶数,而子数列 \([1,k-2]\) , \([k+2,n]\) 的元素个数都为奇数,故输出$ 1,k-1,k+2 $.

代码如下

#include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5+5;
int n,k;
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
int main()
{
	fio();
	int t; cin >> t;
	while(t--)
	{
		cin >> n >> k;
		if(n == 1 && k == 1)cout << 1 <<endl<<1<<endl;
		else if (k == 1 || k == n)cout << -1 << endl;
		else if(k % 2)
		{
			cout << 3 <<endl;
			cout << 1 <<" "<< k-1 <<" "<< k + 2<<endl;
		}
		else 
		{
			cout << 3 <<endl;
			cout << 1 <<" "<< k <<" "<< k + 1<<endl;
		}

	}
}

C. Trinity

You are given an array \(a\) of \(n\) elements \(a_1, a_2, \ldots, a_n\).

You can perform the following operation any number (possibly \(0\)) of times:

  • Choose two integers \(i\) and \(j\), where \(1 \le i, j \le n\), and assign \(a_i := a_j\).

Find the minimum number of operations required to make the array \(a\) satisfy the condition:

  • For every pairwise distinct triplet of indices \((x, y, z)\) (\(1 \le x, y, z \le n\), \(x \ne y\), \(y \ne z\), \(x \ne z\)), there exists a non-degenerate triangle with side lengths \(a_x\), \(a_y\) and \(a_z\), i.e. \(a_x + a_y > a_z\), \(a_y + a_z> a_x\) and \(a_z + a_x > a_y\).

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) — the number of elements in the array \(a\).

The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the elements of the array \(a\).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case, output a single integer — the minimum number of operations required.

大致思路:由题意,本题仅与元素的大小有关,而与元素的顺序无关。故先对 \(a_n\) 进行从小到大排序。要求对任意的 \(a_n\), 均满足 \(a_x + a_y > a_z,x < y < z\) , 求最小的 \(operation\). 考虑如果固定\(a_x, a_y\),最大的\(a_z = a_x +a_y - 1\)。利用二分可以求出 \(\ a_z\) 索引 $index $ 则 \(operation=\) $ n - (index - i + 1)$。则 \(res = min(res, operations)\)

代码如下:

# include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5 + 5;
int n;
void fio() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
int a[N];
int main() {
	fio();
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		for (int i = 1; i <= n; i++)cin >> a[i];
		sort(a + 1, a + n + 1);
		int  res = n;
		for (int i = 1; i <= n - 1; i++) {
			int sum = a[i] + a[i + 1];
			int index = lower_bound(a + i + 1, a + 1 + n, sum) - a - 1;
			res = min(res, n - (index - i + 1));
		}
		cout << res << endl;
	}
}

D. Genokraken

This is an interactive problem.

Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies.

The monster's nerve system can be structured as a tree\(^{\dagger}\) of \(n\) nodes (really, everything should stop resembling trees all the time\(\ldots\)), numbered from \(0\) to \(n-1\), with node \(0\) as the root.

Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values \(p_1, p_2, < , p_{n-1}\) of the tree, where \(p_i\) (\(0 \le p_i < i\)) is the direct parent node of node \(i\) (\(1 \le i \le n - 1\)).

She doesn't know exactly how the nodes are placed, but she knows a few convenient facts:

  • If we remove root node \(0\) and all adjacent edges, this tree will turn into a forest consisting of only paths\(^{\ddagger}\). Each node that was initially adjacent to the node \(0\) will be the end of some path.
  • The nodes are indexed in a way that if \(1 \le x \le y \le n - 1\), then \(p_x \le p_y\).
  • Node \(1\) has exactly two adjacent nodes (including the node \(0\)).
The tree in this picture does not satisfy the condition, because if we remove node \(0\), then node \(2\) (which was initially adjacent to the node \(0\)) will not be the end of the path \(4-2-5\). The tree in this picture does not satisfy the condition, because \(p_3 \le p_4\) must hold. The tree in this picture does not satisfy the condition, because node \(1\) has only one adjacent node.

Gretel can make queries to the containment cell:

  • "? a b" (\(1 \le a, b < n\), \(a \ne b\)) — the cell will check if the simple path between nodes \(a\) and \(b\) contains the node \(0\).

However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most \(2n - 6\) times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand?

\(^{\dagger}\)A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them.

\(^{\ddagger}\)A path is a tree whose vertices can be listed in the order \(v_1, v_2, \ldots, v_k\) such that the edges are \((v_i, v_{i+1})\) (\(1 \le i < k\)).

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer \(n\) (\(4 \le n \le 10^4\)) — the number of nodes in Genokraken's nerve system.

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).

Interaction

For each test case, interaction starts by reading the integer \(n\).

Then you can make queries of the following type:

  • "? a b" (without quotes) (\(1 \le a, b < n\), \(a \ne b\)).

After the query, read an integer \(r\) — the answer to your query. You are allowed to use at most \(2n - 6\) queries of this type.

  • If the simple path between nodes \(a\) and \(b\) does not contain node \(0\), you will get \(r = 0\).
  • If the simple path between nodes \(a\) and \(b\) contains node \(0\), you will get \(r = 1\).
  • In case you make more than \(2n-6\) queries or make an invalid query, you will get \(r = -1\). You will need to terminate after this to get the "Wrong answer" verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you find out the structure, output a line in the format "! \(p_1 \space p_2 \ldots p_{n-1}\)" (without quotes), where \(p_i\) (\(0 \le p_i < i\)) denotes the index of the direct parent of node \(i\). This query is not counted towards the \(2n - 6\) queries limit.

After solving one test case, the program should immediately move on to the next one. After solving all test cases, the program should be terminated immediately.

After printing any query do not forget to output an end of line and flush the output buffer. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

The interactor is non-adaptive. The tree does not change during the interaction.

大致题意:一个树状结构,其满足如下条件:

  • 除了 \(0\) 节点外,其余节点只有一个子节点。
  • \(1\) 节点一定存在子节点。
  • 若节点 \(i < j\),则 \(p_i < p_j\),\(p_i\) 表示为 \(i\) 的父节点。

交互操作为:

  • \(? \;\;a \;\;b\):表示询问 \(a\) 与 \(b\) 之间是否存在节点 \(0\), \(1\) 表示存在, 0表示不存在。最多交互 \(2n - 6\) 次.

最后依次输出 \(p[1], p[2],...,p[n]\).

大致思路

  • 记数组 \(f[i]\) 为节点 \(i\) 的父节点,本题输出结果便是\(f[1],f[2]...,f[n]\).
  • 由树的结构可以得出每一层的节点从左到右依次为:\(1,2,3,4....n\)
  • 对于有序询问 "\(?\;a\;b\,(a<b)\)",从 \(a=1,b=2\) 开始,如果返回值为 \(1\),则表示节点 \(a\) 无子节点,\(a=a+1\)。如果返回值为 \(0\),则表示节点 \(a\) 为 \(b\) 的父节点,则 \(f[b] = a\),\(a=a+1\), \(b=b+1\).
  • \(a = 1\) 时,"\(?\) \(a\) \(b\) \(= 1\)"时,\(0\) 节点便会多一个子节点,直到 \(?\; a\; \;b =0\).

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e4 + 5;
int n;
void fio() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
int f[N];
int main() {
	fio();
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		memset(f, 0, sizeof f);
		int b = 2;
		int op ;
		n -= 1;
		int a = 1;
		while(a <= n - 1){
			if (b > n)break;
			cout << "? " << a << " " << b << endl;
			cin >> op;
			if (op == 0) {
				f[b] = a;
				b ++ ;
				a ++ ;
			}
			else if (op == 1 && a == 1)b ++; 
			else a ++;
		}
		cout << "! ";
		for (int i = 1; i <= n; i++)cout << f[i] << " ";
		cout << endl;
	}
	return 0;
}

标签:node,le,int,983,Codeforces,ldots,test,div,cases
From: https://www.cnblogs.com/LiangSue/p/18548561

相关文章

  • Solution - Codeforces 2031F Penchick and Even Medians
    飞快秒掉了,没报名痛失首杀,痛苦。简略题解:考虑先随机二元下标\((x_0,y_0)\)满足删去\((x_0,y_0)\)后查询的中位数还是\(\frac{n}{2},\frac{n}{2}+1\),那么这就说明\(p_{x_0},p_{y_0}\)一定在中位数的两边。那么还剩下的\(n-2\)个下标两两配对成\(\frac{n-2}{......
  • 2024.11.15 Codeforces Round 987(Div. 2)
    Solved:5/6Rank:74比赛链接A.PenchickandModernMonument给定一个不增序列,修改最少的数字使其不降。全都修改为出现次数最多的数即可。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;voidsolve(){intn;cin>>n;vector<int>a(n);......
  • 第九章 DIV+CSS布局
    9.1DIV+CSS概述9.1.1认识DIV<div>标签是HTML中的一种块级元素,用于定义文档中的一个分区或区域。它是一个容器,可以包含文本、图像、列表、表格、段落、其他块级元素,甚至是其他<div>元素。<div>标签本身不会在页面上显示任何内容,但它可以作为组织和布局HTML文档的工具9.......
  • 第九章 DIV+CSS布局
    9.1DIV+CSS概述 DIV+CSS是Web设计标准,它是一种网页的布局方法。与传统中通过表格(table)布局定位的方式不同,它可以实现网页页面内容与表现相分离。DIV组成了网页的格局,CSS则装饰了格局,比如建一栋房子,开始的架子是DIV,架子搭建好后开始装饰,这个装饰就是CSS样式。用DIV+CSS布......
  • 第9章DIV+CSS布局
    9.1DIV+CSS概述9.1.1认识DIV无特殊作用,一个盒子9.1.2DIV的宽高设置9.1.2.1宽高属性宽度:width高度:height单位:可使用像素或者百分比9.1.2.2div标签内直接设置宽高使用style属性设置宽高9.1.2.3div使用选择器设置宽高可使用class,id等选择器9.1.2.4div高度的百......
  • Solution - Codeforces 1725K Kingdom of Criticism
    首先考虑转化一下操作\(3\)。令\(m=\lfloor\frac{l+r}{2}\rfloor\),操作\(3\)就相当于是在\([l,m]\)内的数变为\(l-1\),在\((m,r]\)内的数变为\(r+1\)。于是现在对于操作\(3\)其实就是将一个区间内的数都转为同一个值。其实对于这类将大量信息整合为一体的......
  • Codeforces Round 986 (Div. 2)
    AB没什么好说的。C把我卡了。dp非常明显,最开始我想的是单向做,\(f[i][0/1]\)表示前\(i\)块蛋糕已经分出去了,01表示Alice是否拿过了,此时分给了几个人。尝试写写转移就知道为什么寄了。状态不够,没法表示答案。就算转移到了最后也没法得出我们需要的答案。可以发现,这个dp不好做的......
  • 第九章 DIV+CSS布局
    9.1DIV+CSS概述9.1.1认识DIVdiv标签在Web标准的网页中使用非常频繁,属于块状元素。div标签是双标签,即以<div></div>的形式存在,中间可以放置任何内容,包括其他的div标签。但是在没有CSS的影响下,每个div标签只占据一行,即一行只能容纳一个div标签。9.1.2DIV的宽高设置对div......
  • Solution - Codeforces 1681E Labyrinth Adventures
    能够发现这个最短路的形态一定是从低层一层一层走到高层的。那么这就说明若起点终点对应层数为\(x,y\)。若\(x=y\)则直接算,就是曼哈顿距离。否则不妨钦定\(x<y\)(不满足就交换,不影响结果),那么层数\(z\in[x,y)\)的其中一个门肯定都会被经过。于是考虑把\(\operator......
  • Solution - Codeforces 1190C Tokitsukaze and Duel
    考虑到两人对应的操作是相同的,于是可以从对称的角度来思考。考虑到在先手做出操作后,后手一个较为特殊的操作是不做任何影响,也就是重复先手的操作。能够发现如果对于后手这不能必胜,那么他一定不会去产生影响,并又把这个局面留给先手,相当于是先后手的交换。对于先手又是同样的,因为......