其他分享
首页 > 其他分享> > 2020.10.2--个人赛补题报告

2020.10.2--个人赛补题报告

作者:互联网

E - Buying a TV Set

Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than aa and screen height not greater than bb. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is ww, and the height of the screen is hh, then the following condition should be met: w/h=x/y

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers ww and hh there is a TV set with screen width ww and height hh in the shop.

Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers ww and hh, beforehand, such that (w≤a), (h≤b) and (w/h=x/y).

In other words, Monocarp wants to determine the number of TV sets having aspect ratio xyxy, screen width not exceeding aa, and screen height not exceeding bb. Two TV sets are considered different if they have different screen width or different screen height.

Input

The first line contains four integers a, b, x, y (1≤a,b,x,y≤10^18) — the constraints on the screen width and height, and on the aspect ratio.

Output

Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.

Examples

Input
17 15 5 3
Output
3
Input
14 16 7 22
Output
0
Input
4 2 6 4
Output
1
Input
1000000000000000000 1000000000000000000 999999866000004473 999999822000007597
Output
1000000063

Note

In the first example, there are 33 possible variants: (5,3)(5,3), (10,6).

In the second example, there is no TV set meeting the constraints.

In the third example, there is only one variant: (3,2)

题意:确定高宽比为x/y、屏幕宽度不超过a、屏幕高度不超过b的电视机数量。如果两台电视机屏幕宽度或屏幕高度不同,则视为不同。

思路:先将高宽比x/y简化为最简比值,找出x,y的最大公因数,化为最简,再直接用a,b分别除以化简后的x,y,取两值的最小值即可

注意:不要使用for循环找,直接出即可,否则会超时,还有题中没说是多个案例,不用while(1)无限输入,因为也会超时;求最大公因数可以用gcd()函数

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll  gcd(ll x,ll y)
{
    ll t;
    while(y)
    {
        t=x%y;
        x=y;
        y=t;
    }
    return x;
}
int main()
{
    /*while(1)
    {*/
        ll a,b,x,y,ct=0,m,j=1,w=1;
        scanf("%lld%lld%lld%lld",&a,&b,&x,&y);
        m=gcd(x,y);
        x=x/m;
        y=y/m;
       /* if(x>a||y>b)cout<<"0"<<endl;
        else
        {*/
            ct=min(a/x,b/y);
            printf("%lld\n",ct);
       // }
    //}
}
View Code

 C - From S To T

You are given three strings ss, tt and pp consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.

During each operation you choose any character from pp, erase it from pp and insert it into string ss (you may insert this character anywhere you want: in the beginning of ss, in the end or between any two consecutive characters).

For example, if pp is aba, and ss is de, then the following outcomes are possible (the character we erase from pp and insert into ss is highlighted):

Your goal is to perform several (maybe zero) operations so that ss becomes equal to tt. Please determine whether it is possible.

Note that you have to answer qq independent queries.

Input

The first line contains one integer qq (1≤q≤1001≤q≤100) — the number of queries. Each query is represented by three consecutive lines.

The first line of each query contains the string ss (1≤|s|≤1001≤|s|≤100) consisting of lowercase Latin letters.

The second line of each query contains the string tt (1≤|t|≤1001≤|t|≤100) consisting of lowercase Latin letters.

The third line of each query contains the string pp (1≤|p|≤1001≤|p|≤100) consisting of lowercase Latin letters.

Output

For each query print YES if it is possible to make ss equal to tt, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example

Input
4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa
Output
YES
YES
NO
NO

Note

In the first test case there is the following sequence of operation:

  1. s=s= ab, t=t= acxb, p=p= cax;
  2. s=s= acb, t=t= acxb, p=p= ax;
  3. s=s= acxb, t=t= acxb, p=p= a.

In the second test case there is the following sequence of operation:

  1. s=s= a, t=t= aaaa, p=p= aaabbcc;
  2. s=s= aa, t=t= aaaa, p=p= aabbcc;
  3. s=s= aaa, t=t= aaaa, p=p= abbcc;
  4. s=s= aaaa, t=t= aaaa, p=p= bbcc.

题意:给出三个字符串s,t,p,你可以执行任意操作(或者零操作),每次操作都从p中删除一个字符,然后插入到s中,可以插入到s的任意位置,如果可以使s等于p则输出“YES”,否则“NO”

思路:先看s是否是t的子串,不是则直接输出“NO”,若是则再看t中字符的种类及数量,以及s中的字符种类及数量,相减,看p中是否有相关字符以及数量是否足够

