2019/4/8 CF训练Codeforces Global Round 2(A)补题(B,C,D,E)未补完(F,G,H)

A. Ilya and a Colorful Walk

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ilya lives in a beautiful city of Chordalsk.

There are nn houses on the street Ilya lives, they are numerated from 11 to nn from left to right; the distance between every two neighboring houses is equal to 11 unit. The neighboring houses are 11 and 22, 22 and 33, ..., n−1n−1 and nn. The houses nn and 11 are not neighboring.

The houses are colored in colors c1,c2,…,cnc1,c2,…,cn so that the ii-th house is colored in the color cici. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.

Ilya wants to select two houses ii and jj so that 1≤i<j≤n1≤i<j≤n, and they have different colors: ci≠cjci≠cj. He will then walk from the house iito the house jj the distance of (j−i)(j−i) units.

Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.

Help Ilya, find this maximum possible distance.

Input

The first line contains a single integer nn (3≤n≤3000003≤n≤300000) — the number of cities on the street.

The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n) — the colors of the houses.

It is guaranteed that there is at least one pair of indices ii and jj so that 1≤i<j≤n1≤i<j≤n and ci≠cjci≠cj.

Output

Print a single integer — the maximum possible distance Ilya can walk.

Examples

input

Copy

5
1 2 3 2 3

output

Copy

4

input

Copy

3
1 2 1

output

Copy

1

input

Copy

7
1 1 3 1 1 1 1

output

Copy

4

Note

In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5−1=45−1=4 units.

In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 11 unit.

In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7−3=47−3=4 units.

原地址在这里

题意:

题目会给你n个数,这些书可能是重复的,在这里面,llya喜欢走路,但是走路他只会走这里面最终的和开始不同的地方,比如:1 1 3 4 2 ,他只会取到3,4,2的地方停下,不会再1停下,他想询问你走的最长长度,题目保证至少有两个不宜一样的数。

题解:

既然是A题不管了,暴力吧,只需要贪心一下,看左右那个最多而已。

我居然还错了一发,真是的,原来是自己的一个计算出错了。。。。。。最后的那个max里面tmp1居然没减1。。。

代码:

#include <bits/stdc++.h>
using namespace std ;

int a[300005] ;

int main()
{
    int n ;
    scanf("%d" , &n) ;
    for(int i = 1 ; i <= n ; i ++) scanf("%d" , &a[i]) ;
    int ans = 0 , tmp = 0 , tmp1 = 0 ;
    for(int i = 1 ; i <= n ; i ++)
    {
        if(a[i] != a[n])
        {
            tmp = i ;
            break ;
        }
    }
    for(int i = n ; i >= 1 ; i --)
    {
        if(a[i] != a[1])
        {
            tmp1 = i ;
            break ;
        }
    }
    ans = max(n - tmp , tmp1 - 1) ;
    printf("%d\n" , ans) ;
    return 0 ;
}

补题:

B. Alyona and a Narrow Fridge

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alyona has recently bought a miniature fridge that can be represented as a matrix with hh rows and 22 columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.

An example of a fridge with h=7h=7 and two shelves. The shelves are shown in black. The picture corresponds to the first example.

Alyona has nn bottles of milk that she wants to put in the fridge. The ii-th bottle is aiai cells tall and 11 cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.

Alyona is interested in the largest integer kk such that she can put bottles 11, 22, ..., kk in the fridge at the same time. Find this largest kk.

Input

The first line contains two integers nn and hh (1≤n≤1031≤n≤103, 1≤h≤1091≤h≤109) — the number of bottles and the height of the fridge.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤h1≤ai≤h) — the heights of the bottles.

Output

Print the single integer kk — the maximum integer such that Alyona can put the bottles 11, 22, ..., kk in the fridge at the same time. If Alyona can put all bottles in the fridge, print nn. It is easy to see that Alyona can always put at least one bottle in the fridge.

Examples

input

Copy

