Codeforces Round #784 (Div. 4)+leetcode每日一题(4.26)

115 篇文章 3 订阅
42 篇文章 2 订阅

leetcode每日一题(4.26)

在 n x n 的网格 grid 中,我们放置了一些与 x,y,z 三轴对齐的 1 x 1 x 1 立方体。
每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上。
现在,我们查看这些立方体在 xy 、yz 和 zx 平面上的投影。
投影 就像影子,将 三维 形体映射到一个 二维 平面上。从顶部、前面和侧面看立方时,我们会看到“影子”。
返回 所有三个投影的总面积 。

在这里插入图片描述

输入:[[1,2],[3,4]]
输出:17
解释:这里有该形体在三个轴对齐平面上的三个投影(“阴影部分”)
class Solution {
public:
    int projectionArea(vector<vector<int>>& grid) {
        int area1=0,area2=0,area3=0;
        for(int i=0;i<grid.size();i++)
        {
            int t=-1;
            int s=-1;
            for(int j=0;j<grid[i].size();j++)
            {
                area1+=grid[i][j]?1:0;
                t=max(t,grid[i][j]);
                s=max(s,grid[j][i]);
            }
            area2+=t;
            area3+=s;
        }
        return area1+area2+area3;
    }
};

Codeforces Round #784 (Div. 4)

A. Division?

Codeforces separates its users into 4 divisions by their rating:
For Division 1: 1900≤rating
For Division 2: 1600≤rating≤1899
For Division 3: 1400≤rating≤1599
For Division 4: rating≤1399
Given a rating, print in which division the rating belongs.
Input
The first line of the input contains an integer t (1≤t≤104) — the number of testcases.
The description of each test consists of one line containing one integer rating (−5000≤rating≤5000).
Output
For each test case, output a single line containing the correct division in the format “Division X”, where X is an integer between 1 and 4 representing the division for the corresponding rating.

input

7
-789
1299
1300
1399
1400
1679
2300

output

Division 4
Division 4
Division 4
Division 4
Division 3
Division 2
Division 1
#include <bits/stdc++.h>
using namespace std;
int t;
int n;
void solve()
{
	cin>>n;
	if(n>=1900)puts("Division 1");
	else if(n>=1600)puts("Division 2");
	else if(n>=1400)puts("Division 3");
	else puts("Division 4");
}
int main()
{
	cin>>t;
	while(t--)solve();
} 

B. Triple

Given an array a of n elements, print any value that appears at least three times or print -1 if there is no such value.
Input
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, print any value that appears at least three times or print -1 if there is no such value.

input
7
1
1
3
2 2 2
7
2 2 3 3 4 2 2
8
1 4 3 4 3 2 4 1
9
1 1 1 2 2 2 3 3 3
5
1 5 2 4 3
4
4 4 4 4
Output
-1
2
2
4
3
-1
4
#include <bits/stdc++.h>
using namespace std;
int _;
int n;
void solve()
{
	cin>>n;
	vector<int>v(n+1);
	unordered_map<int,int>mp;
	for(int i=0;i<n;i++)
	{
	    cin>>v[i];
	    mp[v[i]]++;
	}
	bool f=false;
	for(auto x:mp)
	{
		if(x.second>=3)
		{
			f=true;
			cout<<x.first<<endl;
			break;
		}
	}
	if(!f)cout<<-1<<endl;
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

C. Odd/Even Increments

Given an array a=[a1,a2,…,an] of n positive integers, you can do operations of two types on it:
Add 1 to every element with an odd index. In other words change the array as follows: a1:=a1+1,a3:=a3+1,a5:=a5+1,….
Add 1 to every element with an even index. In other words change the array as follows: a2:=a2+1,a4:=a4+1,a6:=a6+1,….
Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations.
Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times.
Input
The first line contains an integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains an integer n (2≤n≤50) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤103) — the elements of the array.
Note that after the performed operations the elements in the array can become greater than 103.
Output
Output t lines, each of which contains the answer to the corresponding test case. As an answer, output “YES” if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers, and “NO” otherwise.
You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

