Atcoder Begginer Contest 318 讲解(A~E题)

A - Full Moon

Description

Problem Statement

Takahashi likes full moons.
Let today be day 1 1 1. The first day on or after today on which he can see a full moon is day M M M. After that, he can see a full moon every P P P days, that is, on day M + P M+P M+P, day M + 2 P M+2P M+2P, and so on.
Find the number of days between day 1 1 1 and day N N N, inclusive, on which he can see a full moon.

Constraints

1 ≤ N ≤ 2 × 1 0 5 1\leq N\leq 2\times 10^5 1N2×105
1 ≤ M ≤ P ≤ 2 × 1 0 5 1\leq M \leq P \leq 2\times 10^5 1MP2×105
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N M M M P P P

Output

Print the answer as an integer.

Sample Input 1
13 3 5
Sample Output 1
3

He can see a full moon on day 3 3 3, 8 8 8, 13 13 13, 18 18 18, and so on.
From day 1 1 1 to 13 13 13, he can see a full moon on three days: day 3 3 3, 8 8 8, and 13 13 13.

Sample Input 2
5 6 6
Sample Output 2
0

There may be no days he can see a full moon.

Sample Input 3
200000 314 318
Sample Output 3
628

Solution

直接枚举每一天,判断一下,输出就行。
数学其实能直接算~~~


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int N, M, P;

	cin >> N >> M >> P;

	int Result = 0;
	for (int i = M; i <= N; i ++)
		if ((i - M) % P == 0)
			Result ++;

	cout << Result << endl;

	return 0;
}

B - Overlapping sheets

Description

Problem Statement

There are N N N rectangular sheets spread out on a coordinate plane.
Each side of the rectangular region covered by each sheet is parallel to the x x x- or y y y-axis.

Specifically, the i i i-th sheet covers exactly the region satisfying A i ≤ x ≤ B i A_i \leq x\leq B_i AixBi and C i ≤ y ≤ D i C_i \leq y\leq D_i CiyDi.
Let S S S be the area of the region covered by one or more sheets. It can be proved that S S S is an integer under the constraints.

Print S S S as an integer.

Constraints

2 ≤ N ≤ 100 2\leq N\leq 100 2N100
KaTeX parse error: Expected 'EOF', got '&' at position 10: 0\leq A_i&̲lt;B_i\leq 100
KaTeX parse error: Expected 'EOF', got '&' at position 10: 0\leq C_i&̲lt;D_i\leq 100
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
A 1 A_1 A1 B 1 B_1 B1 C 1 C_1 C1 D 1 D_1 D1
A 2 A_2 A2 B 2 B_2 B2 C 2 C_2 C2 D 2 D_2 D2
⋮ \vdots
A N A_N AN B N B_N BN C N C_N CN D N D_N DN

Output

Print the area S S S of the region covered by one or more sheets as an integer.

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

The three sheets cover the following regions.

Here, red, yellow, and blue represent the regions covered by the first, second, and third sheets, respectively.

Therefore, the area of the region covered by one or more sheets is S = 20 S=20 S=20.

Sample Input 2
2
0 100 0 100
0 100 0 100
Sample Output 2
10000

Note that different sheets may cover the same region.

Sample Input 3
3
0 1 0 1
0 3 0 5
5 10 0 10
Sample Output 3
65

Solution

直接暴力标记即可。
题外话:不会真的有人写扫描线吧!!!


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int N;

	cin >> N;

	std::vector<vector<int>> Vis(101, vector<int>(101, 0));
	for (int i = 1; i <= N; i ++)
	{
		int A, B, C, D;

		cin >> A >> B >> C >> D;
		for (int i = C + 1; i <= D; i ++) //注意一下边界
			for (int j = A + 1; j <= B; j ++)
				Vis[i][j] = 1;
	}

	int Result = 0;
	for (int i = 0; i <= 100; i ++)
		for (int j = 0; j <= 100; j ++)
			Result += Vis[i][j];

	cout << Result << endl;

	return 0;
}

C - Blue Spring

Description

Problem Statement

Takahashi is planning an N N N-day train trip.

For each day, he can pay the regular fare or use a one-day pass.
Here, for 1 ≤ i ≤ N 1\leq i\leq N 1iN, the regular fare for the i i i-th day of the trip is F i F_i Fi yen.

