1187 - Educational Codeforces Round 67 (Rated for Div. 2)

1187 - Educational Codeforces Round 67 (Rated for Div. 2)(题解)

A. Stickers and Toys

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Your favorite shop sells **n** Kinder Surprise chocolate eggs. You know that exactly **s** stickers and exactly **t** toys are placed in **n** eggs in total.

Each Kinder Surprise can be one of three types:

it can contain a single sticker and no toy;
it can contain a single toy and no sticker;
it can contain both a single sticker and a single toy.
But you don’t know which type a particular Kinder Surprise has. All eggs look identical and indistinguishable from each other.

What is the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you’ll obtain at least one sticker and at least one toy?

Note that you do not open the eggs in the purchasing process, that is, you just buy some number of eggs. It’s guaranteed that the answer always exists.

Input

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

Next T lines contain three integers n, s and t each (1≤n≤10^9,1≤s,t≤n,s+t≥n) — the number of eggs, stickers and toys.

All queries are independent.

Output

Print T integers (one number per query) — the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you’ll obtain at least one sticker and one toy

Example

input

3
10 5 7
10 10 10
2 1 1

output

6
1
2
Note
In the first query, we have to take at least 6 eggs because there are 5 eggs with only toy inside and, in the worst case, we’ll buy all of them.

In the second query, all eggs have both a sticker and a toy inside, that’s why it’s enough to buy only one egg.

In the third query, we have to buy both eggs: one with a sticker and one with a toy.

题意

鸡蛋共有三种,只有玩具没有贴纸的,只有贴纸没有玩具的,有贴纸又有玩具的,给你n个鸡蛋,里面有s个贴纸,t个玩具,输出最小的能确保同时拿到玩具和贴纸的鸡蛋数。签到题,总共有s+t-n个双黄蛋,双黄蛋>=n的时候输出1,不然为拿到最多的那种蛋减去双黄蛋的个数再加1

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T;
	int n,s,t,max=0,sh;
	cin>>T;
	while(T--)
	{
		cin>>n>>s>>t;
		if(s>t)
		max=s;
		else
		max=t;
		sh=s+t-n;
		if(sh>=n)
		cout<<1<<endl;
		else
		cout<<max-(sh)+1<<endl;
	}
	return 0;
}

B. Letters Shop

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The letters shop showcase is a string s, consisting of n lowercase Latin letters. As the name tells, letters are sold in the shop.

Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string s.

There are m friends, the i-th of them is named ti. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount.

For example, for s=“arrayhead” and ti=“arya” 5 letters have to be bought (“arrayhead”).
For example, for s=“arrayhead” and ti=“harry” 6 letters have to be bought (“arrayhead”).
For example, for s=“arrayhead” and ti=“ray” 5 letters have to be bought (“arrayhead”).
For example, for s=“arrayhead” and ti=“r” 2 letters have to be bought (“arrayhead”).
For example, for s=“arrayhead” and ti=“areahydra” all 9 letters have to be bought (“arrayhead”).
It is guaranteed that every friend can construct her/his name using the letters from the string s.

Note that the values for friends are independent, friends are only estimating them but not actually buying the letters.

Input

The first line contains one integer n (1≤n≤2⋅10^5) — the length of showcase string s.

The second line contains string s, consisting of exactly n lowercase Latin letters.

The third line contains one integer m (1≤m≤5⋅10^4) — the number of friends.

The i-th of the next m lines contains ti (1≤|ti|≤2⋅10^5) — the name of the i-th friend.

It is guaranteed that ∑i=1m|ti|≤2⋅105.

Output

For each friend print the length of the shortest prefix of letters from s s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount.

It is guaranteed that every friend can construct her/his name using the letters from the string s.

Example

input

9
arrayhead
5
arya
harry
ray
r
areahydra

output

5
6
5
2
9

题意

给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t。存一下26个字母出现的位置,查找位置中的最大值即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,q;
    string s;
    vector<int> v[27];
    cin>>n;
    cin>>s;
    for(int i=0; i<s.size(); i++)
    {
        v[s[i]-'a'].push_back(i);
    }
    cin>>q;
    while(q--)
    {
        int a[27]= {0};
        string x;
        cin>>x;
        int ans=0;
        for(int i=0; i<x.size(); i++)
        {
            ans=max(ans,v[x[i]-'a'][a[x[i]-'a']]+1);
            a[x[i]-'a']++;
        }
        cout<<ans<<endl;
    }
}

C. Vasya And Array

time limit per test1 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has an array a1,a2,…,an.

You don’t know this array, but he told you m facts about this array. The i-th fact is a triple of numbers ti, li and ri (0≤ti≤1,1≤li<ri≤n) and it means:

if ti=1 then subbarray ali,ali+1,…,ari is sorted in non-decreasing order;
if ti=0 then subbarray ali,ali+1,…,ari is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter.
For example if a=[2,1,1,3,2] then he could give you three facts: t1=1,l1=2,r1=4 (the subarray [a2,a3,a4]=[1,1,3] is sorted), t2=0,l2=4,r2=5 (the subarray [a4,a5]=[3,2] is not sorted), and t3=0,l3=3,r3=5 (the subarray [a3,a5]=[1,3,2] is not sorted).

You don’t know the array a. Find any array which satisfies all the given facts.

Input

The first line contains two integers n and m (2≤n≤1000,1≤m≤1000).

Each of the next m lines contains three integers ti, li and ri (0≤ti≤1,1≤li<ri≤n).

If ti=1 then subbarray ali,ali+1,…,ari is sorted. Otherwise (if ti=0) subbarray ali,ali+1,…,ari is not sorted.

Output

If there is no array that satisfies these facts in only line print NO (in any letter case).

If there is a solution, print YES (in any letter case). In second line print n integers a1,a2,…,an (1≤ai≤109) — the array a, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.

题意

就是构造一个长度为n的数组,然后给出m条描述条件,分为两种描述,一种是描述区间[l,r]内的数单调不减,另一种描述[l,r]内的数不是单调不减,有则输出s并输出这个序列,无则输出no。我们可以用一个二维数组存放所有的条件,并用另一个数组标记某段区间的描述。因为单调不减的条件显然比不是单调不减的条件强,所以我们可以先标记上所有满足t=1描述的区间,处理完第一种后,对于每一个第二种描述,检查[l,r−1]内的mark是否有0的情况,显然,如果没有,这种描述是无解的,check check所有第二种描述都没问题之后,另a[1]=n+1,然后对于1≤i<n,如果mark[i]=1,不妨令a[i+1]=a[i],否则令a[i+1]=a[i]−1



#include<bits/stdc++.h>
using namespace std;
const int maxn=1002;
int facts[maxn][3],mark[maxn];

int main()
{
	int n,m,i,j;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
		cin>>facts[i][0]>>facts[i][1]>>facts[i][2];
		if(facts[i][0]==1)
		{
			for(j=facts[i][1];j<facts[i][2];j++)
			mark[j]=1;
		}
	}
	for(i=1;i<=m;i++)
	{
		bool flag=false;
		if(facts[i][0]==0)
		{
			for(j=facts[i][1];j<facts[i][2];j++)
				if(mark[j]==0)
				{
					flag=true;
					break;
				}
			if(flag==false)
			{
			cout<<"NO"<<endl;
			return 0;
			}
		}
	}
    int ans = n+1;
    cout << "YES" << endl;
    for(int i = 1; i <= n; ++i){
        if(mark[i-1])
            cout << ans << " ";
        else
            cout << --ans << " ";
    }
	return 0;
}

还有三题待更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值