2021 ICPC Asia Taipei Regional(A M B C)

A

Problem Description

To celebrate the 3rd anniversary of the Ice Cream Professional Company (ICPC), the company has an anniversary sale activity. The activity’s name is “Buy units and Get units free”. The contents of the activity is as below. First, you will be asking to draw a lottery ticket of units and a lottery ticket of units, and then you can enjoy the discount of buy units and get units free. It means for every units of ice cream you bought, you will get units free ice cream. The price of every unit of the ice cream is 3 dollars, and you want to buy some units of ice cream. Please write a program to calculate the minimum price of your order.

Input Format

The first line contains an integer , which represents the number of test cases. Each test case contains a single line with 3 integers , , , which represents the lottery tickets of , , and you want to obtain at least ice cream. Any two consecutive integers are separated by exactly one space.

Output Format

For each test case outputs an integer, which represents the minimum price of your order

Technical Specification

∙ 1 ≤ ≤ 15 ∙ 1 ≤ , ≤ 1, 000 ∙ 1 ≤ ≤ 15, 000

Input

2
4 7 22
4 8 22

Output

24
24

题意:冰淇淋一个3元,买X个送Y的,问凑够N个需要的最小花费。

注意:送的Y个不能算进X中。

#include 
void solve()
{
	int x,y,n;
	int cnt=0,free=0,price=0;//cnt记录购买的数量,free记录免费赠送的个数
	scanf("%d%d%d",&x,&y,&n);
	while(cnt+free

M

Problem Description

Nobi is walking in a foggy forest. He tries to locate himself using a map. The forest is divided using vertical and horizontal lines into × regions so the map is represented by an by matrix, with rows indexed from 0 to −1 and columns indexed from 0 to −1. Each entry of the matrix corresponds to a region of the forest, where the entry with row index and column index is denoted by (, ). Each entry is either 1 or 0. A 1 indicates that there are very tall trees in the region, and a 0 indicates that the region contains only bushes. It is known that the outside of the forest is surrounded by bushes, which means that regions not specified in the map are treated as 0s.

Nobi wants to know which region he might be in. Because the fog is heavy, Nobi can only observe the region he stays in, the region in the front, and the region on the right side. However, he does not know which direction he faces to. Please help Nobi to find the possible regions he is in, assuming that there are only four directions, “N”, “E”, “S”, and “W”. For example, below is a 2 by 3 matrix

2021 ICPC Asia Taipei Regional(A M B C)_第1张图片

 Nobi observes that there are tall trees in the regions he stays in and in front of him (corresponding to matrix entries of 1), and the region on the right side of him has only bushes (corresponding to a matrix entry of 0). It is possible that Nobi is in the region corresponding to entry (0, 0) because (0, 0) = 1, and when he faces to the direction “E”, the entry in front of him, (0, 1), is 1; the entry on the right side of him, (1, 0), is 0. The other possible region is (0, 1) because (0, 1) = 1 and when he faces to “W”, the entries in the front is (0, 0) = 1; the entry on the right side is outside the map, which is treated as 0.

Input Format

The first line of a test case contains two integers and , which are the numbers of rows and columns of the matrix, respectively. The rows of the matrix are indexed from 0 to − 1, and columns are indexed from 0 to −1. The following lines are the content of the map. Each ofthe lines contains integers. The last line contains three integers , , and . The number , , and indicate what Nobi observes in the regions he stays in, in front of him, and on the right side of him, respectively.

Output Format

Each line of the output contains two integers, which are the possible row and column indices of the region Nobi is in, respectively. It is guaranteed that Nobi is in the forest. If there are more than one possible regions, output the regions with smaller row index first; for regions with the same row index, output the one with smaller column index first.

Technical Specification

1 ≤ , ≤ 100 ,Each entry is either 0 or 1,, , ∈ {0, 1}

Input

2 3
1 1 0
0 0 1
1 1 0

Output

0 0
0 1

题意:n行m列,再给出三个数值s,f,r分别表示当前位置,前方,右方的权值,越界的位置都是0,问有多少个位置(i,j)可能满足(4种朝向),如果多个位置满足,行数小的先输出,如果相同就列数小的先输出。

解析:暴力枚举每个位置,判断四个方向中是否能满足三者,是的话存入set中,直接就帮我们解决去重加排序

注意点:因为题目下标从0开始,我们判断方向会越界,我们可以视为从1开始,最后输出时候-1即可。

#include 
#include 
#include 
using namespace std;
const int N=1005;
int g[N][N];//保存地图
set >st;
int main()
{
	int n,m,x,y,z;//xyz分别表示所在地,前方,右方的权值
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) scanf("%d",&g[i][j]);
	scanf("%d%d%d",&x,&y,&z);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(g[i][j]!=x) continue;
			//直接枚举4个朝向,依此判断即可,set自动判重排序
			if(g[i][j-1]==y&&g[i-1][j]==z) st.insert({i,j});
			if(g[i-1][j]==y&&g[i][j+1]==z) st.insert({i,j});
			if(g[i][j+1]==y&&g[i+1][j]==z) st.insert({i,j});
			if(g[i+1][j]==y&&g[i][j-1]==z) st.insert({i,j});
		}
	for(auto f:st) printf("%d %d\n",f.first-1,f.second-1);		
 	return 0;
}