On the other hand, a batch of D D D one-day passes is sold for P P P yen. You can buy as many passes as you want, but only in units of D D D.

Each purchased pass can be used on any day, and it is fine to have some leftovers at the end of the trip.
Find the minimum possible total cost for the N N N-day trip, that is, the cost of purchasing one-day passes plus the total regular fare for the days not covered by one-day passes.

Constraints

1 ≤ N ≤ 2 × 1 0 5 1\leq N\leq 2\times 10^5 1N2×105
1 ≤ D ≤ 2 × 1 0 5 1\leq D\leq 2\times 10^5 1D2×105
1 ≤ P ≤ 1 0 9 1\leq P\leq 10^9 1P109
1 ≤ F i ≤ 1 0 9 1\leq F_i\leq 10^9 1Fi109
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N D D D P P P
F 1 F_1 F1 F 2 F_2 F2 … \ldots F N F_N FN

Output

Print the minimum possible total cost for the N N N-day trip.

Sample Input 1
5 2 10
7 1 6 3 6
Sample Output 1
20

If he buys just one batch of one-day passes and uses them for the first and third days, the total cost will be ( 10 × 1 ) + ( 0 + 1 + 0 + 3 + 6 ) = 20 (10\times 1)+(0+1+0+3+6)=20 (10×1)+(0+1+0+3+6)=20, which is the minimum cost needed.

Thus, print 20 20 20.

Sample Input 2
3 1 10
1 2 3
Sample Output 2
6

The minimum cost is achieved by paying the regular fare for all three days.

Sample Input 3
8 3 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
3000000000

The minimum cost is achieved by buying three batches of one-day passes and using them for all eight days.

Note that the answer may not fit into a 32 32 32-bit integer type.


Solution

  1. 排序
  2. 枚举买几个连续天数的票
  3. 通过贪心,一定是价钱最贵的那几天用连续天数的票,所以减去序列的前几项就可以了。不要忘记加上买连续天数的票的费用。

不要忘记买 0 0 0 个也是可以的!

前面几道讲的很快,不明白可以问我~~~


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int N, D, P;

	cin >> N >> D >> P;

	std::vector<int> F(N + 1);
	int Sum = 0;
	for (int i = 1; i <= N; i ++)
		cin >> F[i], Sum += F[i];

	sort(F.begin() + 1, F.end(), greater<int>());
	int last = 0, Times = 1, Result = Sum;
	for (int k = 1; k <= (N + D - 1) / D; k ++)
	{
		for (int i = last + 1; i <= min(last + D, N); i ++)
			Sum -= F[i];

		Result = min(Result, Times * P + Sum);
		Times ++;
		last = last + D;
	}

	cout << Result << endl;

	return 0;
}

D - General Weighted Max Matching

Problem Statement

You are given a weighted undirected complete graph with N N N vertices numbered from 1 1 1 to N N N. The edge connecting vertices i i i and j j j KaTeX parse error: Expected 'EOF', got '&' at position 3: (i&̲lt; j) has a weight of D i , j D_{i,j} Di,j.
When choosing some number of edges under the following condition, find the maximum possible total weight of the chosen edges.
The endpoints of the chosen edges are pairwise distinct.

Constraints

2 ≤ N ≤ 16 2\leq N\leq 16 2N16
1 ≤ D i , j ≤ 1 0 9 1\leq D_{i,j} \leq 10^9 1Di,j109
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
D 1 , 2 D_{1,2} D1,2 D 1 , 3 D_{1,3} D1,3 … \ldots D 1 , N D_{1,N} D1,N
D 2 , 3 D_{2,3} D2,3 … \ldots D 2 , N D_{2,N} D2,N
⋮ \vdots
D N − 1 , N D_{N-1,N} DN1,N

Output

Print the answer as an integer.

Sample Input 1
4
1 5 4
7 8
6
Sample Output 1
13

If you choose the edge connecting vertices 1 1 1 and 3 3 3, and the edge connecting vertices 2 2 2 and 4 4 4, the total weight of the edges is 5 + 8 = 13 5+8=13 5+8=13.
It can be shown that this is the maximum achievable value.

Sample Input 2
3
1 2
3
Sample Output 2
3

N N N can be odd.

