Codeforces Round #500 (Div. 2)

A - Piles With Stones
There is a beautiful garden of stones in Innopolis.

Its most beautiful place is the n piles with stones numbered from 1 to n.

EJOI participants have visited this place twice.

When they first visited it, the number of stones in piles was x1,x2,…,xn, correspondingly. One of the participants wrote down this sequence in a notebook.

They visited it again the following day, and the number of stones in piles was equal to y1,y2,…,yn. One of the participants also wrote it down in a notebook.

It is well known that every member of the EJOI jury during the night either sits in the room 108 or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night.

Participants want to know whether their notes can be correct or they are sure to have made a mistake.

Input
The first line of the input file contains a single integer n, the number of piles with stones in the garden (1≤n≤50).

The second line contains n integers separated by spaces x1,x2,…,xn, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the first time (0≤xi≤1000).

The third line contains n integers separated by spaces y1,y2,…,yn, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the second time (0≤yi≤1000).

Output
If the records can be consistent output “Yes”, otherwise output “No” (quotes for clarity).

Examples
Input
5
1 2 3 4 5
2 1 4 3 5
Output
Yes
Input
5
1 1 1 1 1
1 0 1 0 1
Output
Yes
Input
3
2 3 9
1 7 9
Output
No
Note
In the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile.

In the second example, the jury took stones from the second and fourth piles.

It can be proved that it is impossible for the jury members to move and took stones to convert the first array into the second array.

只要不变多其他情况都可以

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 2e3 + 100;
const int M = N * N;
const int mod = 2333;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);


signed main(){
    
    
	int n;
	gt(n);
	
	int ans1 = 0, ans2 = 0;
	
	for (int i = 1; i <= n; i ++){
    
    
		int x;
		scanf("%lld", &x);
		ans1 += x;
	}
	
	for (int i = 1; i <= n; i ++){
    
    
		int x;
		gt(x);
		ans2 += x;
	}
	
	if (ans1 >= ans2)   cout << "Yes" << endl;
	else cout << "No" << endl;
	
	return 0;
}

 

And

There is an array with n elements a1, a2, …, an and the number x.

In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.

You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.

Input
The first line contains integers n and x (2 ≤ n ≤ 100 000, 1 ≤ x ≤ 100 000), number of elements in the array and the number to and with.

The second line contains n integers ai (1 ≤ ai ≤ 100 000), the elements of the array.

Output
Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.

Examples
Input
4 3
1 2 3 7
Output
1
Input
2 228
1 1
Output
0
Input
3 7
1 2 3
Output
-1
Note
In the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.

In the second example the array already has two equal elements.

In the third example applying the operation won’t change the array at all, so it is impossible to make some pair of elements equal.

判断一个数存在不存在,桶排序和set的标记都是好办法

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e5 + 10;
const int M = N * N;
const int mod = 2333;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

set<int> ss, sss;
int a[N];