5 7
2 3 5 4 1

output

Copy

3

input

Copy

10 10
9 1 1 1 1 1 1 1 1 1

output

Copy

4

input

Copy

5 10
3 1 4 2 4

output

Copy

5

Note

One of optimal locations in the first example is shown on the picture in the statement.

One of optimal locations in the second example is shown on the picture below.

One of optimal locations in the third example is shown on the picture below.

原地址在这

题意:

给你一个高度为h的货物架,然后可以放东西,这个货物架只有两个宽,然后给你n中高度的物品,叫你放进去,可以放的最大瓶数是多少,在这里,你可以加上任意数量的隔板,可以分开几个空间,但是注意,在放货物时,你必须按照当前给你的顺序进行安放。

题解:

说实话,自己那么菜,真的没看出这题怎么做,知识在那里暴力着打,其实如果算算时间复杂度还是有点高的,暴力就是N^2logN(^代表次方)的复杂度,算起来还是满大的,不过后面听他们说还是能过去.....好吧,不过停留吕大佬的讲解,觉得还是这样做吧,用二分做,我们对瓶子数进行二分,再判断该取到多少的瓶数时,你只需要将前面的进行从大到小排序后,两两的放进去,取其中的最大值,判断是否超出高度就行了。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std ;

int a[1005] , n , h ;

bool cmp(int a , int b) {return  a > b ;}

bool check(int r)
{
    int b[1005] ;
    for(int i = 0 ; i < r ; i ++) b[i] = a[i] ;
    sort(b , b + r , cmp) ;
    ll he = 0 ;
    if(r & 1)
    {
        for(int i = 0 ; i < r - 1 ; i += 2) he += max(1ll * b[i] , 1ll * b[i + 1]) ;
        he += b[r - 1] ;
    }
    else
    {
        for(int i = 0 ; i < r ; i += 2) he += max(1ll * b[i] , 1ll * b[i + 1]) ;
    }
    if(he <= 1ll * h) return true ;
    else return false ;
}

int main()
{
    scanf("%d%d" , &n , &h) ;
    for(int i = 0 ; i < n ; i ++) scanf("%d" , &a[i]) ;
    int l = 1 , r = n ;
    while(l < r)
    {
        int mid = (l + r + 1) >> 1 ;
        if(check(mid)) l = mid ;
        else r = mid - 1 ;
    }
    printf("%d\n" , l) ;
    return 0;
}

C. Ramesses and Corner Inversion

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task.

You are given two matrices AA and BB of size n×mn×m, each of which consists of 00 and 11 only. You can apply the following operation to the matrix AA arbitrary number of times: take any submatrix of the matrix AA that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain 00, will be replaced by 11, and all corners of the submatrix that contain 11, will be replaced by 00). You have to answer whether you can obtain the matrix BB from the matrix AA.

An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow.

Ramesses don't want to perform these operations by himself, so he asks you to answer this question.

A submatrix of matrix MM is a matrix which consist of all elements which come from one of the rows with indices x1,x1+1,…,x2x1,x1+1,…,x2 of matrix MM and one of the columns with indices y1,y1+1,…,y2y1,y1+1,…,y2 of matrix MM, where x1,x2,y1,y2x1,x2,y1,y2 are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells (x1,y1)(x1,y1), (x1,y2)(x1,y2), (x2,y1)(x2,y1), (x2,y2)(x2,y2), where the cell (i,j)(i,j) denotes the cell on the intersection of the ii-th row and the jj-th column.

Input

The first line contains two integers nn and mm (1≤n,m≤5001≤n,m≤500) — the number of rows and the number of columns in matrices AA and BB.

Each of the next nn lines contain mm integers: the jj-th integer in the ii-th line is the jj-th element of the ii-th row of the matrix AA (0≤Aij≤10≤Aij≤1).

Each of the next nn lines contain mm integers: the jj-th integer in the ii-th line is the jj-th element of the ii-th row of the matrix BB (0≤Bij≤10≤Bij≤1).