input
4
3
1 2 1
4
2 2 2 3
4
2 2 2 2
5
1000 1 1000 1 1000
Output
YES
NO
YES
YES
#include <bits/stdc++.h>
using namespace std;
int _;
int n;
void solve()
{
	cin>>n;
	vector<int>v(n+1);
	for(int i=1;i<=n;i++)cin>>v[i];
	bool f1=false,s1=false;
	bool f2=false,s2=false;
	for(int i=1;i<=n;i++)
	{
		if(i%2)
		{
			if(v[i]%2)f1=true;
			else s1=true;
		}
		else
		{
			if(v[i]%2)f2=true;
			else s2=true;
		}
	} 
	if(s1&&f1||f2&&s2)puts("NO");
	else puts("YES");
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

D. Colorful Stamp

A row of n cells is given, all initially white. Using a stamp, you can stamp any two neighboring cells such that one becomes red and the other becomes blue. A stamp can be rotated, i.e. it can be used in both ways: as BR and as RB.
During use, the stamp must completely fit on the given n cells (it cannot be partially outside the cells). The stamp can be applied multiple times to the same cell. Each usage of the stamp recolors both cells that are under the stamp.
For example, one possible sequence of stamps to make the picture BRBBW could be WWWWW→WWRB–––W→BR–––RBW→BRB–––BW. Here W, R, and B represent a white, red, or blue cell, respectively, and the cells that the stamp is used on are marked with an underline.
Given a final picture, is it possible to make it using the stamp zero or more times?
Input
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤105) — the length of the picture.
The second line of each test case contains a string s — the picture you need to make. It is guaranteed that the length of s is n and that s only consists of the characters W, R, and B, representing a white, red, or blue cell, respectively.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
Output t lines, each of which contains the answer to the corresponding test case. As an answer, output “YES” if it possible to make the picture using the stamp zero or more times, and “NO” otherwise.
You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Input
12
5
BRBBW
1
B
2
WB
2
RW
3
BRB
3
RBB
7
WWWWWWW
9
RBWBWRRBW
10
BRBRBRBRRB
12
BBBRWWRRRWBR
10
BRBRBRBRBW
5
RBWBW
Output
YES
NO
NO
NO
YES
YES
YES
NO
YES
NO
YES
NO