signed main(){
    
    
	int n, x;
	gt(n);
	gt(x);
	
	bool flag = false;
	for (int i = 1; i <= n; i ++){
    
    
		scanf("%lld", &a[i]);
		if (ss.count(a[i]) && !flag){
    
    
			cout << "0" << endl;
			flag = true;
		}
		ss.insert(a[i]);
	}
	
	if (!flag){
    
    
		bool flag1 = false;
		for (int i = 1; i <= n; i ++){
    
    
			int temp = (a[i] & x);
			if (ss.count(temp) && (a[i] != temp)){
    
    
			//	cout << temp << endl;
				cout << "1" << endl;
				return 0;
			}
			if (sss.count(temp)){
    
    
				flag1 = true;
			}
			sss.insert(temp);
		}
		
		if (flag1){
    
    
			cout << "2" << endl;
		}
		else  cout << "-1" << endl;
	}
	
	return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e5 + 10;
const int M = N * N;
const int mod = 2333;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

set<int> ss, sss;
int a[N], cnt[N];

signed main(){
    
    
	int n, x;
	gt(n);
	gt(x);
	
	int ans = -1;
	for (int i = 1; i <= n; i ++){
    
    
		int k;
		scanf("%lld", &k);
		
		if (a[k] != 0){
    
    
			ans = 0;
		}
		
		if (ans != 0 && (a[k & x] || cnt[k])){
    
    
			ans = 1;
		}
		
		if (ans != 0 && ans != 1 && cnt[k & x]){
    
    
			ans = 2;
		}
		
		a[k] ++;
		cnt[k & x] ++;
	}
	
	cout << ans << endl;
	
	return 0;
}

Photo of The Sky
Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes.

Strictly speaking, it makes a photo of all points with coordinates (x,y), such that x1≤x≤x2 and y1≤y≤y2, where (x1,y1) and (x2,y2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero.

After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky.

Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it’s impossible to tell which numbers specify coordinates of which points.

Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle.

Input
The first line of the input contains an only integer n (1≤n≤100000), the number of points in Pavel’s records.

The second line contains 2⋅n integers a1, a2, …, a2⋅n (1≤ai≤109), coordinates, written by Pavel in some order.

Output
Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel’s records.

Examples
Input
4
4 1 3 2 3 2 1 3
Output
1
Input
3
5 8 5 5 7 5
Output
0
Note
In the first sample stars in Pavel’s records can be (1,3), (1,3), (2,3), (2,4). In this case, the minimal area of the rectangle, which contains all these points is 1 (rectangle with corners at (1,3) and (2,4)).

一个正方形的面积最小,面积是两个数的乘积,可以让这两个数都尽可能地小,或者是让一个数最大,另外一个数尽可能的小,这些都是可能满足的结果,在这些值种取一个最小值就可以了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 2e5 + 10;
const int M = N * N;
const int mod = 2333;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int a[N];

map<int, int> mp;

signed main(){
    
    
	int n;
	gt(n);
	
	bool flag = false;
	
	for (int i = 1; i <= 2 * n; i ++){
    
    
		scanf("%lld", &a[i]);
		mp[a[i]] ++;
		if (mp[a[i]] >= n && (!flag)){
    
    
			cout << "0" << endl;
			flag = true;
		}
	}
	
	int ans = 0x7f7f7f7f7f7f7f7f;
	if (!flag){
    
    
		sort(a + 1, a + 1 + n * 2);
		 ans = (a[n] - a[1]) * (a[2 * n] - a[n + 1]);
		 
	//	cout << ans << endl;
		for(int i = 2;i <= n; i++){
    
    
		int t;
		t = (a[2 * n ] - a[1]) * (a[n + i - 1] - a[i]);
		ans = min(ans, t);
	}
	
	cout << ans << endl;
	}
	
	
	
	return 0;
}

 

D - Chemical table
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.

Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).

Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.

Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.

Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.

The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.

Output
Print the minimal number of elements to be purchased.

Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.

The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.

The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.

Test 1

We can use nuclear fusion and get the element from three other samples, so we don’t need to purchase anything.

Test 2

We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.

Test 3

There are several possible solutions. One of them is illustrated below.

Note that after purchasing one element marked as red it’s still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.

极其巧妙地转化,如果三个正方形状的方形被标记了,那么剩下那个也被标记了,如果按照每行每列为一个集合的话,很自然的下边那一行和右边那一列是一个集合的,每个点的坐标与他们行和他的列有关,最后需要加几个点,即需要加几条边,让所有的行列在一个集合里

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 4e5 + 10;
const int M = N * N;
const int mod = 2333;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int p[N]; 

int find(int x){
    
    
	if (p[x] != x)   p[x] = find(p[x]);
	return p[x];   
}

signed main(){
    
     
	int n, m, q;
	scanf("%lld%lld%lld", &n, &m, &q);
	
	for (int i = 1; i <= (n + m); i ++)   p[i] = i;
	
	int cnt = 0;
	while(q --){
    
    
		int x, y;
		scanf("%lld%lld", &x, &y);
		int px = find(x), py = find(y + n);
		if (px != py){
    
    
			p[px] = py;
			cnt ++;
		}
		  
	}
	
	cout << (n + m - 1) - cnt << endl;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/112913208