B

Problem Description

Consider two strings of the same length, for example, 1 = "happycoding" and 2 = "onedocument". Comparison 1 and 2 character by character, they get 2 matches, such as happycoding onedocument  00000100010 Suppose that ′ 2 is obtained by reversing the substring from the 4ℎ character to the 8ℎ character of 2, i.e., ′ 2 = "onemucodent". Then, 1 and ′ 2 will get 4 matches, such as happycoding onemucodent 00000111010

Given a pair of two strings of the same length, say 1 and 2, your task is to reverse a substring of 2 so that the resulting number of characters matched by position can be maximized.

Input Format

The first line contains an integer , which represents the number of test cases. Each test case below contains three lines. For each test case, the first line is an integer ≤ 1, 000, which represents the length of the next two lines of strings, and the next two lines are two lowercase strings of length , called 1 and 2 respectively.

Output Format

Each test case outputs four integers 1,2, 1,2*, , , separated by a space. The first integer 1,2 is the number of characters that 1 and 2 match by position. The second integer 1,2* is the maximum possible number of matches by matching 1 and the substring reversed version of 2. ≤ are the indexes of 2, starting from 1. By reversing the substring indexed from to in 2, the maximum number of matches will be obtained. If there are multiple solutions, output the one with the smallest value of − . If multiple solutions are still obtained, the one with the minimum value of is output

Technical Specification

∙ 1 ≤ ≤ 50. ∙ 1 ≤ ≤ 1, 000 ∙ All string characters are lowercase letters.

Input

5
11
happycoding
onedocument
8
abbbcccd
xcbcbybd
3
cat
dog
4
abcd
xyza
3
dog
pop

Ous

2 4 4 8
2 5 2 7
0 0 1 1
0 1 1 4
1 1 1 1

题意: 给两个字符串s1,s2,反转s2中的某个区间,使得两个字符串对应位置匹配字母个数最大,如果有多个区间满足,选择区间差值最小的那个,最后输出初始匹配个数反转后最大匹配个数反转区间左右端点

解析:因为数据比较小,我们可以直接暴力枚举反转的左右端点

