AtCoder Beginner Contest 103 题解

本文解析了四个编程挑战题目,包括任务调度问题、字符串旋转判断、模运算求和的最大值及岛屿间的桥梁拆除最少数量问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - Task Scheduling Problem

Time limit : 2sec / Memory limit : 1024MB

Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |AjAi|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

  • All values in input are integers.
  • 1≤A1,A2,A3≤100

Input

Input is given from Standard Input in the following format:

A1 A2 A3

Output

Print the minimum total cost required to complete all the task.


Sample Input 1

Copy

1 6 3

Sample Output 1

Copy

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

  • Complete the first task at cost 0.
  • Complete the third task at cost 2.
  • Complete the second task at cost 3.

Sample Input 2

Copy

11 5 5

Sample Output 2

Copy

6

Sample Input 3

Copy

100 100 100

Sample Output 3

Copy

0

三个任务,首先可以不花钱完成一个,剩下两个花费是后者减去前者和,问最小花费。

代码实现:

/*
Look at the star
Look at the shine for U
*/ 
 
#include<bits/stdc++.h>
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
ll s[N];
 
int main()
{
	int i,j,k;
	for(i = 0;i < 3;i++)
	sl(s[i]);
	
	sort(s,s+3);
	ll sum = 0;
	for(i = 1;i < 3;i++)
	sum += (s[i]-s[i-1]);
	
	cout<<sum<<endl;
}

B - String Rotation


Time limit : 2sec / Memory limit : 1024MB

Score : 200 points

Problem Statement

You are given string S and T consisting of lowercase English letters.

Determine if S equals T after rotation.

That is, determine if S equals T after the following operation is performed some number of times:

Operation: Let S=S1S2…S|S|. Change S to S|S|S1S2…S|S|−1.

Here, |X| denotes the length of the string X.

Constraints

  • 2≤|S|≤100
  • |S|=|T|
  • S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S equals T after rotation, print Yes; if it does not, print No.


Sample Input 1

Copy

kyoto
tokyo

Sample Output 1

Copy

Yes
  • In the first operation, kyoto becomes okyot.
  • In the second operation, okyot becomes tokyo.

Sample Input 2

Copy

abc
arc

Sample Output 2

Copy

No

abc does not equal arc after any number of operations.


Sample Input 3

Copy

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Copy

Yes

S串每次都在向左移动一个,问移动的过程中是否会出现等于T串。

代码实现:

/*
Look at the star
Look at the shine for U
*/ 
 
#include<bits/stdc++.h>
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
char s[N],t[N];
 
int main()
{
	int i,j,k;
	scanf("%s%s",s,t);
	int l1 = strlen(s);
	int l2 = strlen(t);
	if(l1 != l2)
	{
		puts("No");
		return 0;
	}
	int sum = 0;
	while(sum < l1)
	{
		char ch = s[0];
		for(i = 1;i < l1;i++)
		s[i-1] = s[i];
		
		s[l1-1] = ch;
		if(strcmp(s,t) == 0)
		{
			puts("Yes");
			return 0;
		 } 
		 sum++;
	}
	
	puts("No");
}

C - Modulo Summation


Time limit : 2sec / Memory limit : 1024MB

Score : 300 points

Problem Statement

You are given N positive integers a1,a2,…,aN.

For a non-negative integer m, let f(m)=(m mod a1)+(m mod a2)+…+(m mod aN).

Here, X mod Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

  • All values in input are integers.
  • 2≤N≤3000
  • 2≤ai≤105

Input

Input is given from Standard Input in the following format:

N
a1 a2 … aN

Output

Print the maximum value of f.


Sample Input 1

Copy

3
3 4 6

Sample Output 1

Copy

10

f(11)=(11 mod 3)+(11 mod 4)+(11 mod 6)=10 is the maximum value of f.


Sample Input 2

Copy

5
7 46 11 20 11

Sample Output 2

Copy

90

Sample Input 3

Copy

7
994 518 941 851 647 2 581

Sample Output 3

Copy

4527

一个数去模除所有的数,使得最后和最大。 可以发现,总会找到一个数,当他模除所有数时都是当前数-1.

代码实现:

/*
Look at the star
Look at the shine for U
*/ 
 
#include<bits/stdc++.h>
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
ll s[N];
 
int main()
{
	ll i,j,k,n;
	sl(n);
	for(i = 0;i < n;i++)
	sl(s[i]);
	
	ll sum = 0;
	for(i = 0;i < n;i++)
	sum += s[i]-1;
	
	cout<<sum<<endl;
}

D - Islands War


Time limit : 2sec / Memory limit : 1024MB

Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N−1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the ai-th island from the west and the bi-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

  • All values in input are integers.
  • 2≤N≤105
  • 1≤M≤105
  • 1≤ai<biN
  • All pairs (ai,bi) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a1 b1
a2 b2
:
aM bM

Output

Print the minimum number of bridges that must be removed.


Sample Input 1

Copy

5 2
1 4
2 5

Sample Output 1

Copy

1

The requests can be met by removing the bridge connecting the second and third islands from the west.


Sample Input 2

Copy

9 5
1 8
2 7
3 5
4 6
7 9

Sample Output 2

Copy

2

Sample Input 3

Copy

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

Sample Output 3

Copy

4

桥从1到n排列成一条横线,m次询问,每次输入x,y代表x到y不能联通,问最后需要破坏多少桥。

画图可以看出就是在寻找相交的区间,相交区间可以排序后简单维护。

代码实现:
 

/*
Look at the star
Look at the shine for U
*/ 

#include<bits/stdc++.h>
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
struct node{
	ll x,y;
}p[N];

bool cmp(node a,node b)
{
	return a.y < b.y;
}

int main()
{
	ll i,j,k,n,m,x,y;
	sl(n);sl(m);
	for(i = 0;i < m;i++)
	{
		sl(p[i].x);sl(p[i].y);
	}
	sort(p,p+m,cmp);
	ll l = 0,sum = 0;
	for(i = 0;i < m;i++)
	{
		if(p[i].x >= l)
		{
			l = p[i].y;
			sum++;
		}
	}
	cout<<sum<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值