您的位置:首页 > 其它

【CodeFores-938 】 A B C D

2018-03-01 22:17 155 查看

A - Word Correction

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Victor tries to write his own text editor, with word correction included. However, the rules of word correction are really strange.

Victor thinks that if a word contains two consecutive vowels, then it’s kinda weird and it needs to be replaced. So the word corrector works in such a way: as long as there are two consecutive vowels in the word, it deletes the first vowel in a word such that there is another vowel right before it. If there are no two consecutive vowels in the word, it is considered to be correct.

You are given a word s. Can you predict what will it become after correction?

In this problem letters a, e, i, o, u and y are considered to be vowels.

Input

The first line contains one integer n (1 ≤ n ≤ 100) — the number of letters in word s before the correction.

The second line contains a string s consisting of exactly n lowercase Latin letters — the word before the correction.

Output

Output the word s after the correction.

Examples

inputCopy

5

weird

output

werd

inputCopy

4

word

output

word

inputCopy

5

aaeaa

output

a

Note

Explanations of the examples:

There is only one replace: weird werd;

No replace needed since there are no two consecutive vowels;

aaeaa aeaa aaa aa a.

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

const int N =  1e5+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const LL inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

bool Judge(char a){
return (a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='y');
}
int main(){
int n; string s;
cin>>n>>s;

for(int i=0;i<n;){
if(!Judge(s[i])) putchar(s[i++]);
else {
int j=i;
putchar(s[i]);
while(j<n&&Judge(s[j])) j++  ;
i=j;
}
}
return 0;
}


B -Run For Your Prize

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

You and your friend are participating in a TV show “Run For Your Prize”.

At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You star
13909
t at position 1, your friend — at position 106 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order.

You know that it takes exactly 1 second to move from position x to position x + 1 or x - 1, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend’s speed) at all.

Now you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend.

What is the minimum number of seconds it will take to pick up all the prizes?

Input

The first line contains one integer n (1 ≤ n ≤ 105) — the number of prizes.

The second line contains n integers a1, a2, …, an (2 ≤ ai ≤ 106 - 1) — the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order.

Output

Print one integer — the minimum number of seconds it will take to collect all prizes.

Examples

inputCopy

3

2 3 9

output

8

inputCopy

2

2 999995

output

5

Note

In the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8.

In the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5.

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

const int N = 1e6;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const LL inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

int mp[N+11];
int main(){
int n;scanf("%d",&n);
for(int i=0;i<n;i++){
int zz;
scanf("%d",&zz);
mp[zz]=1;
}

int posa=-1,posb=-1;
for(int i=1,j=N,colc=0;i<=j;i++,j--,colc++){
if(mp[i]) posa=colc;
if(mp[j]) posb=colc;
}
//  printf("%d %d\n",posa,posb);
cout<<max(posa,posb);
return 0;
}


C- Constructing Tests

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Let’s denote a m-free matrix as a binary (that is, consisting of only 1’s and 0’s) matrix such that every square submatrix of size m × m of this matrix contains at least one zero.

Consider the following problem:

You are given two integers n and m. You have to construct an m-free square matrix of size n × n such that the number of 1’s in this matrix is maximum possible. Print the maximum possible number of 1’s in such matrix.

You don’t have to solve this problem. Instead, you have to construct a few tests for it.

You will be given t numbers x1, x2, …, xt. For every , find two integers ni and mi (ni ≥ mi) such that the answer for the aforementioned problem is exactly xi if we set n = ni and m = mi.

Input

The first line contains one integer t (1 ≤ t ≤ 100) — the number of tests you have to construct.

Then t lines follow, i-th line containing one integer xi (0 ≤ xi ≤ 109).

Note that in hacks you have to set t = 1.

Output

For each test you have to construct, output two positive numbers ni and mi (1 ≤ mi ≤ ni ≤ 109) such that the maximum number of 1’s in a mi-free ni × ni matrix is exactly xi. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer  - 1.

Example

inputCopy

3

21

0

1

output

5 2

1 1

-1

