Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

时间:2023-03-09 15:34:00
Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
http://codeforces.com/contest/831
A. Unimodal Array
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Array of integers is unimodal, if:

  • it is strictly increasing in the beginning;
  • after that it is constant;
  • after that it is strictly decreasing.

The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent.

For example, the following three arrays are unimodal: [5, 7, 11, 11, 2, 1], [4, 4, 2], [7], but the following three are not unimodal:[5, 5, 6, 6, 1], [1, 2, 1, 2], [4, 5, 5, 6].

Write a program that checks if an array is unimodal.

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000) — the elements of the array.

Output

Print "YES" if the given array is unimodal. Otherwise, print "NO".

You can output each letter in any case (upper or lower).

Examples
input
6
1 5 5 5 4 2
output
YES
input
5
10 20 30 20 10
output
YES
input
4
1 2 1 2
output
NO
input
7
3 3 3 3 3 3 3
output
YES
Note

In the first example the array is unimodal, because it is strictly increasing in the beginning (from position 1 to position 2, inclusively), that it is constant (from position 2 to position 4, inclusively) and then it is strictly decreasing (from position 4 to position 6, inclusively).

题意:给你一个序列是否是先严格递增有一个极大值然后递减;递增和递减部分可以没有;

题解:找到出数组的最大值,然后看是否符合条件。下面给出AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define LL long long
using namespace std;
const int maxn=1e2+;
int a[maxn],n;
int main()
{
scanf("%d ",&n); int maxl=,id=;
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>maxl)
{
id=i;maxl=a[i];
}
}
bool flag=true,flag2=true;
if(a[]==a[]&&maxl>a[]||a[n-]==a[n-]&&maxl>a[n-])
{
cout<<"NO\n";
}
else
{
for(int i=;i<n-;i++)
{
if(a[i]<a[i+]&&flag2)
{
continue;
}
else if(a[i]==a[i+]&&flag2)
{
if(a[i]!=maxl)
{
flag=false;break;
}
continue;
}
else if(a[i]>a[i+]&&flag2)
{
if(a[i]!=maxl)
{
flag=false;break;
}
flag2=false;
}
if(!flag2&&a[i]>a[i+])
{
continue;
}
else if(!flag2&&a[i]==a[i+])
{
if(a[i]!=maxl)
{
flag=false;break;
}
continue;
}
else if(!flag2&&a[i]<a[i+])
{
flag=false;
//cout<<1<<endl;
break;
} }
if(flag)
{
cout<<"YES\n";
}
else
{
cout<<"NO\n";
}
} }
B. Keyboard Layouts
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.

You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order.

You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.

Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.

Input

The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout.

The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout.

The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.

Output

Print the text if the same keys were pressed in the second layout.

Examples
input
qwertyuiopasdfghjklzxcvbnm
veamhjsgqocnrbfxdtwkylupzi
TwccpQZAvb2017
output
HelloVKCup2017
input
mnbvcxzlkjhgfdsapoiuytrewq
asdfghjklqwertyuiopzxcvbnm
7abaCABAABAcaba7
output
7uduGUDUUDUgudu7

题意:有两个键盘,不同位置对应不同字符,给出他们在相同位置的字符对应关系,求在第一块键盘打的一个字符串,在第二个键盘会打出什么。

题解:用map进行映射,然后注意大小写的转化就可以。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define LL long long
using namespace std;
const int maxn=1e3+;
int n;
char a[],b[],c[maxn];
int main()
{
map<char, int> map1;
for(int i=;i<;i++)
{
scanf("%c",&a[i]);
map1.insert(map<char, int>::value_type(a[i], i));
}
getchar();
for(int i=;i<;i++)
{
scanf("%c",&b[i]);
}
/* for(int i=0;i<26;i++)
{
cout<<b[map1[a[i]]];
}
cout<<endl;*/
getchar();
scanf("%s",c);
n=strlen(c);
for(int i=;i<n;i++)
{
if(c[i]>='a'&&c[i]<='z')
{
cout<<b[map1[c[i]]];
}
else if(c[i]>='A'&&c[i]<='Z')
{
cout<<char(b[map1[c[i]-('A'-'a')]]+('A'-'a'));
}
else
{
cout<<c[i];
}
}
cout<<endl; }
C. Jury Marks
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.

Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.

Your task is to determine the number of options for the score the participant could have before the judges rated the participant.

Input

The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.

The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.

The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.

Output

Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).

Examples
input
4 1
-5 5 0 20
10
output
3
input
2 2
-2000 -2000
3998000 4000000
output
1
Note

The answer for the first example is 3 because initially the participant could have  - 10, 10 or 15 points.

In the second example there is only one correct initial score equaling to 4 002 000.

题意:给出评委的打分顺序(n个按时间顺序),然后一个人记住了一些打分之后出现的分数(k个),让你找到初始分数可能的个数。

题解:说实话当时,比赛时看了半天也没看懂题意(我英语太菜^_^),,,,,

先输入数组a时预处理储存前缀和,用vis数组来标记他出现过,加+N是因为a[i]有可能是负数,然后是数组B,再把b,sort一下,

t=b[1]-a[i]假定t就为初始分数,用一个循环来寻找所有的a[i]是否出现K次如果是ans++;下面是代码
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=2e3+;
const int N = ;
int n,k,ans=;
bool vis[];
int a[maxn],b[maxn];
set<int>s;
int main()
{
scanf("%d %d",&n,&k);
a[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-];
vis[a[i]+N]=true;
}
for(int j=;j<=k;j++)
{
scanf("%d",&b[j]);
}
sort(b+,b+k+);
for(int i=;i<=n;i++)
{
int t=b[]-a[i];int count=;
if(s.count(t))continue;
s.insert(t);
for(int j=;j<=k;j++)
{
count+=vis[b[j]-t+N];
}
if(count==k)ans++;
}
cout<<ans<<endl; }
D. Office Keys
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.

