Codeforce---Educational Codeforces Round 72 (Rated for Div. 2)题解(ABCD)

A题:
Creating a Character
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You play your favourite game yet another time. You chose the character you didn’t play before. It has str
points of strength and int points of intelligence. Also, at start, the character has exp free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by 1 or raise intelligence by 1

).

Since you’d like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).

Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.

Input

The first line contains the single integer T
(1≤T≤100) — the number of queries. Next T

lines contain descriptions of queries — one per line.

This line contains three integers str
, int and exp (1≤str,int≤108, 0≤exp≤108

) — the initial strength and intelligence of the character and the number of free points, respectively.

Output

Print T

integers — one per query. For each query print the number of different character builds you can create.

Example

Input

4
5 3 4
2 1 0
3 5 5
4 10 6

Output

3
1
2
0

Note

In the first query there are only three appropriate character builds: (str=7,int=5)
, (8,4) and (9,3)

. All other builds are either too smart or don’t use all free points.

In the second query there is only one possible build: (2,1)

.

In the third query there are two appropriate builds: (7,6)
, (8,5)

.

In the fourth query all builds have too much brains.


思路:将每一组的str,int和exp加起来,然后用这个sum除以2,不管加起来的sum是奇数还是偶数,要满足题意,必须要str增加后的值大于这个sum除以2之后的值,所以可以给str加的值就是str+exp-这个sum除以2得到的数字,也就是答案。但是要注意,这个值可能小于0或者大于exp+1,因为答案的范围是0到exp+1,因此判断一下在不在这个范围内输出即可。(一开始没判断这个范围wa了一发orz)。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ios::sync_with_stdio(false);
	int t;
	ll a,b,c;
	cin>>t;
	while(t--)
	{
		cin>>a>>b>>c;
		ll zz=a+b+c;
		ll flag=zz%2;
		ll zzz=zz/2;
		ll fin=a+c;
		fin=fin-zzz;
		ll ans1=max(fin,0LL);
		ans1=min(ans1,c+1);
		cout<<ans1<<endl;
	}
	
	return 0;
 } 

B题:
Zmei Gorynich
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
在这里插入图片描述

Initially Zmei Gorynich has x
heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich’s heads by min(di,curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows hi new heads. If curX=0

then Gorynich is defeated.

You can deal each blow any number of times, in any order.

For example, if curX=10
, d=7, h=10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX=10, d=11, h=100 then number of heads changes to 0

and Zmei Gorynich is considered defeated.

Calculate the minimum number of blows to defeat Zmei Gorynich!

You have to answer t

independent queries.

Input

The first line contains one integer t
(1≤t≤100

) – the number of queries.

The first line of each query contains two integers n
and x (1≤n≤100, 1≤x≤109

) — the number of possible types of blows and the number of heads Zmei initially has, respectively.

The following n
lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers di and hi (1≤di,hi≤109) — the description of the i

-th blow.

Output

For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.

If Zmei Gorynuch cannot be defeated print −1

.

Example

Input

3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100

Output

2
3
-1

Note

In the first query you can deal the first blow (after that the number of heads changes to 10−6+3=7

), and then deal the second blow.

In the second query you just deal the first blow three times, and Zmei is defeated.

In third query you can not defeat Zmei Gorynich. Maybe it’s better to convince it to stop fighting?


思路:一个模拟题,拿到手一开始想的就是贪心,首先我们要注意到对于每一个斩法,这个斩法的d-h就是如果斩不死这条龙时所产生的的实际贡献,那我们每一次都直接用这个实际贡献来计算最少次数吗?显然不太对,因为所有的斩法的 d 的最大值其实是一个斩杀线,到达那个线及以下就可以直接杀掉,于是我们用x减去这个斩杀线mx,剩下的值用斩不死时的性价比最高的实际贡献来计算次数,最终加1即可(加1是斩杀线那一刀)。
需要注意的是,题目有两个特判,第一个特判是直接一刀就能斩杀答案就是1。第二个特判是全都是没有正的实际贡献且无法一刀斩杀,此时答案是 -1。
这两个特判的顺序不能颠倒过来。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
typedef long long ll;
int t;
int n,m;
struct node
{
	ll d,h;
	ll cha;
	ll num=0;ll yu=0;
}poi[maxn]; 
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		ll mx=0;
		ll x;
		cin>>n>>x;
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			cin>>poi[i].d>>poi[i].h;
			poi[i].cha=poi[i].d-poi[i].h;
			poi[i].num=0;
			if(poi[i].cha>0) flag=1;
			mx=max(mx,poi[i].d);
		}
		if(mx>=x)
		{
			cout<<"1"<<endl;continue;
		}
		if(flag==0)
		{
			cout<<"-1"<<endl;continue;
		}
		ll fin=1e12;
		for(int i=1;i<=n;i++)
		{
			if(poi[i].cha>0)
			{
				ll zz=x-mx;
				poi[i].num=zz/poi[i].cha;
				if(poi[i].cha*poi[i].num!=zz) poi[i].num+=1;
				fin=min(fin,poi[i].num);
			}
		}
		fin+=1;
		cout<<fin<<endl;
	}
	
	return 0;
 } 

