Codeforces Round #417 (Div.2)

Codeforces Round #417 (Div. 2)

A. Sagheer and Crossroads

Sagheer is walking in the street when he comes to an intersection of two roads. Each road can be represented as two parts where each part has 3 lanes getting into the intersection (one for each direction) and 3 lanes getting out of the intersection, so we have 4 parts in total. Each part has 4 lights, one for each lane getting into the intersection (l — left, s — straight, r— right) and a light p for a pedestrian crossing.

An accident is possible if a car can hit a pedestrian. This can happen if the light of a pedestrian crossing of some part and the light of a lane that can get to or from that same part are green at the same time.

Now, Sagheer is monitoring the configuration of the traffic lights. Your task is to help him detect whether an accident is possible.

Input

The input consists of four lines with each line describing a road part given in a counter-clockwise order.

Each line contains four integers l, s, r, p — for the left, straight, right and pedestrian lights, respectively. The possible values are 0 for red light and 1 for green light.

Output

On a single line, print "YES" if an accident is possible, and "NO" otherwise.

Examples

input

1 0 0 1
0 1 0 0
0 0 1 0
0 0 0 1

output

YES

input

0 1 1 0
1 0 1 0
1 1 0 0
0 0 0 1

output

NO

input

1 0 0 0
0 0 0 1
0 0 0 0
1 0 1 0

output

NO

Note

In the first example, some accidents are possible because cars of part 1 can hit pedestrians of parts 1 and 4. Also, cars of parts 2 and 3 can hit pedestrians of part 4.

In the second example, no car can pass the pedestrian crossing of part 4 which is the only green pedestrian light. So, no accident can occur.

题意:有一个分叉路口,每一边有三条路线和一个人行道,问是否有交通事故;

分析:模拟

source code:

#include <bits/stdc++.h>
​
using namespace std;
​
int l[5],r[5],s[5],p[5];
​
int main()
{
​
    for(int i=1;i<=4;i++)
    {
        scanf("%d%d%d%d",&l[i],&s[i],&r[i],&p[i]);
    }
​
    bool flag = true;
​
    if(p[1]==1) {
        if(l[2]||s[3]||r[4]||l[1]||r[1]||s[1])
            flag = false;
    }
​
    if(p[2]) {
        if(r[1]||l[3]||s[4]||r[2]||l[2]||s[2])
            flag = false;
    }
​
    if(p[3]) {
        if(s[1]||r[2]||l[4]||l[3]||r[3]||s[3])
            flag = false;
    }
​
    if(p[4]) {
        if(l[1]||s[2]||r[3]||l[4]||r[4]||s[4])
            flag = false;
    }
​
    if(flag)
        puts("NO");
    else puts("YES");
​
    return 0;
}

B. Sagheer, the Hausmeister

Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has mrooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

Input

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Output

Print a single integer — the minimum total time needed to turn off all the lights.

Examples

input

2 2
0010
0100

output

5

input

3 4
001000
000010
000010

output

12

input

4 3
01110
01110
01110
01110

output

18

Note

In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.

In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.

In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.

题意:从楼梯左边最下方开始,每次走一步,将图中有1的地方熄灯,图中第一列,最后一列是电梯,每次只能走电梯上楼,求最少多少时间将所有灯熄灭;

分析:DP,从一层到另一层发生状态转移,但是有一个方向, 将第 i 层熄灭后,走左边上楼, 将第i层熄灭后,走右边上楼,那么状态转移就是:

需要注意的地方是,边界条件,最后不需要上楼,只有一层的情况;

source code:

#include <bits/stdc++.h>
​
using namespace std;
​
const int inf = 0x3f3f3f3f;
​
char maps[20][105];
int l[20],r[20];
int d[20][2];
​
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
​
    for(int i=n;i>=1;i--) {
        scanf("%s",maps[i]);
        l[i] = m+1;
        r[i] = 0;
        for(int j=0;j<m+2;j++) {
            if(maps[i][j]=='1') {
                r[i] = max(j,r[i]);
                l[i] = min(j,l[i]);
            }
        }
    }
​
    for(int i=n;i>=1;i--) {
        if(r[i]==0)
            n--;
        else break;
    }
​
    for(int i=n;i>=1;i--) {
        l[i] = m - l[i] + 1;
    }
​
    if(n==0) {
        puts("0");
        return 0;
    }
​
    if(n==1) {
        printf("%d
",r[1]);
        return 0;
    }
​
    memset(d,inf,sizeof(d));
    d[1][0] = 2*r[1]+1;
    d[1][1] = m+2;
​
    for(int i=2;i<n;i++) {
        d[i][0] = min(d[i-1][0]+2*r[i],d[i-1][1]+m+1) + 1;
        d[i][1] = min(d[i-1][0]+m+1,d[i-1][1]+2*l[i]) + 1;
​
    }
​
    int ans = min(d[n-1][0]+r[n],d[n-1][1]+l[n]);
​
    printf("%d
",ans);
​
    return 0;
}

C. Sagheer and Nubian Market

On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a**i Egyptian pounds. If Sagheer buys kitems with indices x1, x2, ..., x**k, then the cost of item x**j is axj + x**j·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.

Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?

Input

The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.

The second line contains n space-separated integers a1, a2, ..., a**n (1 ≤ a**i ≤ 105) — the base costs of the souvenirs.

Output

On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these ksouvenirs.

Examples

input

3 11
2 3 5

output

2 11

input

4 100
1 2 5 6

output

4 54

input

1 7
7

output

0 0

Note

In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.

In the second example, he can buy all items as they will cost him [5, 10, 17, 22].

In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.

题意:有n个物品,s块钱,这n个物品有一个基础价值,从这n个物品中选取一些物品,求最多能选多少,但是这n个物品的价值是 基础价值+编号*k(k为你选了几个)

分析:二分选了多少个,直接贪心选择最便宜的;

Source Code:

#include <bits/stdc++.h>
​
using namespace std;
​
const int maxn = 100000 + 5;
long long a[maxn];
long long tmp[maxn];
​
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    long long s;
    scanf("%d%I64d",&n,&s);
​
    for(int i=0;i<n;i++) {
        scanf("%I64d",&a[i]);
    }
​
    long long sum;
    long long ans = 0;
    int l = 0,r= n;
    int ansl;
    while(l<=r) {
        int mid = (r+l)/2;
        for(long long i=0;i<n;i++)
            tmp[i] = a[i]+(i+1)*mid;
​
        sum = 0;
        sort(tmp,tmp+n);
​
        for(long long i=0;i<mid;i++)
            sum+=tmp[i];
        if(sum>s)
            r = mid-1;
        else {
            ansl = mid;
            l = mid+1;
            ans = sum;
        }
    }
​
    printf("%d %I64d
",ansl,ans);
​
​
    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/7079784.html