#include<bits/stdc++.h>
using namespace std;
int show(string s,string t)//判断s是否是t的子串
{
    int m,n,i,j=0;
    for(i=0;i<t.length();i++)
    {
        for(j;j<s.size();j++)
        {
            if(s[j]==t[i])
            {
                j++;
                break;
            }
            else break;
        }

    }
    if(j==s.size())return 1;
    else return 0;
}
int main()
{
    int q,i,j;
    cin>>q;
    while(q--){
        string s,t,p;
        cin>>s>>t>>p;
        int w[1000]={0},k=0;
        if(show(s,t)==1)
        {
            for(i=0;i<t.size();i++)
            {
                w[t[i]]++;
            }
            for(i=0;i<s.size();i++)//找出s中缺少的字母及数量
            {
                w[s[i]]--;
            }
            for(i=0;i<p.size();i++)//看p中是否有缺少的字母及数量是否足够,若有则减1直到为零
            {
                if(w[p[i]]>0)
                {
                    w[p[i]]--;
                }
            }
            for(i=0;i<t.size();i++)
            {
                if(w[t[i]]>0)//判断缺少的字母数量是否为零,即是否可以使s构成t
                {
                    k=1;
                    break;
                }
            }
            if(k==0)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        else cout<<"NO"<<endl;
    }
}
View Code

 B - Yet Another Crosses Problem

You are given a picture consisting of nn rows and mmcolumns. Rows are numbered from 11 to nn from the top to the bottom, columns are numbered from 11 to mm from the left to the right. Each cell is painted either black or white.

You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers xx and yy, where 1≤x≤n1≤x≤n and 1≤y≤m1≤y≤m, such that all cells in row xx and all cells in column yy are painted black.

For examples, each of these pictures contain crosses:

The fourth picture contains 4 crosses: at (1,3)(1,3), (1,5)(1,5), (3,3)(3,3) and (3,5)(3,5).

Following images don't contain crosses:

You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.

What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?

You are also asked to answer multiple independent queries.

Input

The first line contains an integer qq (1≤q≤5⋅1041≤q≤5⋅104) — the number of queries.

The first line of each query contains two integers nn and mm (1≤n,m≤5⋅1041≤n,m≤5⋅104, n⋅m≤4⋅105n⋅m≤4⋅105) — the number of rows and the number of columns in the picture.

Each of the next nn lines contains mm characters — '.' if the cell is painted white and '*' if the cell is painted black.

It is guaranteed that ∑n≤5⋅104∑n≤5⋅104 and ∑n⋅m≤4⋅105∑n⋅m≤4⋅105.

Output

Print qq lines, the ii-th line should contain a single integer — the answer to the ii-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.

Example

Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2

Note

The example contains all the pictures from above in the same order.

The first 5 pictures already contain a cross, thus you don't have to paint anything.

You can paint (1,3)(1,3), (3,1)(3,1), (5,3)(5,3) and (3,5)(3,5) on the 66-th picture to get a cross in (3,3)(3,3). That'll take you 44 minutes.

You can paint (1,2)(1,2) on the 77-th picture to get a cross in (4,2)(4,2).

You can paint (2,2)(2,2) on the 88-th picture to get a cross in (2,2)(2,2). You can, for example, paint (1,3)(1,3), (3,1)(3,1) and (3,3)(3,3)to get a cross in (3,3)(3,3) but that will take you 33 minutes instead of 11.

There are 9 possible crosses you can get in minimum time on the 99-th picture. One of them is in (1,1)(1,1): paint (1,2)(1,2)and (2,1)(2,1).

题意:给出有n行m列的方格图,图中有些方格被涂成了黑色,现需要使图中出现至少一个十字架,每分钟你可以选择一个白色的方格将它涂成黑色。 你必须花费的最少时间是多少分钟,这样得到的图像至少包含一个叉?

思路:分别得到每行及每列的黑色方格总数,找出最多的行列,再计算出需要涂多少个可以得到一个十字叉,注意可能行列在交叉处会重复,重复时要减一

#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
const int mixn= 5e4+3;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n,m,i,j;
        cin>>n>>m;
        char s[n+5][m+5];
        int ht[mixn]={0},k=0,maxn=0;
         int a,b;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>s[i][j];
            }
        }
        int lt[mixn]={0},p=0,w=0;

        int minn=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)

            {
                if(s[i][j]=='*')
                {
                    lt[j]++;//计算每列的*数
                    ht[i]++;//计算每行的*数
                }
            }

        }
        int sum=m+n;
        int ct;
        for(i=0;i<n;i++)
        {

            for(j=0;j<m;j++)
            {

                if(s[i][j]=='*')ct=ht[i]+lt[j]-1;//行列重复要减一
                else ct=ht[i]+lt[j];
                 sum=min(sum,m+n-ct-1);
            }

        }
        cout<<sum<<endl;
    }
}
View Code

