寒假养成计划——Day2

A题

题目链接

You have an n×n chessboard and k rooks. Rows of this chessboard are numbered by integers from 1 to n from top to bottom and columns of this chessboard are numbered by integers from 1 to n from left to right. The cell (x,y) is the cell on the intersection of row x and collumn y for 1≤x≤n and 1≤y≤n.

The arrangement of rooks on this board is called good, if no rook is beaten by another rook.

A rook beats all the rooks that shares the same row or collumn with it.

The good arrangement of rooks on this board is called not stable, if it is possible to move one rook to the adjacent cell so arrangement becomes not good. Otherwise, the good arrangement is stable. Here, adjacent cells are the cells that share a side.

Such arrangement of 3 rooks on the 4×4 chessboard is good, but it is not stable: the rook from (1,1) can be moved to the adjacent cell (2,1) and rooks on cells (2,1) and (2,4) will beat each other.

Please, find any stable arrangement of k rooks on the n×n chessboard or report that there is no such arrangement.

Input

The first line contains a single integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains two integers n, k (1≤k≤n≤40) — the size of the chessboard and the number of rooks.

Output

If there is a stable arrangement of k rooks on the n×n chessboard, output n lines of symbols . and R. The j-th symbol of the i-th line should be equals R if and only if there is a rook on the cell (i,j) in your arrangement.

If there are multiple solutions, you may output any of them.

If there is no stable arrangement, output −1.

Example

input

5
3 2
3 3
1 1
5 2
40 33

output

..R
...
R..
-1
R
.....
R....
.....
....R
.....
-1

Note

In the first test case, you should find stable arrangement of 2 rooks on the 3×3 chessboard. Placing them in cells (3,1) and (1,3) gives stable arrangement.

In the second test case it can be shown that it is impossbile to place 3 rooks on the 3×3 chessboard to get stable arrangement.


题解:

        这个题的大意还是比较清楚的,就是说给定一个n×n的棋盘和k个棋子,说任意移动一个棋子都能使所有棋子不在同一行或者同一列就算是稳定,输出这个稳定格式,如果没有则输出-1。满足这个稳定是比较容易的,让每个棋子之间隔一行一列就行,输出的时候只输出对角线元素即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		if ((n+1)/2<k)
			puts("-1");
		else
		{
			int cnt=0;
			for (int i=0;i<n;i++)
			{
				for (int j=0;j<n;j++)
				{
					if (i%2==0&&cnt<k&&i==j)
					{
						printf("R");
						cnt++;
					}	
					else
						printf(".");
				}
				printf("\n");
			}
		}
	}
	return 0;
}

B题

题目链接

The integers shop sells n segments. The i-th of them contains all integers from li to ri and costs ci coins.

Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.

After shopping, Vasya will get some more integers as a gift. He will get integer x as a gift if and only if all of the following conditions are satisfied:

  • Vasya hasn't bought x.
  • Vasya has bought integer ll that is less than x.
  • Vasya has bought integer rr that is greater than x.

Vasya can get integer x as a gift only once so he won't have the same integers after receiving a gift.

For example, if Vasya buys segment [2,4] for 20 coins and segment [7,8] for 22 coins, he spends 42 coins and receives integers 2,3,4,7,8 from these segments. He also gets integers 5 and 6 as a gift.

Due to the technical issues only the first ss segments (that is, segments [l1,r1],[l2,r2],…,[ls,rs]) will be available tomorrow in the shop.

Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.

For each s from 1 to n, find how many coins will Vasya spend if only the first ss segments will be available.

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains the single integer n (1≤n≤105) — the number of segments in the shop.

Each of next n lines contains three integers li, ri, ci (1≤li≤ri≤10^{9},1≤ci≤10^{9}) — the ends of the i-th segments and its cost.

It is guaranteed that the total sum of n over all test cases doesn't exceed 2⋅10^{5}.

Output

For each test case output n integers: the s-th (1≤s≤n) of them should be the number of coins Vasia will spend in the shop if only the first s segments will be available.

Example

input

3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1

output

20
42
42
42
4
13
11
256
271
271

Note

In the first test case if s=1 then Vasya can buy only the segment [2,4] for 20 coins and get 3 integers.

The way to get 7 integers for 42 coins in case s=2 is described in the statement.

In the second test case note, that there can be the same segments in the shop.


题解:

        这道题看起来比较长,但实际上挺简单的。这道题大意就是说给定一些区间,可以获得区间内和最大区间范围的所有数,问获得这些数的最少价格是多少,要求输出每一步的结果。做这道题的时候只需要维护当前的最大区间和极限区间的价格就行,遇到相同的区间替换一下价格,如果区间刚好是最大区间,则比较这个区间的价格和当前区间的价格即可。

(看着这段话比较别扭,直接上代码解释)

AC代码:

#include <bits/stdc++.h>
#define lll long long    //注意数据范围
using namespace std;

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		scanf("%d",&n);
		lll ll=1e9+5,rr=0,cl=0,cr=0,clr=0;    //ll是指最大左边界,rr是指最大右边界,cl记录左侧区间的价格,cr记录右侧区间的价格,clr记录当且仅当输入的左右边界与当前边界重合时的价格。
		for (int i=1;i<=n;i++)
		{
			lll l,r,c;
			scanf("%lld%lld%lld",&l,&r,&c);
			if (l<ll)    //更新左边界
			{
				ll=l;
				cl=c;
				clr=1e18+5;
			}
			if (r>rr)    //更新右边界
			{
				rr=r;
				cr=c;
				clr=1e18+5;
			}
			if (l==ll&&r==rr)
				clr=min(clr,c);    //每次只取最小值,下面同理
			if (l==ll)
				cl=min(cl,c);    
			if (r==rr)
				cr=min(cr,c);
			printf("%lld\n",min(cl+cr,clr));
		}
	}
	return 0;
}

ps:本人实力较菜,只能做出这些简单题,后面的题如果有哪位大神可以解决可以私信我,如果这里面有讲的不清楚或者有错误的,望及时指出!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值