Codeforces Round #615 (Div. 3) 题解

2023-11-09 23:40
文章标签 codeforces round div 题解 615

本文主要是介绍Codeforces Round #615 (Div. 3) 题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本次打的极差,我哭了!!!


A - Collecting Coins

题目

A. Collecting Coins

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp has three sisters: Alice, Barbara, and Cerene. They’re collecting coins. Currently, Alice has aa coins, Barbara has bb coins and Cerene has cc coins. Recently Polycarp has returned from the trip around the world and brought nn coins.

He wants to distribute all these nn coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives AA coins to Alice, BB coins to Barbara and CC coins to Cerene (A+B+C=nA+B+C=n), then a+A=b+B=c+Ca+A=b+B=c+C.

Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.

Your task is to find out if it is possible to distribute all nn coins between sisters in a way described above.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The next tt lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a,b,ca,b,c and nn (1≤a,b,c,n≤1081≤a,b,c,n≤108) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.

Output

For each test case, print “YES” if Polycarp can distribute all nn coins between his sisters and “NO” otherwise.

Example

input

Copy

5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3

output

Copy

YES
YES
NO
NO
YES

题意

给你四个数a,b,c,d,n.问你是否能将n拆成三个数A,B,C,使得A+a=B+b=C+c。

思路

先计算三个数的差值的绝对值abs,如果abs大于n则肯定不行,如果小于n,还需判断(n-abs)%3是否为0,不为0则不行。

代码

import sys
import queue
sys.setrecursionlimit(10 ** 9)
IA = lambda: map(int, input().split())
IAL = lambda: list(map(int, input().split()))
IM = lambda N: [IA() for _ in range(N)]
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
N = 105
T=int(input())
for i in range(0,T):a,b,c,n=IA()#print(n-(2*max(a,b,c)-(a+b+c-max(a,b,c))))if (2*max(a,b,c)-(a+b+c-max(a,b,c)))>n:print("NO")elif (n-(2*max(a,b,c)-(a+b+c-max(a,b,c))))%3==0:print("YES")else:print("NO")

B. Collecting Packages

题目

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is at the point (xi,yi)(xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn’t contain a package.

The robot is semi-broken and only can move up (‘U’) and right (‘R’). In other words, in one move the robot can go from the point (x,y)(x,y) to the point (x+1,yx+1,y) or to the point (x,y+1)(x,y+1).

As we say above, the robot wants to collect all nn packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.

The string ss of length nn is lexicographically less than the string tt of length nn if there is some index 1≤j≤n1≤j≤n that for all ii from 11 to j−1j−1 si=tisi=ti and sj<tjsj<tj. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.

Input

The first line of the input contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then test cases follow.

The first line of a test case contains one integer nn (1≤n≤10001≤n≤1000) — the number of packages.

The next nn lines contain descriptions of packages. The ii-th package is given as two integers xixi and yiyi (0≤xi,yi≤10000≤xi,yi≤1000) — the xx-coordinate of the package and the yy-coordinate of the package.

It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn’t contain a package.

The sum of all values nn over test cases in the test doesn’t exceed 10001000.

Output

Print the answer for each test case.

If it is impossible to collect all nn packages in some order starting from (0,00,0), print “NO” on the first line.

Otherwise, print “YES” in the first line. Then print the shortest path — a string consisting of characters ‘R’ and ‘U’. Among all such paths choose the lexicographically smallest path.

Note that in this problem “YES” and “NO” can be only uppercase words, i.e. “Yes”, “no” and “YeS” are not acceptable.

Example

input

Copy

3
5
1 3
1 2
3 3
5 5
4 3
2
1 0
0 1
1
4 3

output

Copy

YES
RUUURRRRUU
NO
YES
RRRRUUU

Note

For the first test case in the example the optimal path RUUURRRRUU is shown below:

##

题意

给出直角坐标系中的一些坐标,你只能向上(‘U’)或向右(‘R’)走,问你能否走完这些坐标,如果能请输出字典序最小的行进路线

思路

先x后y坐标排序,如果有一个点的纵坐标在上一个点的纵坐标下方,则无法走。输出顺序只需要从上一个点先走R再走U走到下一个点即可

代码

import sys
sys.setrecursionlimit(10**9)
IA =lambda: map(int,input().split())T=int(input())
for t in range(0,T):n=int(input())a=[]for i in range(0,n):x,y=IA()a.append([x,y])a.sort()sx=0sy=0ans=""flag=1for item in a:if item[0]>=sx and item[1]>=sy:ans=ans+(item[0]-sx)*'R'+(item[1]-sy)*'U'sx=item[0]sy=item[1]else:print("NO")flag=-1breakif flag==1:print("YES")print(ans)

C - Product of Three Numbers

题目

C. Product of Three Numbers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c and a⋅b⋅c=na⋅b⋅c=n or say that it is impossible to do it.

If there are several answers, you can print any.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The next nn lines describe test cases. The ii-th test case is given on a new line as one integer nn (2≤n≤1092≤n≤109).

Output

For each test case, print the answer on it. Print “NO” if it is impossible to represent nn as a⋅b⋅ca⋅b⋅c for some distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c.

Otherwise, print “YES” and any possible such representation.

Example

input

Copy

5
64
32
97
2
12345

output

Copy

YES
2 4 8 
NO
NO
NO
YES
3 5 823 

题意

给你一个数n,问你是否能将n分解成三个不同的数相乘,可以的话则输出任一解

思路

只需要将遍历从2到img,如果i为n的因子,就将i记录,并将n除以i。这样的操作执行三次之后,就可以跳出循环。

判断条件直接暴力就行,因为就三个。

代码

import sys
sys.setrecursionlimit(10**9)
IA =lambda: map(int,input().split())
ans=[0,0,0,0]
def solve(n):num=int(0)i=int(2)tmp=1m=nwhile i*i<=n:if n%i==0:n=n//itmp*=iif tmp not in ans:num+=1ans[num]=tmptmp=1if num>=3:ans[num]*=nreturn 1i+=1if n>1:if tmp*n not in ans:num+=1ans[num]=n*tmpif num>=3: return 1else:return -1T=int(input())
for t in range(0,T):ans=[0,0,0,0]n=int(input())if solve(n)==1:print("YES")for i in range(1,4):print(ans[i],end=" ")print()else:print("NO")

D. MEX maximizing

题目

D. MEX maximizing

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:

  • for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are presented in the array and 33 is the minimum non-negative integer not presented in the array;
  • for the array [1,2,3,4][1,2,3,4] MEX equals to 00 because 00 is the minimum non-negative integer not presented in the array;
  • for the array [0,1,4,3][0,1,4,3] MEX equals to 22 because 22 is the minimum non-negative integer not presented in the array.

You are given an empty array a=[]a=[] (in other words, a zero-length array). You are also given a positive integer xx.

You are also given qq queries. The jj-th query consists of one integer yjyj and means that you have to append one element yjyj to the array. The array length increases by 11 after a query.

In one move, you can choose any index ii and set ai:=ai+xai:=ai+x or ai:=ai−xai:=ai−x (i.e. increase or decrease any element of the array by xx). The only restriction is that aiai cannot become negative. Since initially the array is empty, you can perform moves only after the first query.

You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).