F - Coffee Break

Recently Monocarp got a job. His working day lasts exactly mm minutes. During work, Monocarp wants to drink coffee at certain moments: there are nn minutes a1,a2,…,ana1,a2,…,an, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute).

However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute aiai, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least dd minutes pass between any two coffee breaks. Monocarp also wants to take these nncoffee breaks in a minimum possible number of workingdays (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than dd minutes pass between the end of any working day and the start of the following working day.

For each of the nn given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.

Input

The first line contains three integers nn, mm, dd (1≤n≤2⋅105,n≤m≤109,1≤d≤m)(1≤n≤2⋅105,n≤m≤109,1≤d≤m) — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.

The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤m)(1≤ai≤m), where aiai is some minute when Monocarp wants to have a coffee break.

Output

In the first line, write the minimum number of days required to make a coffee break in each of the nn given minutes.

In the second line, print nn space separated integers. The ii-th of integers should be the index of the day during which Monocarp should have a coffee break at minute aiai. Days are numbered from 11. If there are multiple optimal solutions, you may print any of them.

Examples

Input
4 5 3
3 5 1 2
Output
3
3 1 1 2
Input
10 10 1
10 5 7 4 6 3 2 1 9 8
Output
2
2 1 1 2 2 1 2 1 1 2

Note

In the first example, Monocarp can take two coffee breaks during the first day (during minutes 11 and 55, 33 minutes will pass between these breaks). One break during the second day (at minute 22), and one break during the third day (at minute 33).

In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.

 题意:(题意挺难理解的)

最近Monocarp找到了一份工作。他的工作时间正好是m分钟。在工作期间,Monocarp想要在特定的时刻喝咖啡:有n分钟a1,a2,…,an,当他能够并且愿意喝咖啡休息(为了简单起见,让我们考虑一下每个咖啡休息时间正好持续1分钟)。

然而,Monocarp的老板不喜欢Monocarp经常喝咖啡休息时间。所以对于给定的咖啡休息时间是在分钟ai上,Monocarp必须选择他在这一分钟内喝咖啡的日期,以便在任何两个咖啡休息时间之间每天至少有d分钟的时间。Monocarp还希望在最短的工作日内享受这n个咖啡休息时间(他不计算他不工作的天数,而且他在这样的日子里不喝咖啡)。考虑到任何工作日结束到下一个工作日开始之间超过d分钟的时间。

在给定的n分钟内决定一天的时间,在这一分钟内单粒种子应该喝咖啡休息。你必须尽量减少花费的天数。

大意:m要在最少的天数里喝完n杯咖啡,每天有n分钟可以喝咖啡,每杯咖啡要花费1单元时间喝完,如果两杯在一天喝,则两杯间隔至少d分钟,要求输出最少几天可以喝完,同时输出题目输入的每一杯咖啡在第几天喝完的

让第一杯在第一天喝,下一杯喝咖啡时间为ai+d+1,以此类推,超过这一天则另开一天

 

#include<bits/stdc++.h>
#include<set>
#include<vector>
using namespace std;
const int maxn=2e5+10;
const int minn=0x3f3f3f3f;
int s[maxn],b[maxn];
set<pair<int,int> >ss;//ss为类名;注意最后的> >中间有空格
//两个> >中间要加空格隔开;set会自动升序排列
//这里每一个元素都是pair,它排列是先排pair里的first,再排second。
//比如说pair<2,3> pair<2,1> pair<0,6>
//排列之后是pair<0,6>,pair<2,1>,pair<2,3>。
//如果first 相同,再比较second
set<pair<int ,int> >::iterator it;//iterator迭代器迭代程序
int n,m,d,ct;
//set()函数:在插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,它不会插入相同键值的元素,而采取忽略处理。
int main()
{
    cin>>n>>m>>d;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
        ss.insert(make_pair(s[i],i));//插入ai及i
        //pair需要指定构造的类型,make_pair可以隐式转换
    }
    while (!ss.empty()){
        int p=ss.begin()->second;
        b[p]=++ct;
        ss.erase(ss.begin());//释放
        while(1){
            it=ss.upper_bound(make_pair(s[p]+d,minn));//返回最后一个大于等于括弧中的定位器
            if(it==ss.end()) //若没有查找到则值等于ss.end();
                break;
            p=it->second;
            b[p]=ct;
            ss.erase(it);//释放掉it的值,相当于丢弃该值
        }
    }
    printf("%d\n",ct);
    for(int i=1;i<=n;i++)
        printf("%d%c",b[i],i==n?'\n':' ');
}
View Code

 

标签:ss,..,Monocarp,--,break,int,补题,2020.10,first
来源: https://www.cnblogs.com/mxw000120/p/13763783.html