[ATcoder Beginner Contest #143]

A - Curtain

Time Limit: 2 sec / Memory Limit: 1024 MB

Problem Statement

The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)

We will close the window so as to minimize the total horizontal length of the uncovered part of the window. Find the total horizontal length of the uncovered parts of the window then.

Constraints

1≤A≤100
1≤B≤100
A and B are integers.

Input

Input is given from Standard Input in the following format:
A B

Output

Print the total horizontal length of the uncovered parts of the window.

Sample Input 1
12 4
Sample Output 1
4
We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4.

Sample Input 2
20 15
Sample Output 2
0
If the window is completely covered, print 0.

Sample Input 3
20 30
Sample Output 3
0
Each curtain may be longer than the window.

题目大意

窗户长度是A,窗户上有两个窗帘,每个窗帘的长度是B,问窗户最多还有多长没有被窗帘覆盖。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int a, b;
	cin >> a >> b;
	if(a-2*b>0)
		cout << a-2*b;
	else
		cout << 0;
	return 0;
}

B - TAKOYAKI FESTIVAL 2019

Time Limit: 2 sec / Memory Limit: 1024 MB

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!
This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is di.
As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore xy health points.
There are N×(N−1)/2 ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these N
(N−1)/2values.

Constraints

All values in input are integers.
2≤N≤50
0≤di≤100

Input

Input is given from Standard Input in the following format:
N d1 d2...dN

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

Sample Input 1
3
3 1 2
Sample Output 1
11
There are three possible choices:
Eat the first and second takoyaki. You will restore 3 health points.
Eat the second and third takoyaki. You will restore 2 health points.
Eat the first and third takoyaki. You will restore 6 health points.
The sum of these values is 11.

Sample Input 2
7
5 0 7 8 3 3 2
Sample Output 2
312

题目大意

有N个美味值,健康值为两个美味值之积。显然有N*N/2种组合,现在求所有组合的美味值之和。

#include <bits/stdc++.h>
using namespace std;
int a[60], n, sum;
int main(){
	cin >> n;
	for(int i=1; i<=n; i++)
		cin >> a[i];
	for(int i=1; i<n; i++)
		for(int j=i+1; j<=n; j++)
			sum += a[i]*a[j];
	cout << sum;
	return 0;
}

C - Slimes

Time Limit: 2 sec / Memory Limit: 1024 MB

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.
Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.
Ultimately, how many slimes will be there?

Constraints

1≤N≤10^5
|S|=N
S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:
N
S

Output

Print the final number of slimes.

Sample Input 1
10
aabbbbaaca
Sample Output 1
5
Ultimately, these slimes will fuse into abaca.

Sample Input 2
5
aaaaa
Sample Output 2
1
All the slimes will fuse into one.

Sample Input 3
Copy
20
xxzaffeeeeddfkkkkllq
Sample Output 3
Copy
10

题目大意

有N个小写字母,连续的相同的字母当成是同一种,问这N个字母中,共有几种字母。

#include <bits/stdc++.h>
using namespace std;
int main(){
	char a = '0', b;
	int n, ans = 0;
	cin >> n;
	for(int i=1; i<=n; i++){
		cin >> b;
		if(b!=a){
			ans++;
			a = b;
		}
	}
	cout << ans;
	return 0;
}

E - Travel by Car

Time Limit: 2 sec / Memory Limit: 1024 MB

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town Ai and Town Bi bidirectionally and has a length of Ci.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:
The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town si to Town ti. If Town ti is unreachable, print −1.

Constraints

All values in input are integers.
2≤N≤300
0≤M≤N(N−1)/2
1≤L≤10^9
1≤Ai,Bi≤N
Ai≠Bi(Ai,Bi)≠(Aj,Bj) (if i≠j)
(Ai,Bi)≠(Bj,Aj) (if i≠j)
1≤Ci≤10^9
1≤Q≤N(N−1)
1≤si,ti≤N
si≠ti(si,ti)≠(sj,tj) (if i≠j)

Input

Input is given from Standard Input in the following format:
N M L
A1 B1 C1
:
AM BM CM
Q
s1 t1
:
sQ tQ

Output

Print Q lines.
The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town si to Town ti. If Town ti is unreachable, the line should contain −1 instead.

Sample Input 1
3 2 5
1 2 3
2 3 3
2
3 2
1 3
Sample Output 1
0
1
To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.
To travel from Town 1 to Town 3, we can first use the first road to get to Town2, full the tank, and use the second road to reach Town 3.

Sample Input 2
4 0 1
1
2 1
Sample Output 2
-1
There may be no road at all.

Sample Input 3
5 4 4
1 2 2
2 3 2
3 4 3
4 5 2
20
2 1
3 1
4 1
5 1
1 2
3 2
4 2
5 2
1 3
2 3
4 3
5 3
1 4
2 4
3 4
5 4
1 5
2 5
3 5
4 5
Sample Output 3
0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0

题目大意

一个无向图,有N个小镇,有M条边,汽车油箱容量为L,每走1单位的距离,消耗1单位的汽车,汽车一开始时满油。每个小镇都可以加油,有Q个询问,问从s小镇到t小镇,最少需要加油多少次,如果无法到达,输出-1。

题解

跑两次floyd,第一次求任意两个小镇之间的最短路a[i][j],然后如果a[i][j]<=L,说明在一次油之内可以通过(实际上是不需要加油),用这个思路给每天边重新赋予权值,再求一次floyd,就可以得到最小要加油的次数。当然要对i=j的情况进行特判、还有b[i][j]实际上是算多了一次加油次数。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 310;
const long long INF = 1e13; // 注意数据范围 
long long a[maxn][maxn], b[maxn][maxn]; 
int n, m, L;

void init(){
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			if(i==j){
				a[i][j] = 0;
				b[i][j] = 0;
			}else{
				a[i][j] = INF;
				b[i][j] = INF;
			}
}

int main(){
	cin >> n >> m >> L;
	int x, y, z;
	init();
	for(int i=1; i<=m; i++){
		cin >> x >> y >> z;
		if(z<=L && z<a[x][y])
			a[y][x] = a[x][y] =  z;
	}
	for(int k=1; k<=n; k++) // 求出各个点之间的最短路径 
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				a[i][j] = min(a[i][j], a[i][k]+a[k][j]);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			if(i!=j && a[i][j]<=L) //当成是加一次油 
				b[i][j] = 1;
	// 以加油次数为权值(b[i][j]),再求一次floyd,就可以求出最小加油次数
	for(int k=1; k<=n; k++)
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				b[i][j] = min(b[i][j], b[i][k]+b[k][j]);

	int q;
	cin >> q;
	for(int i=1; i<=q; i++){
		cin >> x >> y;
		if(x==y)
			cout << 0 << endl;
		else{
			if(b[x][y]>=INF)
				cout << -1 << endl;
			else
				cout << b[x][y]-1 << endl;  // 不超过L时,默认为1,实际要减去1 
		}
	}
	return 0;
}

posted @ 2019-10-21 14:46  gdgzliu  阅读(424)  评论(0编辑  收藏  举报