#include 
const int N=1005;
char a[N],b[N];
int sum(int l,int r)//返回[L,R]区间内匹配字符的个数
{
	int cnt=0;
	if(l>r) return 0;
	for(int i=l;i<=r;i++) if(a[i]==b[i]) cnt++;
	return cnt;	
}
void solve()
{
	int n,ansl=1,ansr=1,maxn=0;//ansl,ansr需要初始化1
	scanf("%d%s%s",&n,a+1,b+1);
	maxn=sum(1,n);
	for(int i=1;i<=n;i++)//枚举左端点
	{
		for(int j=i;j<=n;j++)//枚举右端点
		{
			int l=i,r=j;
			int cntl=sum(1,l-1),cntr=sum(r+1,n);//记录没反转区间的匹配个数
			int cntlr=0;//记录反转区间匹配个数
			for(int x=l,y=r;x<=r;x++,y--) if(a[x]==b[y]) cntlr++;
			if(cntl+cntlr+cntr>maxn)
			{
				ansl=l,ansr=r;//更新答案区间
				maxn=cntl+cntlr+cntr;//更新最大长度
			}else if(cntl+cntlr+cntr==maxn&&r-l

C

Problem Description

ICPC (International-Competition Partner Community) is an emerging community. The people who live there are very young, competitive and willing to serve the community. The residents of ICPC live on a straight-line street. One can use integers to locate or address specific points on the line. The point addresses in the line are numbered from 0 to −1, where 1 ≤ ≤ 1, 000, 000. Many young people have moved to ICPC one after another. Everyone who newly moves into the community must provide a one-time community service at certain time. According to the place of residence, each newcomer can choose a set of interesting points covered by an integer range, say [, ](0 ≤ ≤ < ) to provide such a one-time service. When the community center requests a service on a set of points covered by an integer range, say [, ](0 ≤ ≤ < ), the newest newcomer whose interesting-point set overlays with that set, i.e., has a non-empty intersection, should provide the service. Once the newcomer has provided the service, there is no need to provide service any more.

In this problem, you are given the number of points, say , on ICPC street, and a sequence of events that occur consecutively over time. There are two types of events, called newcomer events and service-request events, defined below:

Newcomer event

1

where is a string (without space) representing the newcomer’s name, and are integers such that 0 ≤ ≤ < . Any points between and (included) are interesting points of this newcomer to provide the one-time service.

Service-request events

2

where and are integers such that 0 ≤ ≤ < . This event represents that ICPC community center issues a service request in the area which includes all points between and (included).

Write a program to process the events described above. When receiving a service-request event, output a single line which reports the name of newest newcomer who provides the service. If it is unavailable (because residents who meet the preference condition all have provided services before, or no one interest to provide services in the designated area), output a line of string "> <" (without quotation mark).

Figure 1 gives an schematic explanation for the sample input/output. On different time ticks, display the corresponding events that occurred. The shallow-gray bar with name attached shows the newcomer’s interesting area, and the dark-gray bar shows the service request area. 

Input Format

The first line includes two integer (1 ≤ ≤ 1, 000, 000) and 1 ≤ ≤ 200, 000, where denotes the number of points that the community line has, and denotes the number of events to be processed in sequel. The next lines describes events that occur in sequence over time. The format of the event description is specified in the problem statement.

Output Format

For each service event, output a line describing the name of the resident who provided the corresponding service. If not available, output a line of string "> <" (without quotation mark). Please note that different residents may have the same name, but this in fact has nothing to do with problem solving.

Technical Specification

∙ 1 ≤ ≤ 1, 000, 000 ∙ 1 ≤ ≤ 200, 000 ∙ 0 ≤ ≤ < ∙ 0 ≤ ≤ <

∙ All newcomers’ names are composed of alphabets without any spaces, and each length does not exceed 16.

2021 ICPC Asia Taipei Regional(A M B C)_第2张图片

 

Input

12 10
1 Jessica 5 10
1 Sara 4 7
1 Peter 7 9
2 3 5
2 3 7
1 Olivia 4 6
2 1 3
2 8 8
2 8 9
2 3 11

Output

Sara
Peter
>_<
Jessica
>_<
Olivia

题意:在一个0~N-1长度的数轴上,维护两种操作,操作1是加入一个name在[L,R]区间,操作2就是查询与[L,R]区间有交集的最新加入的name,然后删除这个区间名字,如果不存在就输出>_<。

解析:我们用线段树来维护,每个节点存两个vector,Path和had,表示子树中包含的名字和该区间自己包含的名字,然后每次添加名字递归每个节点在Path中添加,如果区间被完全包含再在had中添加。查询操作每次与当前节点的had最后一个合法元素取max,如果区间完全包含,那么就直接跟Path中最后一个合法元素取max,如此就得到了答案。
注意:我们存vector时候存的是name的编号,这样最后找到了ans,输出name[ans]即可,还需注意下标是从0开始的,我们需要将L,R都+1,这样方便操作。

#include 
#include 
#include 
#include 
using namespace std;
const int N=1e6+5;
string name[N];//保存名字
bool st[N];//判读该名字是否可用
struct s
{
	int l,r;
	vector path,had;
	//记录该子树中包含的名字和该区间自己包含的名字
}tr[N*4];
void build(int u,int l,int r)
{
	if(l==r) tr[u]={l,r};
	else
	{
		tr[u]={l,r};
		int mid=l+r>>1;
		build(u*2,l,mid);
		build(u*2+1,mid+1,r);
	}
}
void modify(int u,int l,int r,int v)
{
	tr[u].path.push_back(v);//经过的路径,存入v
	if(l<=tr[u].l&&r>=tr[u].r) tr[u].had.push_back(v);//该节点所属
	else
	{
		int mid=tr[u].l+tr[u].r>>1;
		if(r<=mid) modify(u*2,l,r,v);
		else if(l>mid) modify(u*2+1,l,r,v);
		else modify(u*2,l,r,v),modify(u*2+1,l,r,v);
	}
}
int query(int u,int l,int r)
{
	int ans=-1;//初始化ans
	while(tr[u].had.size())//该节点所属的name
	{
		//如果该名字已经被删除了,就一直pop
		if(st[tr[u].had.back()]) tr[u].had.pop_back();
		else break;
	}
	if(tr[u].had.size()) ans=max(ans,tr[u].had.back());
	if(l<=tr[u].l&&r>=tr[u].r)//如果已经被全部包含了,那么下面全部路径也就包含了
	{
		while(tr[u].path.size())
		{
			if(st[tr[u].path.back()]) tr[u].path.pop_back();
			else break;
		}
		if(tr[u].path.size()) ans=max(ans,tr[u].path.back());
		return ans;
	}
	int mid=tr[u].l+tr[u].r>>1;
	if(r<=mid) ans=max(ans,query(u*2,l,r));
	else if(l>mid) ans=max(ans,query(u*2+1,l,r));
	else ans=max(ans,max(query(u*2,l,r),query(u*2+1,l,r)));
	return ans;
}
int main()
{
	int n,m,cnt=0;
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--)
	{
		int op,l,r;
		scanf("%d",&op);
		if(op==1)
		{
			cin>>name[++cnt];
			scanf("%d%d",&l,&r);
			l++,r++;//记录要+1
			modify(1,l,r,cnt);
		}else
		{
			scanf("%d%d",&l,&r);
			l++,r++;
			int ans=query(1,l,r);
			if(ans==-1) printf(">_<\n");//不存在交集
			else
			{
				cout<

你可能感兴趣的:(算法,c++,c语言,数据结构)