Codeforces Round #556 Div. 2
手速场,输了手速,在线掉分。
A. Stock Arbitraging
Welcome to Codeforces Stock Exchange! We’re pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope you’ll still be able to make profit from the market!
In the morning, there are n opportunities to buy shares. The i-th of them allows to buy as many shares as you want, each at the price of si bourles.
In the evening, there are m opportunities to sell shares. The i-th of them allows to sell as many shares as you want, each at the price of bi bourles. You can’t sell more shares than you have.
It’s morning now and you possess r bourles and no shares.
What is the maximum number of bourles you can hold after the evening?
Input
The first line of the input contains three integers n,m,r (1≤n≤30, 1≤m≤30, 1≤r≤1000) — the number of ways to buy the shares on the market, the number of ways to sell the shares on the market, and the number of bourles you hold now.
The next line contains n integers s1,s2,…,sn (1≤si≤1000); si indicates the opportunity to buy shares at the price of si bourles.
The following line contains m integers b1,b2,…,bm (1≤bi≤1000); bi indicates the opportunity to sell shares at the price of bi bourles.
Output
Output a single integer — the maximum number of bourles you can hold after the evening.
Examples
input
3 4 11
4 2 5
4 4 5 4
output
26
input
2 2 50
5 7
4 2
output
50
Note
In the first example test, you have 11 bourles in the morning. It’s optimal to buy 5 shares of a stock at the price of 2 bourles in the morning, and then to sell all of them at the price of 5 bourles in the evening. It’s easy to verify that you’ll have 26 bourles after the evening.
In the second example test, it’s optimal not to take any action.
大水题,就不说了,但要注意可能买的话会亏钱的情况
#include<bits/stdc++.h>
using namespace std;
int n,m,r;
int mins,maxb;
int main()
{
while (~scanf("%d %d %d", &n, &m, &r)) {
mins = 1001, maxb = 0;
for (int i = 0; i < n; i++) {
int number;
scanf("%d", &number);
if (mins > number)
mins = number;
}
for (int i = 0; i < m; i++) {
int number;
scanf("%d", &number);
if (maxb < number)
maxb = number;
}
cout << max(r, r / mins * maxb + r % mins) << endl;
}
return 0;
}
B. Tiling Challenge
One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile:
By the pieces lay a large square wooden board. The board is divided into n2 cells arranged into n rows and n columns. Some of the cells are already occupied by single tiles stuck to it. The remaining cells are free.
Alice started wondering whether she could fill the board completely using the pieces she had found. Of course, each piece has to cover exactly five distinct cells of the board, no two pieces can overlap and every piece should fit in the board entirely, without some parts laying outside the board borders. The board however was too large for Alice to do the tiling by hand. Can you help determine if it’s possible to fully tile the board?
Input
The first line of the input contains a single integer n (3≤n≤50) — the size of the board.
The following n lines describe the board. The i-th line (1≤i≤n) contains a single string of length n. Its j-th character (1≤j≤n) is equal to “.” if the cell in the i-th row and the j-th column is free; it is equal to “#” if it’s occupied.
You can assume that the board contains at least one free cell.
Output
Output YES if the board can be tiled by Alice’s pieces, or NO otherwise. You can print each letter in any case (upper or lower).
Examples
input
3
#.#
…
#.#
output
YES
input
4
##.#
#…
##.#
output
NO
input
5
#.###
…#
#…
###.#
output
YES
input
5
#.###
…#
#…
…#
#…##
output
NO
Note
The following sketches show the example boards and their tilings if such tilings exist:
水题,直接暴力找就可以了,但注意边界的判断,一大佬就是因为没判边界被人hack掉了。
#include<bits/stdc++.h>
using namespace std;
int n;
bool flag;
bool mmap[55][55];
void judge(int i,int j){
if(i==n-1||i==n-2||j==n-1||j==0){
flag=false;
return;
}
if(mmap[i+1][j+1]&&mmap[i+1][j-1]&&mmap[i+1][j]&&mmap[i+2][j]){
mmap[i+1][j+1]=mmap[i+1][j-1]=mmap[i+1][j]=mmap[i+2][j]=false;
return;
}
else{
flag=false;
return;
}
}
int main(){
flag=true;
memset(mmap,false,sizeof(mmap));
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(getchar()=='.')
mmap[i][j]=true;
}
getchar();
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mmap[i][j]){
judge(i,j);
}
if(!flag)
break;
}
if(!flag)
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
C. Prefix Sum Primes
We’re giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2.
However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get.
Can you win the prize? Hurry up, the bags are waiting!
Input
The first line of the input contains a single integer n (1≤n≤200000) — the number of number tiles in the bag. The following line contains n space-separated integers a1,a2,…,an (ai∈{1,2}) — the values written on the tiles.
Output
Output a permutation b1,b2,…,bn of the input sequence (a1,a2,…,an) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any.
Examples
input
5
1 2 1 2 1
output
1 1 1 2 2
input
9
1 1 2 1 1 1 2 1 1
output
1 1 1 2 1 1 1 2 1
Note
The first solution produces the prefix sums 1,2,3,5,7 (four primes constructed), while the prefix sums in the second solution are 1,2,3,5,6,7,8,10,11 (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
C题也挺水的,只要知道是个spj就可以了,我一开始就是没看到spj,卡了半天…,只有1的时候特判一下就行
#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+5;
bool prime[maxn];
int c[maxn],k;
int a[maxn],b[10];
int main(){
for(int i=2;i<maxn;i++){
if(!prime[i]) c[k++]=i;
for(int j=0;i*c[j]<maxn;j++){
prime[i*c[j]]=true;
if(i%c[j]==0) break;
}
}
int n;
cin>>n;
for(int i=0;i<n;i++){
scanf("%d",a+i);
b[a[i]]++;
}
if(b[2]==0){
for(int i=0;i<n;i++)
printf("1%c",i==n?'\n':' ');
return 0;
}
int x=2;
printf("2");
b[2]--;
for(int i=1;i<n;i++){
if(!prime[x+1]&&b[1]){
printf(" 1");
b[1]--;
x+=1;
}
else if(b[2]){
printf(" 2");
b[2]--;
x+=2;
}
else{
printf(" 1");
b[1]--;
x+=1;
}
}
return 0;
}