Output

Print "Yes" (without quotes) if it is possible to transform the matrix AA to the matrix BB using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower).

Examples

input

Copy

3 3
0 1 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0

output

Copy

Yes

input

Copy

6 7
0 0 1 1 0 0 1
0 1 0 0 1 0 1
0 0 0 1 0 0 1
1 0 1 0 1 0 0
0 1 0 0 1 0 1
0 1 0 1 0 0 1
1 1 0 1 0 1 1
0 1 1 0 1 0 0
1 1 0 1 0 0 1
1 0 1 0 0 1 0
0 1 1 0 1 0 0
0 1 1 1 1 0 1

output

Copy

Yes

input

Copy

3 4
0 1 0 1
1 0 1 0
0 1 0 1
1 1 1 1
1 1 1 1
1 1 1 1

output

Copy

No

Note

The examples are explained below.

Example 1.

Example 2.

Example 3.

原地址在这

题意:

就是给你两个矩阵A和B,问你能都将A晋国若干次的转换变成B,在这里定义了一个转换的规则,就是你只能选择一个大于等于两行两列的子矩阵,将其的四个角上的01数字转换,也就是0变1,1变0,回答就是yes或no,你可以任意大小写。

题解:

一开始我时懵的,不知道怎么做。。。。暴力又会超时,搜索吧,好像不行,后面听完他们的讲解后,才知道。。。

就是按照他的规则,我们只能同时改变行或列的两个,这样改变的格子一定时偶数,那么这样的话,如果A和B不一样的行的格子或列的格子有奇数个那么就一定不能,否则一定能。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std ;

int Mapa[505][505] , Mapb[505][505] ;

int main()
{
    int n , m ;
    scanf("%d%d" , &n , &m) ;
    for(int i = 0 ; i < n ; i ++)
    {
        for(int j = 0 ; j < m ; j ++)
        {
            scanf("%d" , &Mapa[i][j]) ;
        }
    }
    for(int i = 0 ; i < n ; i ++)
    {
        for(int j = 0 ; j < m ; j ++)
        {
            scanf("%d" , &Mapb[i][j]) ;
        }
    }
    int flag = 0 ;
    for(int i = 0 ; i < n ; i ++)
    {
        int num = 0 ;
        for(int j = 0 ; j < m ; j ++)
        {
            if(Mapa[i][j] != Mapb[i][j]) num ++ ;
        }
        if(num & 1)
        {
            flag = 1 ;
            break ;
        }
    }
    for(int i = 0 ; i < m ; i ++)
    {
        int num = 0 ;
        for(int j = 0 ; j < n ; j ++)
        {
            if(Mapa[j][i] != Mapb[j][i]) num ++ ;
        }
        if(num & 1)
        {
            flag = 1 ;
            break ;
        }
    }
    printf("%s\n" , flag ? "NO" : "YES") ;
	return 0 ;
}

D. Frets On Fire

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.

In return, the fleas made a bigger ukulele for her: it has nn strings, and each string has (1018+1)(1018+1) frets numerated from 00 to 10181018. The fleas use the array s1,s2,…,sns1,s2,…,sn to describe the ukulele's tuning, that is, the pitch of the jj-th fret on the ii-th string is the integer si+jsi+j.

Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.

Each question is in the form of: "How many different pitches are there, if we consider frets between ll and rr (inclusive) on all strings?"

Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!

Formally, you are given a matrix with nn rows and (1018+1)(1018+1) columns, where the cell in the ii-th row and jj-th column (0≤j≤10180≤j≤1018) contains the integer si+jsi+j. You are to answer qq queries, in the kk-th query you have to answer the number of distinct integers in the matrix from the lklk-th to the rkrk-th columns, inclusive.

Input

The first line contains an integer nn (1≤n≤1000001≤n≤100000) — the number of strings.

