Educational Codeforces Round 45 (Rated for Div. 2)题解(a-e)

A. Commentary Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland Football Cup starts really soon! Commentators from all over the world come to the event.

Organizers have already built nn commentary boxes. mm regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.

If nn is not divisible by mm, it is impossible to distribute the boxes to the delegations at the moment.

Organizers can build a new commentary box paying aa burles and demolish a commentary box paying bb burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.

What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm)?

Input

The only line contains four integer numbers nn, mm, aa and bb (1n,m10121≤n,m≤1012, 1a,b1001≤a,b≤100), where nn is the initial number of the commentary boxes, mm is the number of delegations to come, aa is the fee to build a box and bb is the fee to demolish a box.

Output

Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm). It is allowed that the final number of the boxes is equal to 00.

Examples
input
Copy
9 7 3 8
output
Copy
15
input
Copy
2 7 3 7
output
Copy
14
input
Copy
30 6 17 19
output
Copy
0
Note

In the first example organizers can build 55 boxes to make the total of 1414 paying 33 burles for the each of them.

In the second example organizers can demolish 22 boxes to make the total of 00 paying 77 burles for the each of them.

In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 55 boxes.

#include 
#define ll long long
using namespace std;
int main()
{
    ll n,m,a,b;
    cin>>n>>m>>a>>b;//a -> +
    if(n
模拟,注意n m的时候往两边两种做法比较一下就ok

B. Micro-World
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.

You know that you have nn bacteria in the Petri dish and size of the ii-th bacteria is aiai. Also you know intergalactic positive integer constant KK.

The ii-th bacteria can swallow the jj-th bacteria if and only if ai>ajai>aj and aiaj+Kai≤aj+K. The jj-th bacteria disappear, but the ii-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria ii can swallow any bacteria jjif ai>ajai>aj and aiaj+Kai≤aj+K. The swallow operations go one after another.

For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]a=[101,53,42,102,101,55,54] and K=1K=1. The one of possible sequences of swallows is: [101,53,42,102,101––,55,54][101,53,42,102,101_,55,54]  [101,53,42,102,55,54][101,53_,42,102,55,54]  [101––,42,102,55,54][101_,42,102,55,54]  [42,102,55,54][42,102,55,54_]  [42,102,55][42,102,55]. In total there are 33 bacteria remained in the Petri dish.

Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.

Input

The first line contains two space separated positive integers nn and KK (1n21051≤n≤2⋅105, 1K1061≤K≤106) — number of bacteria and intergalactic constant KK.

The second line contains nn space separated integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106) — sizes of bacteria you have.

Output

Print the only integer — minimal possible number of bacteria can remain.

Examples
input
Copy
7 1
101 53 42 102 101 55 54
output
Copy
3
input
Copy
6 5
20 15 10 15 20 25
output
Copy
1
input
Copy
7 1000000
1 1 1 1 1 1 1
output
Copy
7
Note

The first example is clarified in the problem statement.

In the second example an optimal possible sequence of swallows is: [20,15,10,15,20,25][20,15,10,15,20_,25]  [20,15,10,15,25][20,15,10,15_,25]  [20,15,10,25][20,15,10_,25]  [20,15,25][20,15_,25]  [20,25][20_,25]  [25][25].

In the third example no bacteria can swallow any other bacteria.


模拟 stl乱搞 这里用map当作一个计数set用,second变量统计first出现了几次。
根据题意推一下aj的范围是 [ai-k,ai)  lower_ bound找一下,用erase的第三种用法,删除这个区间的数据即可
最后算结果的时候要乘上first出现的次数,就是second
stl真好用(笑)



#include 
#include 
#include 
using namespace std;
#define ll long long
maps;
map::iterator iter;
map::iterator iter1;
//set::iterator left;
//set::iterator right;
//int a[200005];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n,k;
	int t;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>t;
		s[t]++;
	}
	map::iterator left;
	map::iterator right;
	for(iter=s.begin();iter!=s.end();iter++)
	{
		left=s.lower_bound(iter->first-k);//第一个大于等于的
		//cout<first<<' ';
		right=iter;
		//cout<second);
	}

	cout<


C. Bracket Sequences Concatenation Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A bracket sequence is a string containing only characters "(" and ")".

A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

You are given nn bracket sequences s1,s2,,sns1,s2,…,sn. Calculate the number of pairs i,j(1i,jn)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence. Operation ++ means concatenation i.e. "()(" + ")()" = "()()()".

If si+sjsi+sj and sj+sisj+si are regular bracket sequences and iji≠j, then both pairs (i,j)(i,j) and (j,i)(j,i) must be counted in the answer. Also, if si+sisi+si is a regular bracket sequence, the pair (i,i)(i,i) must be counted in the answer.

Input

The first line contains one integer n(1n3105)n(1≤n≤3⋅105) — the number of bracket sequences. The following nn lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed 31053⋅105.

Output

In the single line print a single integer — the number of pairs i,j(1i,jn)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence.

Examples
input
Copy
3
)
()
(
output
Copy
2
input
Copy
2
()
()
output
Copy
4
Note

In the first example, suitable pairs are (3,1)(3,1) and (2,2)(2,2).

In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)(1,1),(1,2),(2,1),(2,2).


