Codeforces Round #551 (Div. 2)

链接:http://codeforces.com/contest/1153

A. Serval and Bus

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.

Serval will go to the bus station at time tt, and there are nn bus routes which stop at this station. For the ii-th bus route, the first bus arrives at time sisi minutes, and each bus of this route comes didi minutes later than the previous one.

As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.

Input

The first line contains two space-separated integers n and t (1≤n≤1001≤n≤100, 1≤t≤105,1≤t≤105) — the number of bus routes and the time Serval goes to the station.

Each of the next nn lines contains two space-separated integers si and di (1≤si,di≤105) — the time when the first bus of this route arrives and the interval between two buses of this route.

Output

Print one number — what bus route Serval will use. If there are several possible answers, you can print any of them.

Examples

input

Copy

2 2
6 4
9 5

output

Copy

1

input

Copy

5 5
3 3
2 5
5 6
4 9
6 1

output

Copy

3

input

Copy

3 7
2 2
2 3
2 4

output

Copy

1

Note

In the first example, the first bus of the first route arrives at time 66, and the first bus of the second route arrives at time 99, so the first route is the answer.

In the second example, a bus of the third route arrives at time 55, so it is the answer.

In the third example, buses of the first route come at times 22, 44, 66, 88, and so fourth, buses of the second route come at times 22, 55, 88, and so fourth and buses of the third route come at times 22, 66, 1010, and so on, so 11 and 22 are both acceptable answers while 33 is not.

题意:

给出时间 t 和n辆车的首发时间和发车间隔时间,问发车时间 >=t,且离 t 最近的是哪辆车

emmmm...我是按轮来找的时间,看每一辆车的出车时间,如果>t的话,看是不是比当前最小时间小,小的话就更新。

注意可能前一轮的车比后一轮的车时间晚,所以当一轮结束时,先计算出下一轮各辆车的时间,和当前的最小时间比较,如果小的话,那么就可能更新,如果大的话,下一轮时间会更长,那么就结束。