The second line contains nn integers s1,s2,…,sns1,s2,…,sn (0≤si≤10180≤si≤1018) — the tuning of the ukulele.

The third line contains an integer qq (1≤q≤1000001≤q≤100000) — the number of questions.

The kk-th among the following qq lines contains two integers lklk,rkrk (0≤lk≤rk≤10180≤lk≤rk≤1018) — a question from the fleas.

Output

Output one number for each question, separated by spaces — the number of different pitches.

Examples

input

Copy

6
3 1 4 1 5 9
3
7 7
0 2
8 17

output

Copy

5 10 18

input

Copy

2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000

output

Copy

2 1500000000000000000

Note

For the first example, the pitches on the 66 strings are as follows.

 

Fret:0,1,2,3,4,5,6,7,8,9......

   s1:3,4,5,6,7,8,9,10,11,12...........

   s2:1,2,3,4,5,6,7,8,9,10............

   s3:4,5,6,7,8,9,10,11,12,13........

   s4:1,2,3,4,5,6,7,8,9,10............

   s5:5,6,7,8,9,10,11,12,13,14........

   s6:9,10,11,12,13,14,15,16,17,18.......

There are 55 different pitches on fret 77 — 8,10,11,12,168,10,11,12,16.

There are 1010 different pitches on frets 0,1,20,1,2 — 1,2,3,4,5,6,7,9,10,111,2,3,4,5,6,7,9,10,11.

原地址在这

题意:

给你n个数,规定在下面是这样的:这n个数就是n排,每一排都是无限长的,在一排的第一个数是相对应这n个数的这个数,然后后面一次递增,后一个比前一个大一。

例如:1,2,5,3

a1:1,2,3,4,5,6,7,8,9.........

a2:2,3,4,5,6,7,8,9,10........

a3:5,6,7,8,9,10,11,12,13.......

a4:3,4,5,6,7,8,9,10,11.........

像上面一样递增下去,然后下面是q个询问,问你在这n排数的l到r区间里面所有数有多少是不一样的,当然,每一排是从0开始的,若求0,1,那么就是第一排和第二排的数:1,2,5,3;2,3,6,4。这些数里面所有不同的数的个数。

题解:

在这里面,你不能去暴力是肯定的,他的区间询问时1e18,每个数也可以是1e18以内的,n和q都是十万,你还想暴力?准备死亡吧。这题需要一点技巧和思维来做。看完题解在哪里想半天才想明白,在这里面你需要很好的思维来察觉这些数的相关联系。

在这里面你可以这样算:(以题目所给样例一来进行解释)

给了你五个数是3,1,4,1,5,9,这样排下来就是如下:

Fret:0,1,2,3,4,5,6,7,8,9......

   s1:3,4,5,6,7,8,9,10,11,12...........

   s2:1,2,3,4,5,6,7,8,9,10............

   s3:4,5,6,7,8,9,10,11,12,13........

   s4:1,2,3,4,5,6,7,8,9,10............

   s5:5,6,7,8,9,10,11,12,13,14........

   s6:9,10,11,12,13,14,15,16,17,18.......(虽然有粘贴的嫌疑。。。。)

1、如果a[i+1] = a[i],那么这排不管在哪里都是多余的,因为他都会和a[i]重叠,所以将其去除

2、在每一个区间,都是在第一个数上进行加的,也就是当前的a[l] = a[i]+l,那么若a[i] - a[j](a[i] > a[j])之间相差的大于a[i] + l,那么就会在后面重复了,比如a[i] = 4,a[j] = 2 ,若区间为3到5,那么2 + 3 = 5,此时4开始和a[i]重复了所以要算其小于r-l+1这个区间的差值

3、在后面就是r-l+1的个数了,因为其区间都大于这个查询区间,所有全都是不重复的,就加起来有多少个就行了

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std ;

ll a[100005] , d[100005] , sum[100005] ;