#include
#include
#define maxn 300005
using namespace std;
int fl[maxn],fr[maxn];
int l[maxn],r[maxn];
long long ans = 0;
int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	string s;
	for(int i=1;i<=n;i++)
	{
		cin>>s;
		int x = 0,y = 0;//x记录左括号的数量,y记录右括号的数量
		for(int j=0;j
这题当时有点钻牛角尖了,stl乱搞失败,当时的想法是先找(),不断用erase的第二种用法 删除(),最后计数之后放进两个map里,复杂度大概O(nlog(n))  tle  gg
后来的想法直接数组模拟了,前面的操作差不多,记数模拟删除,后面的操作O(n)解决。
我们用fl和fr数组来存储当前出现的经过处理后的字符串的数量   记录当前字符串的情况,比如剩下((,那就fl[2]++ 。
假如当前这个字符串处理后剩下 ))),那么和它配对的就是(((,那么最终结果就要加上 (1*fl[3]),并且把fr[3]++;

ps:这种做法可以不用考虑括号平衡的情况,含在里面了。


D. Graph And Its Complement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given three numbers n,a,bn,a,b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to aa, and the number of components in its complement is bb. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.

In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.

The adjacency matrix of an undirected graph is a square matrix of size nn consisting only of "0" and "1", where nn is the number of vertices of the graph and the ii-th row and the ii-th column correspond to the ii-th vertex of the graph. The cell (i,j)(i,j) of the adjacency matrix contains 11 if and only if the ii-th and jj-th vertices in the graph are connected by an edge.

A connected component is a set of vertices XX such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to XX violates this rule.

The complement or inverse of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are not adjacent in GG.

Input

In a single line, three numbers are given n,a,b(1n1000,1a,bn)n,a,b(1≤n≤1000,1≤a,b≤n): is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement. 

Output

If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).

Otherwise, on the first line, print "YES"(without quotes). In each of the next nn lines, output nn digits such that jj-th digit of ii-th line must be 11 if and only if there is an edge between vertices ii and jj in GG (and 00 otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes. 

If there are several matrices that satisfy the conditions — output any of them.

Examples
input
Copy
3 1 2
output
Copy
YES
001
001
110
input
Copy
3 3 3
output
Copy
NO


简单构造题,失策了,应该跳过c做d的。

题意:给你n,a,b n代表有n个顶点的图,原图中有a个连通块,它的补图中有b个连通块。(离散真好用)

画图推了个结论:除了n=1以外,其他的原图和补图间肯定存在一个必定全连通,因为完全图的威力是很大的。

继续往细里推:1<=n<=3的时候,a和b不可能全为1,其他情况可能出现全为1的情况

接下来就是构造了,假如a!=1 ,就需要构造a个连通块的图的邻接矩阵,最容易想到的情况是有(a-1)个孤立顶点,剩下的n-(a-1)个顶点连通,直接一条线连过去就ok,注意无向图邻接矩阵对称。

如果b!=1,我们就构造这个图的补图,方法同上,输出的时候用!输出其反,特判主对角线为0


#include 
#include
#include 
#include 
#define ll long long
#define maxn 300005
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);
	int n,a,b;
	cin>>n>>a>>b;
	bool map[n+1][n+1];
	memset(map,0,sizeof(map));
	if(a!=1&&b!=1)
	{
		cout<<"NO"<


E. Post Lamps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has nn positions to install lamps, they correspond to the integer numbers from 00 to n1n−1 on the OX axis. However, some positions are blocked and no post lamp can be placed there.

There are post lamps of different types which differ only by their power. When placed in position xx, post lamp of power ll illuminates the segment [x;x+l][x;x+l]. The power of each post lamp is always a positive integer number.

The post lamp shop provides an infinite amount of lamps of each type from power 11 to power kk. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power ll cost alal each.

What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment [0;n][0;n] of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power 33 in position n1n−1 (even though its illumination zone doesn't completely belong to segment [0;n][0;n]).

Input

The first line contains three integer numbers nn, mm and kk (1kn1061≤k≤n≤106, 0mn0≤m≤n) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available.

The second line contains mm integer numbers s1,s2,,sms1,s2,…,sm (0s1<s2<sm<n0≤s1) — the blocked positions.

The third line contains kk integer numbers a1,a2,,aka1,a2,…,ak (1ai1061≤ai≤106) — the costs of the post lamps.

Output

Print the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment [0;n][0;n] of the street.

If illumintaing the entire segment [0;n][0;n] is impossible, print -1.

题意:一条街道[0,n],第i个灯可以照亮[m,m+i] 这片区域(m任取),费用为ai,block点不能放灯。

贪心题,从0开始放,然后跳到下一个放灯点,如果这个点block了,就退到最近的没有被block的点,如果相隔大于等于i就gg

#include 
#include
#include 
#include 
#define ll long long
const ll inf=(ll)1e18+7;
using namespace std;
int a[1000005];
int near[1000005];
int main()
{
	ios::sync_with_stdio(false);
	cout.tie(0);cin.tie(0);
	int n,m,k;
	cin>>n>>m>>k;
	bool block[n];
	memset(block,0,sizeof(block));
	int t;
	for(int i=1;i<=m;i++)
	{
		cin>>t;
		block[t]=1;
	}
	if(block[0])
	{
		cout<<-1<>a[i];
	}
	for(int i=1;i<=k;i++)
	{
		bool flag=0;
		int now=0;
		int cnt=0;//统计循环次数
		while(1)
		{
			cnt++;
			if(now+i>=n)
			{
				break;
			}
			else if(near[now+i]==near[now])//无法全部照亮 gg
			{
				flag=1;
				break;
			}
			now=near[now+i];
		}
		if(flag==0)
		{
			res=min(res,1ll*cnt*1ll*a[i]);
		}
	}

	if(res>=1e18)
	{
		cout<<-1<

你可能感兴趣的:(Educational Codeforces Round 45 (Rated for Div. 2)题解(a-e))