C题:
The Number Of Good Substrings
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a binary string s
(recall that a string is binary if each character is either 0 or 1

).

Let f(t)
be the decimal representation of integer t written in binary form (possibly with leading zeroes). For example f(011)=3,f(00101)=5,f(00001)=1,f(10)=2,f(000)=0 and f(000100)=4

.

The substring sl,sl+1,…,sr
is good if r−l+1=f(sl…sr)

.

For example string s=1011
has 5 good substrings: s1…s1=1, s3…s3=1, s4…s4=1, s1…s2=10 and s2…s4=011

.

Your task is to calculate the number of good substrings of string s

.

You have to answer t

independent queries.

Input

The first line contains one integer t
(1≤t≤1000

) — the number of queries.

The only line of each query contains string s
(1≤|s|≤2⋅105), consisting of only digits 0 and 1

.

It is guaranteed that ∑i=1t|si|≤2⋅105

.

Output

For each query print one integer — the number of good substrings of string s

.

Example

Input

4
0110
0101
00001000
0001000

Output

4
3
4
3


思路:我们想想如何能遍历所有的01串,仔细想一想如果我们对于每一个1都往后面暴力枚举搜索是不是可以找到所有可能组成的数字,但是这样并不能遍历出所有的01串,因为我们枚举的串的起点都是1,那我们在第一个1前面加上0呢?这个数字会改变吗?显然不会,并且在第一个1前面加上连续的0可以囊括所有的串的情况。
对于往后面遍历增加位数后的数字计算,拿当前的cnt*2+这个数字(0或1)就是此时的数字了。如果这个数字在当前枚举的长度和当前枚举的长度加前缀0的个数之间,那么就是合法的,答案就要加一。
再加上一个小剪枝,如果搜到的数字大于这个输入的串的总长度就直接break掉,就万无一失不怕hack不怕tle不怕爆long long辽。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
char s[maxn];
int num[maxn];
int main()
{
//	ios::sync_with_stdio(false);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s+1);
		int len=strlen(s+1);
		for(int i=1;i<=len;i++) num[i]=0;
		
		for(int i=2;i<=len;i++)
		{
			if(s[i-1]=='0') num[i]=num[i-1]+1;
			if(s[i-1]=='1') num[i]=0;
		}
		ll ans=0;
		for(int i=1;i<=len;i++)
		{
			if(s[i]=='0') continue;
			ll cnt=0;
			for(int j=0;j<=100&&i+j<=len;j++)
			{
				cnt=cnt*2+s[i+j]-'0';
				if(cnt>len) break;
				if(j+1<=cnt&&cnt<=j+1+num[i]) ans++;
			}
		}
		printf("%lld\n",ans);
	}
	
	return 0;
 } 

D题:
(先上课,晚上待补)
upd:来了来了~
D. Coloring Edges
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a directed graph with n
vertices and m

directed edges without self-loops or multiple edges.

Let’s denote the k
-coloring of a digraph as following: you color each edge in one of k colors. The k

-coloring is good if and only if there no cycle formed by edges of same color.

Find a good k
-coloring of given digraph with minimum possible k

.
Input

The first line contains two integers n
and m (2≤n≤5000, 1≤m≤5000

) — the number of vertices and edges in the digraph, respectively.

Next m
lines contain description of edges — one per line. Each edge is a pair of integers u and v (1≤u,v≤n, u≠v) — there is directed edge from u to v

in the graph.

It is guaranteed that each ordered pair (u,v)

appears in the list of edges at most once.
Output

In the first line print single integer k
— the number of used colors in a good k

-coloring of given graph.

In the second line print m
integers c1,c2,…,cm (1≤ci≤k), where ci is a color of the i

-th edge (in order as they are given in the input).

If there are multiple answers print any of them (you still have to minimize k

).
Examples
Input
Copy

4 5
1 2
1 3
3 4
2 4
1 4

Output
Copy

1
1 1 1 1 1

Input
Copy

3 3
1 2
2 3
3 1

Output
Copy

2
1 1 2


思路:题目说给图的每条边染色,使得染色后的图中的每个环中的每条边的颜色不是仅仅只有一种的,考虑dfs,大家看代码理解一下吧,不是很难理解。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+50;
struct node
{
	int y;int id;
};
vector<node> edge[maxn];
int n,m;
int vis[maxn];
int ans;
int col[maxn];

void dfs(int u)
{
	vis[u]=1;
	for(int i=0;i<edge[u].size();i++)
	{
		int v=edge[u][i].y;
		int id=edge[u][i].id;
		if(vis[v]==1)
		{
			col[id]=2;
			ans=2;
		}
		else
		{
			if(vis[v]==0) dfs(v);
		 } 
	}
	vis[u]=2;
	return ;
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int a,b;
		cin>>a>>b;
		edge[a].push_back(node{b,i});
		col[i]=1;
	}
	ans=1;
	for(int i=1;i<=n;i++)
	{
		if(!vis[i]) dfs(i);
	}
	cout<<ans<<endl;
	for(int i=1;i<=m;i++)
	{
		if(i==1) cout<<col[i];
		else cout<<" "<<col[i];
	}
	cout<<endl;
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值