You have to find the answer after each of qq queries (i.e. the jj-th answer corresponds to the array of length jj).

Operations are discarded before each query. I.e. the array aa after the jj-th query equals to [y1,y2,…,yj][y1,y2,…,yj].

Input

The first line of the input contains two integers q,xq,x (1≤q,x≤4⋅1051≤q,x≤4⋅105) — the number of queries and the value of xx.

The next qq lines describe queries. The jj-th query consists of one integer yjyj (0≤yj≤1090≤yj≤109) and means that you have to append one element yjyj to the array.

Output

Print the answer to the initial problem after each query — for the query jj print the maximum value of MEX after first jj queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.

Examples

input

Copy

7 3
0
1
2
2
0
0
10

output

Copy

1
2
3
3
4
4
7

input

Copy

4 3
1
2
1
2

output

Copy

0
0
0
0

Note

In the first example:

  • After the first query, the array is a=[0]a=[0]: you don’t need to perform any operations, maximum possible MEX is 11.

  • After the second query, the array is a=[0,1]a=[0,1]: you don’t need to perform any operations, maximum possible MEX is 22.

  • After the third query, the array is a=[0,1,2]a=[0,1,2]: you don’t need to perform any operations, maximum possible MEX is 33.

  • After the fourth query, the array is a=[0,1,2,2]a=[0,1,2,2]: you don’t need to perform any operations, maximum possible MEX is 33 (you can’t make it greater with operations).

  • After the fifth query, the array is a=[0,1,2,2,0]a=[0,1,2,2,0]: you can perform a[4]:=a[4]+3=3a[4]:=a[4]+3=3. The array changes to be a=[0,1,2,2,3]a=[0,1,2,2,3]. Now MEX is maximum possible and equals to 44.

  • After the sixth query, the array is a=[0,1,2,2,0,0]a=[0,1,2,2,0,0]: you can perform a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3. The array changes to be a=[0,1,2,2,3,0]a=[0,1,2,2,3,0]. Now MEX is maximum possible and equals to 44.

  • After the seventh query, the array is

    a=[0,1,2,2,0,0,10]a=[0,1,2,2,0,0,10]

    . You can perform the following operations:

    • a[3]:=a[3]+3=2+3=5a[3]:=a[3]+3=2+3=5,
    • a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3,
    • a[5]:=a[5]+3=0+3=3a[5]:=a[5]+3=0+3=3,
    • a[5]:=a[5]+3=3+3=6a[5]:=a[5]+3=3+3=6,
    • a[6]:=a[6]−3=10−3=7a[6]:=a[6]−3=10−3=7,
    • a[6]:=a[6]−3=7−3=4a[6]:=a[6]−3=7−3=4.

    The resulting array will be

    a=[0,1,2,5,3,6,4]a=[0,1,2,5,3,6,4]

    . Now MEX is maximum possible and equals to 7

    题意

    • 输入q,x,然后输入q个数(q个询问),每次放进一个数(开始为空数组)
    • 同时每次可以对数组内的某个数或者某几个数做任意操作数的加x或者减x (x为input第二个数),
    • 输出:处理完后询问,在该数组情况下的最小整数(就是数组里的数都被排除的情况下)从0开始找,第一个不在数组里的整数就是了,然后要求操作加x或者减x的任意操作后使得那个询问结果最大化。

    思路

    构建mod x的剩余系,每次找到这个剩余系里面最大的加上x即可,从0开始判断即可,直接看代码,很短。

    代码

    import sys
    sys.setrecursionlimit(10**9)
    from collections import defaultdictIA =lambda: map(int,input().split())
    ans=[0,0,0,0]T,x=IA()
    se=defaultdict(int)
    be=0for t in range(0,T):a=int(input())temp=0se[a%x]+=1while se[be%x] > 0:se[be%x]-=1be+=1print(be)
    

这篇关于Codeforces Round #615 (Div. 3) 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/379143

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述