Educational Codeforces Round 24 A~E (A,B,C,D暴力 E双指针)

【总结:说在前面】

好久不更新博客了~终于等到暑假有空打一场CF!

这是教育场,所以题目其实都很水吧,至少我看了A~E都Too Simple啊。。。4道题随便暴力就过了。。。

不过好久不写代码,熟练度直线下滑,WA了好几次,细节还是要考虑全面的!

我想去睡觉了,所以也就先简单写写感受贴贴代码吧,有时间再补坑,就不提供翻译了。。。

A. Diplomas and Certificates
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n students who have taken part in an olympiad. Now it's time to award the students.

Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly k times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of n). It's possible that there are no winners.

You have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners.

Input

The first (and the only) line of input contains two integers n and k (1 ≤ n, k ≤ 1012), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas.

Output

Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.

It's possible that there are no winners.

Examples
input
18 2
output
3 6 9
input
9 10
output
0 0 9
input
1000000000000 5
output
83333333333 416666666665 500000000002
input
1000000000000 499999999999
output
1 499999999999 500000000000

 

感觉没啥好说的,随便xjb搞搞,小学生入门都会吧。。。

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

long long n,k,a,x,y,z;

int main()
{
	scanf("%I64d%I64d",&n,&k);
	a=n/2;
	x=a/(k+1);y=x*k;z=n-x-y;
	printf("%I64d %I64d %I64d\n",x,y,z);
	return 0;
}

  

B. Permutation Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.

The game consists of m steps. On each step the current leader with index i counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.

You are given numbers l1, l2, ..., lm — indices of leaders in the beginning of each step. Child with number l1 is the first leader in the game.

Write a program which will restore a possible permutation a1, a2, ..., an. If there are multiple solutions then print any of them. If there is no solution then print -1.

Input

The first line contains two integer numbers nm (1 ≤ n, m ≤ 100).

The second line contains m integer numbers l1, l2, ..., lm (1 ≤ li ≤ n) — indices of leaders in the beginning of each step.

Output

Print such permutation of n numbers a1, a2, ..., an that leaders in the game will be exactly l1, l2, ..., lm if all the rules are followed. If there are multiple solutions print any of them.

If there is no permutation which satisfies all described conditions print -1.

Examples
input
4 5
2 3 1 4 4
output
3 1 2 4 
input
3 3
3 1 2
output
-1
Note

Let's follow leadership in the first example:

  • Child 2 starts.
  • Leadership goes from 2 to 2 + a2 = 3.
  • Leadership goes from 3 to 3 + a3 = 5. As it's greater than 4, it's going in a circle to 1.
  • Leadership goes from 1 to 1 + a1 = 4.
  • Leadership goes from 4 to 4 + a4 = 8. Thus in circle it still remains at 4.

也是一个非常水的暴力题,不过细节要注意,有坑,我掉了两次。。。

样例具有迷惑,题目开始看错了。。。

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

int n,m,x,cnt;
int a[105],b[105],c[105];
int vis[105];

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&a[i]);
		if(i==1)continue;
		int x=a[i-1],y=a[i];
		if(y<=x)y+=n;
		b[a[i-1]]=y-x;
		if(vis[y-x] && vis[y-x]!=a[i-1])
		{
			cout<<-1;
			return 0;
		}
		if(b[a[i-1]] && b[a[i-1]]!=y-x)
		{
			cout<<-1;
			return 0;
		}
		vis[y-x]=a[i-1];
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			c[++cnt]=i;
		}
	}
	cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(!b[i])
		{
			b[i]=c[++cnt];
		}
		if(!b[i])
		{
			cout<<-1;
			return 0;
		}
	}
	for(int i=1;i<=n;i++)printf("%d ",b[i]);
	return 0;
}

  C. Sofa Thief

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss?

Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same!

The storehouse is represented as matrix n × m. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells.

Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xba is covered by A and b is covered by B. Sofa Ais standing to the top of sofa B if there exist two such cells a and b that ya < yba is covered by A and b is covered by B. Right and bottom conditions are declared the same way.

Note that in all conditions A ≠ B. Also some sofa A can be both to the top of another sofa B and to the bottom of it. The same is for left and right conditions.