#include
using namespace std;
#define ll long long
const int maxn=1e5+5;
int n,m;
int a[105],d[105];
int mint=0x3f3f3f3f;
int cur;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i]>>d[i];
    for(int i=1;i<=n;i++)
    {
        bool flag=false;
        if(a[i]>=m)//如果可以赶上
        {
            if(a[i]<=mint)//如果此时需要的时间少
            {
                mint=a[i];//更新最小时间
                cur=i;//第i辆
            }
        }
        if(i==n)//到了第n辆,开始新一轮
            for(int j=1;j<=n;j++)
            {
                a[j]+=d[j];//下一轮的各辆车时间
                if(a[j]

也可以通过时间差来判断,t-a[i]是 t 和第i辆车首发之间的时间差,那 (t-a[i])/d[i] 就是这个时间段内可以发多少辆车。

(发车数量肯定是整数也就是  ( t-a[i] ) %d[i] ? (t-a[i])/d[i] : (t-a[i]) /d[i]+1 )

#include
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
const int maxn=200;

int n,t;
int s[maxn];
int d[maxn];

int Solve()
{
    int ans;
    int time=INF;
    for(int i=1;i <= n;++i)
    {
        int curTime=s[i];
        if(s[i] < t)
            curTime += ((t-s[i])/d[i]+((t-s[i])%d[i] == 0 ? 0:1))*d[i];//发符合>=t这辆车的时间
        if(curTime < time)//如果时间早的话  更新
        {
            time=curTime;
            ans=i;
        }
    }
    return ans;
}
int main()
{
    scanf("%d%d",&n,&t);
    for(int i=1;i <= n;++i)
        scanf("%d%d",s+i,d+i);

    printf("%d\n",Solve());

    return 0;
}

B. Serval and Toy Bricks

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.

He has a special interest to create difficult problems for others to solve. This time, with many 1×1×11×1×1 toy bricks, he builds up a 3-dimensional object. We can describe this object with a n×mn×m matrix, such that in each cell (i,j)(i,j), there are hi,jhi,j bricks standing on the top of each other.

However, Serval doesn't give you any hi,jhi,j, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are mm columns, and in the ii-th of them, the height is the maximum of h1,i,h2,i,…,hn,ih1,i,h2,i,…,hn,i. It is similar for the left view, where there are nn columns. And in the top view, there is an n×mn×m matrix ti,jti,j, where ti,jti,j is 00 or 11. If ti,jti,j equals 11, that means hi,j>0hi,j>0, otherwise, hi,j=0hi,j=0.

However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try?

Input

The first line contains three positive space-separated integers n,m,hn,m,h (1≤n,m,h≤1001≤n,m,h≤100) — the length, width and height.

The second line contains mm non-negative space-separated integers a1,a2,…,ama1,a2,…,am, where aiai is the height in the ii-th column from left to right of the front view (0≤ai≤h0≤ai≤h).

The third line contains nn non-negative space-separated integers b1,b2,…,bnb1,b2,…,bn (0≤bj≤h0≤bj≤h), where bjbj is the height in the jj-th column from left to right of the left view.

Each of the following nn lines contains mm numbers, each is 00 or 11, representing the top view, where jj-th number of ii-th row is 11 if hi,j>0hi,j>0, and 00 otherwise.

It is guaranteed that there is at least one structure satisfying the input.

Output

Output nn lines, each of them contains mm integers, the jj-th number in the ii-th line should be equal to the height in the corresponding position of the top view. If there are several objects satisfying the views, output any one of them.

Examples

input

Copy

3 7 3
2 3 0 0 2 0 1
2 1 3
1 0 0 0 1 0 0
0 0 0 0 0 0 1
1 1 0 0 0 0 0

output

Copy

1 0 0 0 2 0 0
0 0 0 0 0 0 1
2 3 0 0 0 0 0

input

Copy

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

output

Copy

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

题意:

给出主视图,左视图,俯视图,求一种可能的物体

我们可以用主视图把这个物体覆盖,然后根据左视图,再改变每一行的最高高度。最后根据俯视图调整没有方块的地方

#include
using namespace std;
int H[105][105];
int main()
{
    int n,m,h;
    cin>>n>>m>>h;
    for(int i=1;i<=m;i++)//输入主视图
    {
        int x;
        cin>>x;
        for(int j=1;j<=n;j++)
            H[j][i]=x;//覆盖物体
    }
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        for(int j=1;j<=m;j++)
            H[i][j]=min(H[i][j],x);//调整每一行的最高高度,让这一行>=H的变为H
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int x;
            cin>>x;
            if(x==0)//调整空块
                cout<<"0 ";
            else
                cout<

C. Serval and Parenthesis Sequence

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.

In his favorite math class, the teacher taught him the following interesting definitions.

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

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

We define that |s||s| as the length of string ss. A strict prefix s[1…l]s[1…l] (1≤l<|s|)(1≤l<|s|) of a string s=s1s2…s|s|s=s1s2…s|s| is string s1s2…sls1s2…sl. Note that the empty string and the whole string are not strict prefixes of any string by the definition.

Having learned these definitions, he comes up with a new problem. He writes down a string ss containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in ss independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.

After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.

Input

The first line contains a single integer |s||s| (1≤|s|≤3⋅1051≤|s|≤3⋅105), the length of the string.

The second line contains a string ss, containing only "(", ")" and "?".

Output

A single line contains a string representing the answer.

If there are many solutions, any of them is acceptable.

If there is no answer, print a single line containing ":(" (without the quotes).

Examples

input

Copy

6
(?????

output

Copy

(()())

input

Copy

10
(???(???(?

output

Copy

:(

Note

It can be proved that there is no solution for the second sample, so print ":(".

题意:

前缀括号不能完全匹配,但整个字符串的括号必须完全匹配。在?处填")" or " (",不能的话输出 :)

刚开始没理解题意,然后还在纠结样例2  ( 嘤嘤嘤(╥╯^╰╥) )

思路:

括号匹配那,“(”,“)”数目肯定相等,所以n必须是偶数才能继续,

因为前缀括号不能完全匹配,所以前面"("总要比")"多,最后一个也肯定要为“)”

#include
using namespace std;
const int maxn=1e5+5;
int n;
string s;
int main()
{
    cin>>n>>s;
    if(n&1)//n为奇数 不符合
    {
        cout<<":("<0)// 变成(
            {
                s[i]='(';
                cnt--;
                Count++;
            }
            else //变成)
            {
                s[i]=')';
                Count--;
                if(Count<=0)// )比( 多,不符合
                {
                    cout<<":("<

 

 

你可能感兴趣的:(cf,div2)