Codeforces Round #353 (Div. 2) A B C D map迭代器



链接:戳这里


A. Infinite Sequence
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that si = b. Of course, you are the person he asks for a help.

Input
The first line of the input contain three integers a, b and c ( - 109 ≤ a, b, c ≤ 109) — the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.

Output
If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (without quotes).

Examples
input
1 7 3
output
YES
input
10 10 0
output
YES
input
1 -4 5
output
NO
input
0 60 50
output
NO
Note
In the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element.

In the second sample, the favorite integer of Vasya is equal to the first element of the sequence.

In the third sample all elements of the sequence are greater than Vasya's favorite integer.

In the fourth sample, the sequence starts from 0, 50, 100, and all the following elements are greater than Vasya's favorite integer.


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll a,b,c;
int main(){
    cin>>a>>b>>c;
    if(c==0){
        if(a==b) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        return 0;
    }
    if((b-a)%c==0 && (b-a)/c>=0){
        cout<<"YES"<<endl;
        return 0;
    }
    cout<<"NO"<<endl;
    return 0;
}


B. Restoring Painting
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.


The painting is a square 3 × 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.
The sum of integers in each of four squares 2 × 2 is equal to the sum of integers in the top left square 2 × 2.
Four elements a, b, c and d are known and are located as shown on the picture below.

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.


Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.


Input
The first line of the input contains five integers n, a, b, c and d (1 ≤ n ≤ 100 000, 1 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.


Output
Print one integer — the number of distinct valid squares.


Examples
input
2 1 1 1 2
output
2
input
3 3 1 2 3
output
6
Note
Below are all the possible paintings for the first sample. 


In the second sample, only paintings displayed below satisfy all the rules. 







代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,a,b,c,d;
int x[10];
int main(){
    scanf("%d",&n);
    cin>>a>>b>>c>>d;
    ll ans=0;
    for(int i=1;i<=n;i++){
        ll C=b+i-c;
        ll B=a+i-d;
        ll D=a+b+i-c-d;
        if(C>=1 && C<=n && B>=1 && B<=n &&D>=1 && D<=n)
            ans++;
    }
    printf("%I64d\n",ans*n);
    return 0;
}


C. Money Transfers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.

Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.

There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.

Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.

The second line contains n integers ai ( - 109 ≤ ai ≤ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all ai is equal to 0.

Output
Print the minimum number of operations required to change balance in each bank to zero.

Examples
input
3
5 0 -5
output
1
input
4
-1 0 1 0
output
2
input
4
1 2 3 -6
output
3
Note
In the first sample, Vasya may transfer 5 from the first bank to the third.

In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.

In the third sample, the following sequence provides the optimal answer:

transfer 1 from the first bank to the second bank;
transfer 3 from the second bank to the third;
transfer 6 from the third bank to the fourth.


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int a[100100];
ll c[100100],sum[100100];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++) c[i]=-sum[i-1];
    sort(c+1,c+n+1);
    int num,ans=0;
    for(int i=1;i<=n;i++){
        num=1;
        while(c[i]==c[i+1] && i+1<=n){
            num++;
            i++;
        }
        ans=max(ans,num);
    }
    cout<<n-ans<<endl;
    return 0;
}


D. Tree Construction
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.

You are given a sequence a, consisting of n distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.

First element a1 becomes the root of the tree.
Elements a2, a3, ..., an are added one by one. To add element ai one needs to traverse the tree starting from the root and using the following rules:
The pointer to the current node is set to the root.
If ai is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.
If at some point there is no required child, the new node is created, it is assigned value ai and becomes the corresponding child of the current node.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the length of the sequence a.

The second line contains n distinct integers ai (1 ≤ ai ≤ 109) — the sequence a itself.

Output
Output n - 1 integers. For all i > 1 print the value written in the node that is the parent of the node with value ai in it.

Examples
input
3
1 2 3
output
1 2
input
5
4 2 3 1 6
output
4 2 2 4
Note
Picture below represents the tree obtained in the first sample.


Picture below represents the tree obtained in the second sample.


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
struct node{
    int v,id;
    bool operator < (const node a) const{
        return v<a.v;
    }
}s[100100],p[100100];
map<int ,int> mp;
int l[100100],r[100100];
int anw[100100];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&s[i].v);
        s[i].id=i;
    }
    sort(s+1,s+n+1);
    for(int i=1;i<=n;i++){
        p[s[i].id].v=i;
        p[s[i].id].id=s[i].v;
    }
    map<int ,int>::iterator it;
    mp[0]=1;
    mp[100001]=1;
    r[0]=-1;
    l[100001]=-1;
    for(int i=1;i<=n;i++){
        if(mp.size()==2) {
            mp[p[i].v]=1;
            continue;
        }
        it=mp.lower_bound(p[i].v);
        int x=it->first;
        it--;
        if(x<p[i].v) r[x]=p[i].v;
        else {
            if(l[x]==0) l[x]=p[i].v;
            else {
                x=it->first;
                r[x]=p[i].v;
            }
        }
        mp[p[i].v]=1;
    }
    for(int i=1;i<=n;i++){
        anw[l[i]]=i;
        anw[r[i]]=i;
    }
    for(int i=1;i<=n;i++) anw[i]=s[anw[i]].v;
    for(int i=1;i<=n;i++){
        if(anw[p[i].v]==0) continue;
        cout<<anw[p[i].v]<<" ";
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值