The note also stated that there are cntl sofas to the left of Grandpa Maks's sofa, cntr — to the right, cntt — to the top and cntb — to the bottom.

Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions.

Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1.

Input

The first line contains one integer number d (1 ≤ d ≤ 105) — the number of sofas in the storehouse.

The second line contains two integer numbers nm (1 ≤ n, m ≤ 105) — the size of the storehouse.

Next d lines contains four integer numbers x1y1x2y2 (1 ≤ x1, x2 ≤ n1 ≤ y1, y2 ≤ m) — coordinates of the i-th sofa. It is guaranteed that cells (x1, y1) and (x2, y2) have common side, (x1, y1 ≠  (x2, y2) and no cell is covered by more than one sofa.

The last line contains four integer numbers cntlcntrcnttcntb (0 ≤ cntl, cntr, cntt, cntb ≤ d - 1).

Output

Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through d as given in input. If there is no such sofa then print -1.

Examples
input
2
3 2
3 1 3 2
1 2 2 2
1 0 0 1
output
1
input
3
10 10
1 2 1 1
5 5 6 5
6 4 5 4
2 1 2 0
output
2
input
2
2 2
2 1 1 1
1 2 2 2
1 0 0 0
output
-1
Note

Let's consider the second example.

  • The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below).
  • The second sofa has cntl = 2cntr = 1cntt = 2 and cntb = 0.
  • The third sofa has cntl = 2cntr = 1cntt = 1 and cntb = 1.

So the second one corresponds to the given conditions.

In the third example

  • The first sofa has cntl = 1cntr = 1cntt = 0 and cntb = 1.
  • The second sofa has cntl = 1cntr = 1cntt = 1 and cntb = 0.

And there is no sofa with the set (1, 0, 0, 0) so the answer is -1.

此题就是先记录每个沙发上下左右的沙发数量,类似前缀和思路O(N)搞定,很简单。后面就是扫一下,要注意一个沙发如果x1<x2此时在此沙发前的会多一个自己,要去掉,其他方向类似,然后就没了。。。

 

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int d,n,m,l,r,t,b,cnt;
int x1[100005],yy1[100005],x2[100005],y2[100005];
int lef[100005],rig[100005],top[100005],bot[100005];