分析: 首先对于长度为n一维的数组,我们往其中添加几个0,使之任意m长的连续子序列都可以有一个0,可以知道至少为 n / m 个0 .

现在题目问的是二维的 。

可以知道对于 任意的n和m其最少放 (n / m)^2 个0 就可以满足。

所以可以将问题转化为 n ^2 - floor( n / m )^2 = x 看是否存在n和m使之成立

关键 是处理floor(n/m)的时候 注意点就好了。

代码一: 式子化为 (n + floor( n / m ) ) ( n - floor( n / m )) = x ,枚举x的因子 ,将n和m解出来 ,然后判断一下就好。

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

const int N = 1e6;
const int M = 1e9+111;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const LL inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

int main(){

int t;scanf("%d",&t);
while(t--){
LL x;scanf("%lld",&x);
int flag=0; LL ansn,ansm;
for(LL i=1;i<100000;i++){
if(i*i<=x) continue;
LL k=(LL) sqrt(i*i*1.0-x);
LL m=i/k;
if(k>0 && (i/m)*(i/m) == i*i-x) {
flag=1; ansn=i; ansm=m;
break;
}
}
if(!flag|| x==1 ) puts("-1");
else printf("%lld %lld\n",ansn,ansm);
}
return 0;
}


代码二 : 直接枚举 n ,枚举n的话,我们可以解出m,验证这个m就行

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

const int N = 1e6;
const int M = 1e9+111;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const LL inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

int main(){

int t;scanf("%d",&t);
while(t--){
LL x;scanf("%lld",&x);
int flag=0; LL ansn,ansm;
for(LL i=1;i<100000;i++){
if(i*i<=x) continue;
LL k=(LL) sqrt(i*i*1.0-x);
LL m=i/k;
if(k>0 && (i/m)*(i/m) == i*i-x) {
flag=1; ansn=i; ansm=m;
break;
}
}
if(!flag|| x==1 ) puts("-1");
else printf("%lld %lld\n",ansn,ansm);
}
return 0;
}


D. Buy a Ticket ##

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Musicians of a popular band “Flayer” have announced that they are going to “make their exit” with a world tour. Of course, they will visit Berland as well.

There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from city vi to city ui (and from ui to vi), and it costs wi coins to use this route.

Each city will be visited by “Flayer”, and the cost of the concert ticket in i-th city is ai coins.

You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city i you have to compute the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).

Formally, for every you have to calculate , where d(i, j) is the minimum number of coins you have to spend to travel from city i to city j. If there is no way to reach city j from city i, then we consider d(i, j) to be infinitely large.

Input

The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105).

Then m lines follow, i-th contains three integers vi, ui and wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1012) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v, u) nor (u, v) present in input.

The next line contains n integers a1, a2, … ak (1 ≤ ai ≤ 1012) — price to attend the concert in i-th city.

Output

Print n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).

Examples

inputCopy

4 2

1 2 4

2 3 7

6 20 1 25

output

6 14 1 25

inputCopy

3 3

1 2 1

2 3 1

1 3 1

30 10 20

output

12 10 12

分析:多源最短路问题,我们将建立一个超级原点就好了。

没想到还卡spfa了。TAT 。只好djk

代码

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

const int N = 2e5+11;
const int M = 1e6;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const LL inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/
struct edge{
int to;
LL cost;
};
typedef pair<int,int>P;
int n,m;
vector<edge>G
; LL d
;

void djk(int S){
priority_queue<P, vector < P > ,greater < P >  >que;
fill(d,d+n+1,inff);
d[S]=0;
que.push(P(0,S));
while(!que.empty()){
P p = que.top();que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}

int main(){
scanf("%d%d",&n,&m);
while(m--){
int a,b;LL c;scanf("%d%d%lld",&a,&b,&c);
G[a].push_back(edge{b,2*c});
G[b].push_back(edge{a,2*c});
}
int S=0;
for(int i=1;i<=n;i++) { //  建立超级源点
LL c;scanf("%lld",&c);
G[S].push_back(edge{i,c});
}

djk(S);
for(int i=1;i<=n;i++){
if(i!=1) putchar(' ');
printf("%lld",d[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: