20240504训练题解

B. Memory

Singing a song with cups of drink, That days are short is what I think. They are like dews in morning early, Therefore, I feel suffered for time gone surely.

Little G used to be a participant in programming contests and he had attended n n n contests in total, and for the i i i-th contest, Little G got a i a_i ai happiness. However, Little G would also be influenced by past contests since memory also plays an important role to influence one’s mood. So we can use following formula to value Little G’s mood after the i i i-th contest:

M o o d ( i ) = ∑ j = 1 i 2 j − i × a j Mood(i) = \sum_{j=1}^{i}2^{j-i}\times a_j Mood(i)=j=1i2ji×aj

Now Little G is recalling the past and is curious about the moods after every contest, so he wants to tag the moods for every contest. Specifically, Little G will tag a positive sign (“+”) for the i i i-th contest if KaTeX parse error: Expected 'EOF', got '&' at position 9: Mood(i) &̲gt; 0, or tag a negative sign (“-”) if KaTeX parse error: Expected 'EOF', got '&' at position 9: Mood(i) &̲lt; 0, or tag a zero (“0”) if M o o d ( i ) = 0 Mood(i) = 0 Mood(i)=0. But Little G is busy working and working, so he is now asking you, the best programming contestant, to help him tag the moods.
Input

The first line contains one integers n n n ( 1 ≤ n ≤ 1 0 5 1\le n\le 10^5 1n105), denoting the number of contests Little G had attended.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109), denoting the happiness values after every contest.
Output

Output one line containing a string which is of length n n n and only contains “+”, “-” or “0”, denoting the mood tags after every contest.

解题思路

特别处理浮点数

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
double a[N],b[N];
int main()
{
	int n,x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		int t=(int)a[i-1];
		double tt=a[i-1]-t;
		if(tt<0)	a[i-1]=t/2-0.5;
		else if(tt>0)	a[i-1]=t/2+0.5;
		else a[i-1]=(double)t/2;
		a[i]=a[i-1]+x;
		if(a[i]==0)	cout<<"0";
		else if(a[i]>0)	cout<<"+";
		else if(a[i]<0)	cout<<"-";
	}
	return 0;
}

L - Palm Island

Toph is playing a card game. She has n n n cards and each card has a unique number of 1 , 2 ⋯ n 1,2\cdots n 1,2n. In this game, Toph can operate the deck of the cards. We may wish to assume that the cards from the top to the bottom of the deck are p 1 , p 2 , ⋯ p n p_1,p_2,\cdots p_n p1,p2,pn (a permutation), then each operation must be one of the following two cases:

  1. Place the top card at the bottom of the deck, that is, change the order of the deck into p 2 , p 3 ⋯ p n , p 1 p_2,p_3\cdots p_n,p_1 p2,p3pn,p1.
  2. Place the second card from the top at the bottom of the deck, that is, change the order of the deck into p 1 , p 3 ⋯ p n , p 2 p_1,p_3\cdots p_n,p_2 p1,p3pn,p2

Now, you know that the initial order(from top to bottom) of Toph’s deck is a 1 , a 2 , ⋯ a n a_1,a_2,\cdots a_n a1,a2,an, and Toph wants to change the order of the deck into b 1 , b 2 , ⋯ b n b_1,b_2,\cdots b_n b1,b2,bn after some operations. Please construct the operation sequence to help Toph implement the change.

Toph has no patience. So the number of operations should not exceed n 2 n^2 n2.

Input

The first line contains an integer T T T , indicating the number of test cases.

For each test case:

  • The first line contains an integer, n ( 3 ≤ n ≤ 1000 ) n(3\le n\le 1000) n(3n1000), indicating the number of Toph’s cards.
  • The second line contains n n n integers a 1 , a 2 , ⋯ a n a_1,a_2,\cdots a_n a1,a2,an, a permutation indicating the order of the deck initially.
  • The third line contains n n n integers b 1 , b 2 , ⋯ b n b_1,b_2,\cdots b_n b1,b2,bn, a permutation indicating the order of the deck want to make.

It is guaranteed that the sum of n n n in T T T test cases is not exceed 1000 1000 1000.

Output

For each test case:

  • Output a line, which contains a string s 1 s 2 … s k ( s i ∈ { 1 , 2 } ,   1 ≤ i ≤ k ) s_1s_2\dots s_k(s_i\in \{1,2\},\ 1\le i\le k) s1s2sk(si{1,2}, 1ik) as your operation sequence. The length of the string should not exceed n 2 n^2 n2, or you will get “Wrong Answer”.
  • If there are multiple solutions, output any of them.

解题思路

冒泡排序的理解

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
long long a[N],b[N],c[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
			c[b[i]]=i;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n-1;j++)
			{
				if(c[a[j]]>c[a[j+1]])
				{
					cout<<"2";
					swap(a[j],a[j+1]);
				}
				else	cout<<"1";
			}
			cout<<"1";
		}
		cout<<endl;
	}
	return 0;
}

M - Painter

Little G is a painter and is painting on a 2D plane. Each integral point has a color character and the initial color characters for all integral points are “.”(ASCII = 46). Now Little G is planning to do some operations one by one, where each operation is in one of the following three types:

  1. “Circle x   y   r   c o l x\,y\,r\,col xyrcol”, which means to draw a circle. Formally, change the color characters to c o l col col for these points ( u , v ) (u,v) (u,v) that ( u − x ) 2 + ( v − y ) 2 ≤ r 2 (u-x)^2+(v-y)^2\le r^2 (ux)2+(vy)2r2.
  2. “Rectangle x 1   y 1   x 2   y 2   c o l x_1\,y_1\,x_2\,y_2\,col x1y1x2y2col”, which means to draw a rectangle. Formally, change the color characters to c o l col col for these points ( u , v ) (u,v) (u,v) that x 1 ≤ u ≤ x 2 , y 1 ≤ v ≤ y 2 x_1 \le u \le x_2, y_1 \le v \le y_2 x1ux2,y1vy2.
  3. “Render x 1   y 1   x 2   y 2 x_1\,y_1\,x_2\,y_2 x1y1x2y2”, which means to render the image of given region. Formally, print the color characters for these points ( u , v ) (u,v) (u,v) that x 1 ≤ u ≤ x 2 , y 1 ≤ v ≤ y 2 x_1 \le u \le x_2, y_1 \le v \le y_2 x1ux2,y1vy2.

But now, Little G is busy replying clarifications, so could you help him and be the painter?

Little G is a painter and is painting on a 2D plane. Each integral point has a color character and the initial color characters for all integral points are “.”(ASCII = 46). Now Little G is planning to do some operations one by one, where each operation is in one of the following three types:

  1. “Circle x   y   r   c o l x\,y\,r\,col xyrcol”, which means to draw a circle. Formally, change the color characters to c o l col col for these points ( u , v ) (u,v) (u,v) that ( u − x ) 2 + ( v − y ) 2 ≤ r 2 (u-x)^2+(v-y)^2\le r^2 (ux)2+(vy)2r2.
  2. “Rectangle x 1   y 1   x 2   y 2   c o l x_1\,y_1\,x_2\,y_2\,col x1y1x2y2col”, which means to draw a rectangle. Formally, change the color characters to c o l col col for these points ( u , v ) (u,v) (u,v) that x 1 ≤ u ≤ x 2 , y 1 ≤ v ≤ y 2 x_1 \le u \le x_2, y_1 \le v \le y_2 x1ux2,y1vy2.
  3. “Render x 1   y 1   x 2   y 2 x_1\,y_1\,x_2\,y_2 x1y1x2y2”, which means to render the image of given region. Formally, print the color characters for these points ( u , v ) (u,v) (u,v) that x 1 ≤ u ≤ x 2 , y 1 ≤ v ≤ y 2 x_1 \le u \le x_2, y_1 \le v \le y_2 x1ux2,y1vy2.

But now, Little G is busy replying clarifications, so could you help him and be the painter?

Output

For each rendering operation “Render x 1   y 1   x 2   y 2 x_1\,y_1\,x_2\,y_2 x1y1x2y2”, print y 2 − y 1 + 1 y_2 - y_1 + 1 y2y1+1 lines each containing one string of length x 2 − x 1 + 1 x_2 - x_1 + 1 x2x1+1, denoting the region image(from row y 2 y_2 y2 to row y 1 y_1 y1).

解题思路

先保存更改记录,最后输出时判断

AC代码

# include <bits/stdc++.h>
using namespace std;
struct rect
{
	int x,y;
	int x1,y1;
	int r;
	int flag;
	char l;
}d[2100];
int check(int u,int v,int x,int y,int r)
{
	if((x-u)*(x-u)+(y-v)*(y-v)<=r*r)	return 1;
	else	return 0;
}
int check1(int u,int v,int x,int x1,int y,int y1)
{
	if(u>=x&&u<=x1&&v>=y&&v<=y1)    return 1;
	else    return 0;
}
signed  main()
{
	int n;
	cin>>n;
	int m=0;
	for(int i=1; i<=n; i++)
	{
		string s;
		cin>>s;
		if(s=="Circle")
		{
			cin>>d[m].x>>d[m].y>>d[m].r;
			d[m].flag=1;
			cin>>d[m].l;
			m++;
		}
		else if(s=="Rectangle")
		{
			cin>>d[m].x>>d[m].y>>d[m].x1>>d[m].y1;
			d[m].flag=2;
			cin>>d[m].l;
			m++;
		}
		else
		{
			int x,y,x1,y1;
			cin>>x>>y>>x1>>y1;
			for(int i=y1; i>=y; i--)
			{
				for(int j=x; j<=x1; j++)
				{
					char o='.';
					for(int z=m-1; z>=0; z--)
					{
						if(d[z].flag==1)
						{
							if(check(j,i,d[z].x,d[z].y,d[z].r))
							{
								o=d[z].l;
								break;
							}

						}
						else if(d[z].flag==2)
						{
							if(check1(j,i,d[z].x,d[z].x1,d[z].y,d[z].y1))
							{
								o=d[z].l;
								break;
							}
						}
					}
					cout<<o;
				}
				cout<<endl;
			}
		}
	}
    return 0;
}
  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值