int main()
{
    int n ;
    scanf("%d" , &n) ;
    for(int i = 0 ; i < n ; i ++) scanf("%I64d" , &a[i]) ;
    sort(a , a + n) ;///将其排序
    int m = unique(a , a + n) - a ;///将其去重,m为去重后的个数
    for(int i = 0 ; i < m - 1 ; i ++) d[i] = a[i + 1] - a[i] ;///求取两个数的前面没有重复的
    sort(d , d + m - 1) ;///排序好进行前缀和求解,最后O(1)给出
    sum[0] = d[0] ;
    for(int i = 1 ; i < m - 1 ; i ++) sum[i] = sum[i - 1] + d[i] ;///前缀和
    int q ;
    scanf("%d" , &q) ;
    while(q --)
    {
        ll l , r ;
        scanf("%I64d%I64d" , &l , &r) ;
        int pos = upper_bound(d , d + m - 1 , r - l) - d ;///在这个区间长度之前的都需要加一遍
        ll ans = sum[pos - 1] + (r - l + 1) * (m - pos) ;///前缀和已经加完了,加上在这个后面的大与这个区间的长度的个数
        printf("%I64d " , ans) ;
    }
    printf("\n") ;
	return 0 ;
}

E. Pavel and Triangles

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pavel has several sticks with lengths equal to powers of two.

He has a0a0 sticks of length 20=120=1, a1a1 sticks of length 21=221=2, ..., an−1an−1 sticks of length 2n−12n−1.

Pavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.

It is forbidden to break sticks, and each triangle should consist of exactly three sticks.

Find the maximum possible number of triangles.

Input

The first line contains a single integer nn (1≤n≤3000001≤n≤300000) — the number of different lengths of sticks.

The second line contains nn integers a0a0, a1a1, ..., an−1an−1 (1≤ai≤1091≤ai≤109), where aiai is the number of sticks with the length equal to 2i2i.

Output

Print a single integer — the maximum possible number of non-degenerate triangles that Pavel can make.

Examples

input

Copy

5
1 2 2 2 2

output

Copy

3

input

Copy

3
1 1 1

output

Copy

0

input

Copy

3
3 3 3

output

Copy

3

Note

In the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): (2^0,2^4,2^4)(2^0,2^4,2^4), (2^1,2^3,2^3)(2^1,2^3,2^3), (2^1,2^2,2^2)(2^1,2^2,2^2).

In the second example, Pavel cannot make a single triangle.

In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): (2^0,20,20)(2^0,2^0,2^0), (2^1,2^1,2^1)(2^1,2^1,2^1), (2^2,2^2,2^2)(2^2,2^2,2^2).

(^代表次方)

原地址在这

题意:

就是给你n个数,这些数代表编号为i的木棍的个数,标号i的木棍长度是2^i,求你在这些木棍可以组成最多的三角形是多少

题解:

后面听见他们说后,觉得这个其实好像理解后更加简单,你只需要每次都判断当前输入的木棍就行了,第一次输入,能等边就等边,剩下来的在这后面就直接进行等腰的操作,不过不能将其用当前的两个木棍后后面的一个组成,因为相差两倍,那么一定两个小的相加不大于后面一个大的木棍,若后面的不够,那么就去小的那个,剩下就不用了

代码:

#include <bits/stdc++.h>
using namespace std ;

long long a[300005] ;

int main()
{
    int n ;
    scanf("%d" , &n) ;
    long long ans = 0 , num = 0 ;
    for(int i = 1 ; i <= n ; i ++)
    {
        scanf("%I64d" , &a[i]) ;
        if(num)
        {
            int tmp = min(num , a[i] >> 1) ;
            ans += tmp ;
            num -= tmp ;
            a[i] -= tmp << 1 ;
        }
        ans += a[i] / 3 ;
        num += a[i] % 3 ;
    }
    printf("%I64d\n" , ans) ;
    return 0 ;
}

菜得不一样,菜出新高度。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值