You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.

Input

The first line contains three integers nk and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.

The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.

The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.

Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.

Output

Print the minimum time (in seconds) needed for all n to reach the office with keys.

Examples
input
2 4 50
20 100
60 10 40 80
output
50
input
1 2 10
11
15 7
output
7
Note

In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50seconds. Thus, after 50 seconds everybody is in office with keys.

题意:一条线上有N个人K个钥匙,以及一扇门的位置P,给出人和钥匙的位置,求所有的人拿不同钥匙然后通过这扇门最短的时间(好不合理的逻辑--人可以先通过门到另一边拿钥匙在过门)无语中,,。

题解:贪心,n个人肯定是拿连续的n个钥匙才能最小然后枚举每次更新最小值;

#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e3+;
const int inf=0x3f3f3f3f3f;
const int N = ;
int n,k,p,ans=;
bool vis[];
int a[maxn],b[*maxn];
int main()
{
int l=,r=;
scanf("%d %d %d",&n,&k,&p);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(int i=;i<k;i++)
{
scanf("%d",&b[i]);
}
sort(b,b+k);
int ans=inf*;
for(int j=;j<=k-n;j++)
{
int mt=;
for(int i=;i<n;i++)
{ mt=max(mt,abs(a[i]-b[j+i])+abs(p-b[j+i]));
}
ans=min(ans,mt);
}
//cout<<ans<<endl;
printf("%d\n",ans); }
E. Cards Sorting
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.

Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.

You are to determine the total number of times Vasily takes the top card from the deck.

Input

The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.

The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.

Output

Print the total number of times Vasily takes the top card from the deck.

Examples
input
4
6 3 1 2
output
7
input
1
1000
output
1
input
7
3 3 3 3 3 3 3
output
7
Note

In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.

题意:就是抽牌从最小开始抽出,如果不是最小放后面,继续抽,问抽完所有牌的的抽牌次数

题解:用线段数,0表示此牌已经抽出,1表示此牌还未抽出,这样抽牌次数的话可以用线段树快速求的,所以我在输入的时候先用结构体储存牌的位置和值,

然后每次抽出最小的牌,第一张是特判,然后每一张考虑与前一张的位置关系如果后一张在前一张的后面就只要求区间前一张到后一张位置的和,前一张在后面的话就求区间前一张到N,和后一张到1的(不理解的话可以画画图)。但这样还是不够得因为还有相同值的牌由于前的位置关系,会造另一种大小关系,比如说这组数据

5

12 2 2 12 1;

我们正常抽只会用十次,但用刚才的方法要用11次至于为什么,抽到最后是12 12 的时候其实后面那个是先输入的,所以会多抽一次(我在这wa了一年),所以在后面我们还得加一个优化,相同的直接抽。下面代码,我把错的那部分注释掉。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=4e5+;
int tree[maxn],n;
struct node
{
int i,pos;
}a[(maxn>>)+];
void push(int rt)
{
tree[rt]=tree[rt<<]+tree[rt<<|];
return;
}
bool cmp(const node a,const node b)
{
if(a.i==b.i)
return a.pos<b.pos;
return a.i<b.i;
}
void built(int l,int r,int rt)
{
if(l==r)
{
tree[rt]=;
return ;
}
int mid=(l+r)>>;
built(l,mid,rt<<);
built(mid+,r,rt<<|);
push(rt);
return;
}
int query(int l,int r,int L,int R,int rt)
{
if(r<L||l>R)
{
return ;
}
else if(r<=R&&l>=L)
{
return tree[rt];
}
int mid=(r+l)>>;
return query(l,mid,L,R,rt<<)+query(mid+,r,L,R,rt<<|);
}
void update(int l,int r,int loc,int rt)
{
if(l==r)
{
tree[rt]=;
return;
}
int mid=(r+l)>>;
if(loc<=mid)
update(l,mid,loc,rt<<);
else
update(mid+,r,loc,rt<<|);
push(rt);
return;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].i);
a[i].pos=i;
}
sort(a+,a+n+,cmp);
built(,n,);
//cout<<query(1,n,1,n,1)<<endl;
// int ans=0;
/* int index=0,now=1;
for(int i=1;i<=n;i++)
{
int last=i+1,mi=a[last].pos,mx=mi;
while(last<n&&a[last].i==a[last+1].i)
{
int p=a[++last].pos;
mx=p;
if(p<now)
{
mi=p;
}
}
if(i==1)
{
ans+=a[i].pos;
update(1,n,a[i].pos,1);
}
else
{
if(a[i-1].pos<a[i].pos)
{
ans+=query(1,n,a[i-1].pos,a[i].pos,1);
update(1,n,a[i].pos,1);
}
else
{
ans+=(query(1,n,a[i-1].pos,n,1)+query(1,n,1,a[i].pos,1));
update(1,n,a[i].pos,1);
}
}
}
printf("%d\n",ans);*/
long long ans=;
int index=,now=;
while(index<n){
int last=index+,mi=a[last].pos,mx=mi;
while(last<n&&a[last].i==a[last+].i)
{
int p=a[++last].pos;
mx=p;
if(p<now)
{
mi=p;
}
}
if(mi<now){
ans+=query(,n,now,n,)+query(,n,,mi,);
now =mi;
}
else
{
ans += query(,n,now,mx,) ;
now = mx;
}
while(++index<=last)
{
update(,n,a[index].pos,);
}
index--;
}
printf("%I64d\n",ans); }