Sample Input 3
16
5 6 5 2 1 7 9 7 2 5 5 2 4 7 6
8 7 7 9 8 1 9 6 10 8 8 6 10 3
10 5 8 1 10 7 8 4 8 6 5 1 10
7 4 1 4 5 4 5 10 1 5 1 2
2 9 9 7 6 2 2 8 3 5 2
9 10 3 1 1 2 10 7 7 5
10 6 1 8 9 3 2 4 2
10 10 8 9 2 10 7 9
5 8 8 7 5 8 2
4 2 2 6 8 3
2 7 3 10 3
5 7 10 3
8 5 7
9 1
4
Sample Output 3
75

Solution

暴力出奇迹!

直接暴力,但是暴力的方式一定要注意!
某位 伤心的蜜蜂 直接暴力对于每一条边,选择任意 2 2 2 个点(前提是没选过)然后来计算,喜提 TLE \text{TLE} TLE
其实,枚举每一个点就行,对于每一个点找到一个还没有选过的点选上就行。这样是不会炸的!


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

const int SIZE = 20;

int N;
int D[SIZE][SIZE], st[SIZE];
int Result = 0;

void DFS(int u, int sum)
{
	if (u > N)
	{
		Result = max(Result, sum);
		return;
	}

	for (int j = u + 1; j <= N; j ++)
		if (!st[u] && !st[j])
		{
			st[u] = st[j] = 1;
			DFS(u + 1, sum + D[u][j]);
			st[u] = st[j] = 0;
		}
	DFS(u + 1, sum);
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> N;

	for (int i = 1; i < N; i ++)
		for (int j = i + 1; j <= N; j ++)
			cin >> D[i][j];

	DFS(1, 0);

	cout << Result << endl;

	return 0;
}

E - Sandwiches

Description

Problem Statement

You are given a sequence of positive integers of length N N N: A = ( A 1 , A 2 , … , A N ) A=(A_1,A_2,\ldots,A_N) A=(A1,A2,,AN). Find the number of triples of positive integers ( i , j , k ) (i,j,k) (i,j,k) that satisfy all of the following conditions:
KaTeX parse error: Expected 'EOF', got '&' at position 9: 1\leq i &̲lt; j &lt; k\le…,
A i = A k A_i = A_k Ai=Ak,
A i ≠ A j A_i \neq A_j Ai=Aj.

Constraints

3 ≤ N ≤ 3 × 1 0 5 3\leq N\leq 3\times 10^5 3N3×105
1 ≤ A i ≤ N 1\leq A_i \leq N 1AiN
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
A 1 A_1 A1 A 2 A_2 A2 … \ldots A N A_N AN

Output

Print the answer as an integer.

Sample Input 1
5
1 2 1 3 2
Sample Output 1
3

The following three triples of positive integers ( i , j , k ) (i,j,k) (i,j,k) satisfy the conditions:
( i , j , k ) = ( 1 , 2 , 3 ) (i,j,k)=(1,2,3) (i,j,k)=(1,2,3)
( i , j , k ) = ( 2 , 3 , 5 ) (i,j,k)=(2,3,5) (i,j,k)=(2,3,5)
( i , j , k ) = ( 2 , 4 , 5 ) (i,j,k)=(2,4,5) (i,j,k)=(2,4,5)

Sample Input 2
7
1 2 3 4 5 6 7
Sample Output 2
0

There may be no triples of positive integers ( i , j , k ) (i,j,k) (i,j,k) that satisfy the conditions.

Sample Input 3
13
9 7 11 7 3 8 1 13 11 11 11 6 13
Sample Output 3
20

Solution

ABC318(E题)


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int N;

	cin >> N;

	std::vector<int> A(N + 1);
	std::vector<vector<int>> Pos(N + 1);
	for (int i = 1; i <= N; i ++)
		cin >> A[i], Pos[A[i]].push_back(i);

	int Result = 0;
	for (int i = 1; i <= N; i ++)
	{
		int add = 0, mns = 0, sum = 0;
		for (int j = 0; j < Pos[i].size(); j ++)
			mns += (Pos[i][j] * (Pos[i].size() - j - 1));
		for (int j = Pos[i].size() - 1; j >= 0; j --)
			add += (Pos[i][j] * j);
		for (int j = Pos[i].size() - 1; j >= 0; j --)
			sum += j * (Pos[i].size() - j);
		Result += add - mns - sum;
	}

	cout << Result << endl;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值