A | 2048 Game standard input/output |
| x5275 |
B | Knights standard input/output |
| x3777 |
C | Perfect Team standard input/output |
| x4452 |
D | Make The Fence Great Again standard input/output |
| x1318 |
E | Game With String standard input/output |
| x112 |
F | Choose a Square standard input/output |
| x78 |
G | Graph And Numbers standard input/output |
| x9 |
A. 2048 Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every integer in this multiset is a power of two.
You may perform any number (possibly, zero) operations with this multiset.
During each operation you choose two equal integers from ss, remove them from ss and insert the number equal to their sum into ss.
For example, if s={1,2,1,1,4,2,2}s={1,2,1,1,4,2,2} and you choose integers 22 and 22, then the multiset becomes {1,1,1,4,4,2}{1,1,1,4,4,2}.
You win if the number 20482048 belongs to your multiset. For example, if s={1024,512,512,4}s={1024,512,512,4} you can win as follows: choose 512512 and 512512, your multiset turns into {1024,1024,4}{1024,1024,4}. Then choose 10241024 and 10241024, your multiset turns into {2048,4}{2048,4} and you win.
You have to determine if you can win this game.
You have to answer qq independent queries.
Input
The first line contains one integer qq (1≤q≤1001≤q≤100) – the number of queries.
The first line of each query contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in multiset.
The second line of each query contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤2291≤si≤229) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two.
Output
For each query print YES if it is possible to obtain the number 20482048 in your multiset, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
input
Copy
6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096
output
Copy
YES YES NO NO YES YES
Note
In the first query you can win as follows: choose 512512 and 512512, and ss turns into {1024,64,1024}{1024,64,1024}. Then choose 10241024 and 10241024, and ss turns into {2048,64}{2048,64} and you win.
In the second query ss contains 20482048 initially.
题意:n个2的倍数的数,每次合并两个,问最后能否合并成2048?
分析:
自上而下的凑或者自下而上的凑都可以
自上而下
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
const ll MOD=1e9+7;
int n,sum;
int a[maxn];
map<int,int>mp;
int dfs(int x,int y,int num)
{
//cout<<x<<" "<<y<<" "<<num<<endl;
if(x==0)
return 0;
if(mp[x]>=(num*y)/x)
{
mp[x]-=(num*y)/x;
return 1;
}
else
{
int temp=mp[x];
mp[x]=0;
return dfs(x/2,x,(num*y/x-temp));
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
mp.clear();
int n;
scanf("%d",&n);
int flag=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
if(a[i]==2048)
{
flag=1;
}
}
if(flag==1)
{
printf("YES\n");
continue;
}
flag=dfs(2048,2048,1);
if(flag==1)
{
printf("YES\n");
}
else
printf("NO\n");
}
return 0;
}
自下而上
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
const ll MOD=1e9+7;
int n,sum;
int a[maxn];
map<int,int>mp;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
mp.clear();
scanf("%d",&n);
int flag=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
if(a[i]==2048)
{
flag=1;
}
}
if(flag==1)
{
printf("YES\n");
continue;
}
int k=2048;
for(int i=1; i<=2048; i*=2)
{
if(mp[i]>=k)
{
//cout<<mp[i]<<" "<<k<<endl;
flag=1;
printf("YES\n");
break;
}
else
{
mp[i*2]+=mp[i]/2;
}
k/=2;
}
if(flag==0)
{
printf("NO\n");
continue;
}
}
return 0;
}
B. Knights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a chess board with nn rows and nn columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board.
A knight is a chess piece that can attack a piece in cell (x2x2, y2y2) from the cell (x1x1, y1y1) if one of the following conditions is met:
- |x1−x2|=2|x1−x2|=2 and |y1−y2|=1|y1−y2|=1, or
- |x1−x2|=1|x1−x2|=1 and |y1−y2|=2|y1−y2|=2.
Here are some examples of which cells knight can attack. In each of the following pictures, if the knight is currently in the blue cell, it can attack all red cells (and only them).
A duel of knights is a pair of knights of different colors such that these knights attack each other. You have to put a knight (a white one or a black one) into each cell in such a way that the number of duels is maximum possible.
Input
The first line contains one integer nn (3≤n≤1003≤n≤100) — the number of rows (and columns) in the board.
Output
Print nn lines with nn characters in each line. The jj-th character in the ii-th line should be W, if the cell (ii, jj) contains a white knight, or B, if it contains a black knight. The number of duels should be maximum possible. If there are multiple optimal answers, print any of them.
Example
input
Copy
3
output
Copy
WBW BBB WBW
Note
In the first example, there are 88 duels:
- the white knight in (11, 11) attacks the black knight in (33, 22);
- the white knight in (11, 11) attacks the black knight in (22, 33);
- the white knight in (11, 33) attacks the black knight in (33, 22);
- the white knight in (11, 33) attacks the black knight in (22, 11);
- the white knight in (33, 11) attacks the black knight in (11, 22);
- the white knight in (33, 11) attacks the black knight in (22, 33);
- the white knight in (33, 33) attacks the black knight in (11, 22);
- the white knight in (33, 33) attacks the black knight in (22, 11).
题意:一个n*n的棋盘,放两种不同颜色的马(马走日),使得不同颜色之间可以相互吃的对数最大。
画一下图一看就知道
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
const ll MOD=1e9+7;
int n,sum;
int a[maxn];
map<int,int>mp;
int main()
{
int T;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((i+j)&1)
{
cout<<"B";
}
else
cout<<"W";
}
cout<<endl;
}
return 0;
}
C. Perfect Team
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that cc of your students are coders, mm are mathematicians and xx have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer qq independent queries.
Input
The first line contains a single integer qq (1≤q≤1041≤q≤104) — the number of queries.
Each of the next qq lines contains three integers cc, mm and xx (0≤c,m,x≤1080≤c,m,x≤108) — the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
Output
Print qq integers — the ii-th of them should be the answer to the ii query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
Example
input
Copy
6 1 1 1 3 6 0 0 0 0 0 1 1 10 1 10 4 4 1
output
Copy
1 3 0 0 1 3
Note
In the first example here are how teams are formed:
- the only team of 1 coder, 1 mathematician and 1 without specialization;
- all three teams consist of 1 coder and 2 mathematicians;
- no teams can be formed;
- no teams can be formed;
- one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team;
- one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
题意:q组询问,给你c,m,x,对应c,m,x的人数,小组要求三个人至少有一个c和m即可,问最大分组数?
分析:分类讨论一下即可
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
const ll MOD=1e9+7;
int n,sum;
int a[maxn];
map<int,int>mp;
int main()
{
int T;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int c,m,x;
scanf("%d%d%d",&c,&m,&x);
int ave=(c+m+x)/3;
int ans=min(c,m);
int r=c+m-2*ans;
cout<<min(ans,ave)<<endl;
}
return 0;
}
D. Make The Fence Great Again
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a fence consisting of nn vertical boards. The width of each board is 11. The height of the ii-th board is aiai. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from 22 to nn, the condition ai−1≠aiai−1≠ai holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the ii-th board by 11, but you have to pay bibi rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer qq independent queries.
Input
The first line contains one integer qq (1≤q≤3⋅1051≤q≤3⋅105) — the number of queries.
The first line of each query contains one integers nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of boards in the fence.
The following nn lines of each query contain the descriptions of the boards. The ii-th line contains two integers aiai and bibi (1≤ai,bi≤1091≤ai,bi≤109) — the length of the ii-th board and the price for increasing it by 11, respectively.
It is guaranteed that sum of all nn over all queries not exceed 3⋅1053⋅105.
It is guaranteed that answer to each query will not exceed 10181018.
Output
For each query print one integer — the minimum number of rubles you have to spend to make the fence great.
Example
input
Copy
3 3 2 4 2 1 3 5 3 2 3 2 10 2 6 4 1 7 3 3 2 6 1000000000 2
output
Copy
2 9 0
Note
In the first query you have to increase the length of second board by 22. So your total costs if 2⋅b2=22⋅b2=2.
In the second query you have to increase the length of first board by 11 and the length of third board by 11. So your total costs if 1⋅b1+1⋅b3=91⋅b1+1⋅b3=9.
In the third query the fence is great initially, so you don't need to spend rubles.
题意:n个篱笆,高度为a[i],每单位造价为b[i],要求每相邻两个的篱笆高度不同,可以的操作是增加,求最小花费?
分析:我们发现对于每一个篱笆,无非就三个状态增加0,1,2,且由上一个篱笆同样的三种状态转移过来,DP转移即可。
#include<bits/stdc++.h>
using namespace std;
#define maxn 500005
typedef long long ll;
const ll MOD=1e9+7;
int n,sum;
ll a[maxn],b[maxn];
ll dp[maxn][10];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%lld%lld",&a[i],&b[i]);
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=2;j++)
{
dp[i][j]=1e18+10;
}
}
a[0]=1e18+10;
a[n+1]=1e18+10;
int minn=1e9;
ll ans=0;
for(int i=1; i<=n;i++)
{
for(int j=0;j<=2;j++)
{
for(int k=0;k<=2;k++)
{
if(a[i]+j!=a[i-1]+k)
dp[i][j]=min(dp[i][j],dp[i-1][k]+j*b[i]);
}
}
}
ans=min(dp[n][0],min(dp[n][1],dp[n][2]));
cout<<ans<<endl;
}
return 0;
}