#include <bits/stdc++.h>
using namespace std;
int _;
int n;
void solve()
{
	cin>>n;
	string s;
	vector<string>v;
	bool f=false;
	for(int i=0;i<n;i++)
	{
		char c;
		cin>>c;
		if(c=='W'&&s.size())
		{
			v.push_back(s);
			s.clear();
			continue;
		}
		if(c!='W')s+=c;
	}
	if(s.size())v.push_back(s);
	for(auto x:v)
	{
		int cnt=1;
		for(int i=1;i<x.size();i++)
		{
			if(x[i]!=x[i-1])
				break;
			cnt++;
		}
		if(cnt==x.size())
		{
			f=true;
			break;
		}
	}
	if(f)puts("NO");
	else puts("YES");
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

E. 2-Letter Strings

Given n strings, each of length 2, consisting of lowercase Latin alphabet letters from ‘a’ to ‘k’, output the number of pairs of indices (i,j) such that i<j and the i-th string and the j-th string differ in exactly one position.
In other words, count the number of pairs (i,j) (i<j) such that the i-th string and the j-th string have exactly one position p (1≤p≤2) such that sip≠sjp.
The answer may not fit into 32-bit integer type, so you should use 64-bit integers like long long in C++ to avoid integer overflow.
Input
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (1≤n≤105) — the number of strings.
Then follows n lines, the i-th of which containing a single string si of length 2, consisting of lowercase Latin letters from ‘a’ to ‘k’.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, print a single integer — the number of pairs (i,j) (i<j) such that the i-th string and the j-th string have exactly one position p (1≤p≤2) such that sip≠sjp.
Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Input
4
6
ab
cb
db
aa
cc
ef
7
aa
bb
cc
ac
ca
bb
aa
4
kk
kk
ab
ab
5
jf
jf
jk
jk
jk

Output
5
6
0
6

#include <bits/stdc++.h>
#define int long long
using namespace std;
int _;
int n;
void solve()
{
	cin>>n;
	int sum=0;
	vector<vector<int>>cnt(12,vector<int>(12,0));
	for(int i=0;i<n;i++)
	{
		string s;
		cin>>s;
		for(int j=0;j<2;j++)
		{
			for(char c='a';c<='k';c++)
			{
				if(c==s[j])continue;
				string a=s;
				a[j]=c;
				sum+=cnt[a[0]-'a'][a[1]-'a'];
			}
		}
		cnt[s[0]-'a'][s[1]-'a']++;
	}
	cout<<sum<<endl;
}
signed main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

F. Eating Candies

There are n candies put from left to right on a table. The candies are numbered from left to right. The i-th candy has weight wi. Alice and Bob eat candies.
Alice can eat any number of candies from the left (she can’t skip candies, she eats them in a row).
Bob can eat any number of candies from the right (he can’t skip candies, he eats them in a row).
Of course, if Alice ate a candy, Bob can’t eat it (and vice versa).
They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total?
Input
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the number of candies on the table.
The second line of each test case contains n integers w1,w2,…,wn (1≤wi≤104) — the weights of candies from left to right.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, print a single integer — the maximum number of candies Alice and Bob can eat in total while satisfying the condition.

#include <bits/stdc++.h>
using namespace std;
int _;
int n;
void solve()
{
	cin>>n;
	vector<int>v(n+1);
	for(int i=0;i<n;i++)cin>>v[i];
	int l=0,r=n-1;
	int sum_l=v[0],sum_r=v[n-1];
	int res=0;
	while(l<r)
	{
		if(sum_l==sum_r)res=max(res,l+1+n-r);
		if(sum_l<=sum_r)
		{
			l++;
			sum_l+=v[l];
		}
		else 
		{
			r--;
			sum_r+=v[r];
		}
	}
	cout<<res<<endl;
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

G. Fall Down

There is a grid with n rows and m columns, and three types of cells:
An empty cell, denoted with ‘.’.
A stone, denoted with ‘'.
An obstacle, denoted with the lowercase Latin letter ‘o’.
All stones fall down until they meet the floor (the bottom row), an obstacle, or other stone which is already immovable. (In other words, all the stones just fall down as long as they can fall.)
Simulate the process. What does the resulting grid look like?
Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n,m≤50) — the number of rows and the number of columns in the grid, respectively.
Then n lines follow, each containing m characters. Each of these characters is either ‘.’, '
’, or ‘o’ — an empty cell, a stone, or an obstacle, respectively.
Output
For each test case, output a grid with n rows and m columns, showing the result of the process.
You don’t need to output a new line after each test, it is in the samples just for clarity.

Input
3
6 10
.*.*....*.
.*.......*
...o....o.
.*.*....*.
..........
.o......o*
2 9
...***ooo
.*o.*o.*o
5 5
*****
*....
*****
....*
*****

Output
..........
...*....*.
.*.o....o.
.*........
.*......**
.o.*....o*

....**ooo
.*o**o.*o

.....
*...*
*****
*****
*****

#include <bits/stdc++.h>
using namespace std;
int _;
int n,m;
void solve()
{
	cin>>n>>m;
	char g[n+10][m+10];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>g[i][j];
	for(int i=0;i<m;i++)
	{
		int last=n-1;
		for(int j=n-1;j>=0;j--)
		{
			if(g[j][i]=='o')last=j-1;
			else if(g[j][i]=='*')
			{
				swap(g[j][i],g[last][i]);
				last--;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cout<<g[i][j];
		}
		cout<<endl;
	}
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值