Educational Codeforces Round 35 (Rated for Div. 2) A B C D

A. Nearest Minimums
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

Input

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Output

Print the only number — distance between two nearest minimums in the array.

Examples
Input
2
3 3
Output
1
Input
3
5 6 5
Output
2
Input
9
2 1 3 5 4 1 2 3 1
Output
3

思路:输入的时候搜一下最小值并记录一下位置,然后再找出最小位置


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[100005];
int b[100000];
int main()
{
    int n;
    cin>>n;
    int mi=1000000000;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]<mi){mi=a[i];ans=0;b[ans++]=i;}
        else if(a[i]==mi){b[ans++]=i;}
    }


    int sum=1000000000;
    for(int i=1;i<ans;i++)
    {
        if((b[i]-b[i-1])<sum)sum=b[i]-b[i-1];
    }
    cout<<sum<<endl;
    return 0;
}


B. Two Cakes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.

Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:

  1. Each piece of each cake is put on some plate;
  2. Each plate contains at least one piece of cake;
  3. No plate contains pieces of both cakes.

To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.

Help Ivan to calculate this number x!

Input

The first line contains three integers n, a and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.

Output

Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.

Examples
Input
5 2 3
Output
1
Input
4 7 10
Output
3


思路:按题目要求搜索一下

代码:
#include <iostream>

using namespace std;

int main()
{
int n, a, b;
cin >> n >> a >> b;

int sum1 = min(a, b);
int sum2 = max(a, b);
int ans = 1,mi = 0;
while (ans < n) {
	int cx = n - ans;
	int smc = sum1/ans;
	int mxc = sum2/cx;
	mi = max(mi,min(smc,mxc));
	ans++;
}

cout << mi;
return 0;
}


C. Three Garlands
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.

When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + ki, x + 2ki, x + 3ki and so on.

Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.

Help Mishka by telling him if it is possible to do this!

Input

The first line contains three integers k1, k2 and k3 (1 ≤ ki ≤ 1500) — time intervals of the garlands.

Output

If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.

Otherwise, print NO.

Examples
Input
2 2 3
Output
YES
Input
4 2 3
Output
NO


思路:找规律,一共四种情况

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1550];
int main()
{
    int q,w,e;
    cin>>q>>w>>e;
    a[q]++;a[w]++;a[e]++;
    if(a[1]>0||a[2]>=2||a[3]>=3||(a[4]>=2&&a[2]==1))
    cout<<"YES"<<endl;
    else cout<<"NO"<<endl;


    return 0;
}

D. Inversion Counting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).

You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].

After each query you have to determine whether the number of inversions is odd or even.

Input

The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.

The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.

Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.

Output

Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.

Examples
Input
3
1 2 3
2
1 2
2 3
Output
odd
even
Input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
Output
odd
odd
odd
even

思路:先算一下逆序数的个数,然后交换的数的顺序必改变。然后输出即可,刚开始用了树状数组,麻烦了

代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a[1550];
int c[1550];

int lowbit(int x)
{
    return x&(-x);
}

void add(int x,int v)
{
    while(x<=1550)
    {
        c[x]+=v;
        x+=lowbit(x);
    }

}

long long sum(int x)
{
    long long sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    int n,m;
    while(~scanf("%d",&n))
    {
    long long ans=0;
    memset(c,0,sizeof(c));
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        add(a[i],1);
        ans+=i-sum(a[i]-1);
    }
   // cout<<ans<<endl;

    scanf("%d",&m);
    int q,w,vv,v1;
    while(m--)
    {

            scanf("%d%d",&q,&w);
            q--;w--;
            int su=w-q;
            if(su%2==0)su=su/2;
            else su=(su+1)/2;
            ans+=su;
            //cout<<ans<<endl;
            if(ans%2==0)cout<<"even"<<endl;
            else cout<<"odd"<<endl;


    }

    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值