int main()
{
	scanf("%d%d%d",&d,&n,&m);
	for(int i=1;i<=d;i++)
	{
		scanf("%d%d%d%d",&x1[i],&yy1[i],&x2[i],&y2[i]);
		lef[min(x1[i],x2[i])]++;
		rig[max(x1[i],x2[i])]++;
		top[min(yy1[i],y2[i])]++;
		bot[max(yy1[i],y2[i])]++;
	}
	scanf("%d%d%d%d",&l,&r,&t,&b);
	
	for(int i=1;i<=n+1;i++)
	{
		lef[i]+=lef[i-1];
	}
	for(int i=1;i<=m+1;i++)
	{
		top[i]+=top[i-1];
	}
	for(int i=n+1;i>=1;i--)
	{
		rig[i-1]+=rig[i];
	}
	for(int i=m+1;i>=1;i--)
	{
		bot[i-1]+=bot[i];
	}
	
	for(int i=1;i<=d;i++)
	{
		int ll=0,rr=0,tt=0,bb=0;
		ll+=lef[max(x1[i],x2[i])-1];
		if(x1[i]!=x2[i])ll--;
		rr+=rig[min(x1[i],x2[i])+1];
		if(x1[i]!=x2[i])rr--;
		tt+=top[max(yy1[i],y2[i])-1];
		if(yy1[i]!=y2[i])tt--;
		bb+=bot[min(yy1[i],y2[i])+1];
		if(yy1[i]!=y2[i])bb--;
		if(l==ll && r==rr && t==tt && b==bb)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	
	printf("%d\n",-1);
	return 0;
}

 

  

D. Multicolored Cars

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another.

The game rules are like this. Firstly Alice chooses some color A, then Bob chooses some color B (A ≠ B). After each car they update the number of cars of their chosen color that have run past them. Let's define this numbers after i-th car cntA(i) and cntB(i).

  • If cntA(i) > cntB(i) for every i then the winner is Alice.
  • If cntB(i) ≥ cntA(i) for every i then the winner is Bob.
  • Otherwise it's a draw.

Bob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color A and Bob now wants to choose such color B that he will win the game (draw is not a win). Help him find this color.

If there are multiple solutions, print any of them. If there is no such color then print -1.

Input

The first line contains two integer numbers n and A (1 ≤ n ≤ 105, 1 ≤ A ≤ 106) – number of cars and the color chosen by Alice.

The second line contains n integer numbers c1, c2, ..., cn (1 ≤ ci ≤ 106) — colors of the cars that Alice and Bob will encounter in the order of their appearance.

Output

Output such color B (1 ≤ B ≤ 106) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1.

It is guaranteed that if there exists any solution then there exists solution with (1 ≤ B ≤ 106).

Examples
input
4 1
2 1 4 2
output
2
input
5 2
2 2 4 5 3
output
-1
input
3 10
1 2 3
output
4
Note

Let's consider availability of colors in the first example:

  • cnt2(i) ≥ cnt1(i) for every i, and color 2 can be the answer.
  • cnt4(2) < cnt1(2), so color 4 isn't the winning one for Bob.
  • All the other colors also have cntj(2) < cnt1(2), thus they are not available.

In the third example every color is acceptable except for 10.

这题就是纯粹暴力带比较解决,还没有C难,和B差不多难度,没什么坑,自己手滑写炸了一次555。。。

 

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std;

int n,m,x;
int cnt[1000005];
bool vis[1000005];
queue<int>q,qt;
bool f,turn;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(x!=m)
		{
			cnt[x]++;
			if(!f && !vis[x])q.push(x);
			if(!vis[x])vis[x]=1;
		}
		else
		{
			f=1;
			cnt[m]++;
			if(!turn)
			{
				while(!q.empty())
				{
					int v=q.front();
					q.pop();
					if(cnt[v]>=cnt[m])qt.push(v);
				}
				if(qt.empty())
				{
					cout<<-1<<endl;
					return 0;
				}
				turn=1;
			}
			else
			{
				while(!qt.empty())
				{
					int v=qt.front();
					qt.pop();
					if(cnt[v]>=cnt[m])q.push(v);
				}
				if(q.empty())
				{
					cout<<-1<<endl;
					return 0;
				}
				turn=0;
			}
		}
	}
	if(!q.empty())printf("%d\n",q.front()); else printf("%d\n",qt.front());
	return 0;
}

 

  E. Card Game Again

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vova again tries to play some computer card game.

The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number ai is written on the i-th card in the deck.

After receiving the deck and the magic number, Vova removes x (possibly x = 0) cards from the top of the deck, y (possibly y = 0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards x + 1x + 2, ... n - y - 1n - y from the original deck.

Vova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid?

Input

The first line contains two integers n and k (1 ≤ n ≤ 100 0001 ≤ k ≤ 109).

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 109) — the numbers written on the cards.

Output

Print the number of ways to choose x and y so the resulting deck is valid.

Examples
input
3 4
6 2 8
output
4
input
3 6
9 1 14
output
1
Note

In the first example the possible values of x and y are:

  1. x = 0, y = 0;
  2. x = 1, y = 0;
  3. x = 2, y = 0;
  4. x = 0, y = 1.

 

E题算是唯一有技术含量的题,是双指针+数学乘法原理,1A!注意要long long啊!

对于每次 : ans = ans+ (n - end) * (start - previousStart)

双指针维护start , end 即可.也不算难.

 

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std;

int n,k,lef,rig,last,i;
int a[100005];
long long ans=0;

int main()
{
	scanf("%d%d",&n,&k);
	lef=1,rig=1,last=0;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	while(rig<=n)
	{
		long long p=1;
		for(i=lef;i<=n;i++)
		{
			p*=a[i];
			p%=(long long)k;
			if(p==0)break;
		}
		rig=i;
		p=1;
		for(i=rig;i>=1;i--)
		{
			p*=a[i];
			p%=(long long)k;
			if(p==0)break;
		}
		lef=i;
		ans+=(long long)(lef-last)*(long long)(n-rig+1);
		last=lef;
		lef++;
		rig++;
	}
	printf("%I64d\n",ans);
	return 0;
}

 

  睡觉去了~~~~~~~~~~

 

转载于:https://www.cnblogs.com/winmt/p/7129090.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值