2021-01-25广州大学ACM寒假训练赛解题心得

https://vjudge.net/contest/419545


A - Airplane AtCoder - abc129_a

Problem Statement
There are three airports A A A, B B B and C C C, and flights between each pair of airports in both directions.

A one-way flight between airports A A A and B B B takes P P P hours, a one-way flight between airports B B B and C C C takes Q Q Q hours, and a one-way flight between airports C C C and A A A takes R R R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints
1 ≤ P , Q , R ≤ 100 1 ≤ P , Q , R ≤ 100 1P,Q,R100 All values in input are integers.

m i n ( P + Q , P + R , Q + R ) min(P+Q, P+R, Q+R) min(P+Q,P+R,Q+R) 即可。


B - Balance AtCoder - abc129_b

Problem Statement
We have N N N weights indexed 1 1 1 to N N N . The mass of the weight indexed i i i is W i W _i Wi .

We will divide these weights into two groups: the weights with indices not greater than T T T , and those with indices greater than T T T , for some integer 1 ≤ T < N 1 ≤ T < N 1T<N . Let S 1 S_1 S1 be the sum of the masses of the weights in the former group, and S 2 S_2 S2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S 1 S_1 S1 and S 2 S_2 S2 .

Constraints
2 ≤ N ≤ 100 , 1 ≤ W i ≤ 100 2 ≤ N ≤ 100,1 ≤ W_i ≤ 100 2N1001Wi100 All values in input are integers.

数据范围很小,枚举 T T T 的位置,前后分别求和做差,取差的最大值即可。


C - Typical Stairs AtCoder - abc129_c

Problem Statement
There is a staircase with N N N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0 0 0-th step. He can climb up one or two steps at a time.

However, the treads of the a 1 a_1 a1-th, a 2 a_2 a2-th, a 3 a_3 a3-th, … , a M a_M aM-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N N N-th step, without setting foot on the broken steps? Find the count modulo 1 , 000 , 000 , 007 1 ,000,000,007 1,000,000,007 .

Constraints
1 ≤ N ≤ 1 0 5 , 0 ≤ M ≤ N − 1 , 1 ≤ a 1 < a 2 < . . . < a M ≤ N − 1 1 ≤ N ≤ 10^5 ,0 ≤ M ≤ N − 1,1 ≤ a_1 < a_2 < . . . < a_M ≤ N − 1 1N105,0MN1,1a1<a2<...<aMN1

基本模型是动态规划,第 i i i 个台阶的情况来自于第 i − 1 , i − 2 i-1,i-2 i1,i2 个台阶的情况, d p [ i ] = ( d p [ i − 1 ] + d p [ i − 2 ] ) % m o d dp[i] = (dp[i - 1] + dp[i - 2]) \% mod dp[i]=(dp[i1]+dp[i2])%mod,若第 k k k 个台阶坏掉,则 d p [ k ] = 0 dp[k] = 0 dp[k]=0,另外特别注意第一个台阶和第二个台阶坏掉的情况。


D - The Rank CodeForces - 1017A

Problem Description
John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.

There are n students, each of them has a unique id (from 1 to n). Thomas’s id is 1. Every student has four scores correspond to his or her English, German, Math, and History scores. The students are given in order of increasing of their ids.

In the table, the students will be sorted by decreasing the sum of their scores. So, a student with the largest sum will get the first place. If two or more students have the same sum, these students will be sorted by increasing their ids.

Please help John find out the rank of his son.

Input
The first line contains a single integer n (1≤n≤1000) — the number of students.

Each of the next n lines contains four integers ai, bi, ci, and di (0≤ai,bi,ci,di≤100) — the grades of the i-th student on English, German, Math, and History. The id of the i-th student is equal to i.

Output
Print the rank of Thomas Smith. Thomas’s id is 1.

由于Thomas的id是1,所以之后任何一个与他同分的都排在他的后面,所以只需要在读入的时候统计比Thomas总分高的人数就可以了,答案就是人数+1。


E - The Bits CodeForces - 1017B

Problem Description
Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:

Given two binary numbers a a a and b b b of length n n n. How many different ways of swapping two digits in a a a (only in a a a, not b b b) so that bitwise OR of these two numbers will be changed? In other words, let c c c be the bitwise OR of a a a and b b b, you need to find the number of ways of swapping two bits in a a a so that bitwise OR will not be equal to c c c.

Note that binary numbers can contain leading zeros so that length of each number is exactly n n n.

Bitwise OR is a binary operation. A result is a binary number which contains a one in each digit if there is a one in at least one of the two numbers. For example, 010102 010102 010102 OR 100112 = 110112 100112 = 110112 100112=110112.

Well, to your surprise, you are not Rudolf, and you don’t need to help him… You are the security staff! Please find the number of ways of swapping two bits in a so that bitwise OR will be changed.

Input
The first line contains one integer n n n ( 2 ≤ n ≤ 105 ) (2≤n≤105) (2n105) — the number of bits in each number.

The second line contains a binary number a a a of length n n n.

The third line contains a binary number b b b of length n n n.

bitwise OR 的结果改变肯定只来自于串 a a a 中交换的两个位,因此只需要考察这两个位置 a a a b b b 的情况。对于 a , b a,b a,b 同一位置的情况,可以分为以下四种情况:

编号ab
A00
B01
C10
D11

手动模拟后发现,只有其中 A C , A D , B C , C A , C B , D A AC,AD,BC,CA,CB,DA AC,AD,BC,CA,CB,DA 的交换情况会使结果改变,又因为顺序无关,所以只需要 A C , A D , B C AC,AD,BC AC,AD,BC。因此只要计算 A A A B B B C C C D D D出现的次数,输出 A ∗ C + A ∗ D + B ∗ C A*C+A*D+B*C AC+AD+BC 即可。本题思考的关键在于如何避免两两枚举交换的位,根据有贡献的交换的特征,设法将其拆开来统计。


F - News Distribution CodeForces - 1167C

Problem Description
In some social network, there are n n n users communicating with eachother in m m m groups of friends. Let’s analyze the process of distributing some news between users.

Initially, some user x x x receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on.
The process ends when there is no pair of friends such that one of
them knows the news, and another one doesn’t know.

For each user x x x you have to determine what is the number of users that will know the news if initially only user x x x starts distributing it.

Input
The first line contains two integers n n n and m m m ( 1 ≤ n , m ≤ 5 ⋅ 1 0 5 ) (1≤n,m≤5⋅10^5) (1n,m5105) — the number of users and the number of groups of friends, respectively.

Then m lines follow, each describing a group of friends. The i i i-th line begins with integer k i k_i ki ( 0 ≤ k i ≤ n ) (0≤k_i≤n) (0kin) — the number of users in the i i i-th group. Then ki distinct integers follow, denoting the users belonging to the i i i-th group.

It is guaranteed that ∑ i = 1 m k i ≤ 5 ⋅ 1 0 5 ∑_{i=1}^{m}k_i≤5⋅10^5 i=1mki5105.

Output
Print n n n integers. The i i i-th integer should be equal to the number of users that will know the news if user i i i starts distributing it.

从某一个用户开始的所有有交集的圈子最后都会传达到,所以我们干脆把所有有交集的圈子用并查集合并到一起。但题目给出的是每个圈子里的人,我们要把他转化成每个人所在的圈子。由于人数和圈数都很多,直接开二维不明智,所以决定用链式前向星,把所在的圈子串成链。但后来发现如果边串边用并查集合并,前面存进去的再也用不上,所以只需要first数组,不需要nxt数组,更加方便。最后每个人对其所在的大圈子有一个贡献,查询的时候取大圈子的人数即可。需要注意的是,有的人可能不属于任何一个圈子。还有并查集记得路径压缩和启发式合并。


G - Regular Bracket Sequence Again? Gym - 102766D

Problem Description
You have a collection of N N N opening brackets ‘(’ and N N N closing brackets ‘)’.

A bracket sequence is N N N periodic if it has a period of length N N N.

Example - “)()(” N N N=2. It is N N N periodic but “)(()” is not.

Your task is to find out how many distinct regular bracket sequence can be formed using these 2 N 2N 2N brackets such that sequence is not N N N periodic.

As the answer can be rather large, print the remainder after dividing it by 1000000007 ( 1 0 9 + 7 ) 1000000007(10^9+7) 1000000007(109+7).

Recall what the regular bracket sequence is:

“()” is regular bracket sequence. if s is regular bracket sequence then “(”+s+")" is regular bracket sequence. if s and t are regular bracket sequences then s+t is regular bracket sequence. For example, “()()”, “(())()”, “(())” and “()” are regular bracket sequences, but “)(”, “()(” and “)))” are not.

Input
The first line contains a single integer T ( 1 ≤ T ≤ 1 0 3 ) T(1≤T≤10^3) T(1T103) — the number of test cases in the input. Then T T T test cases follow.

Each query contains one integer N ( 1 ≤ N ≤ 1 0 3 ) N(1≤N≤10^3) N(1N103): the number of opening and closing brackets you have.

Output
For each test from the input print the number of distinct regular bracket sequence we can form using N N N opening bracket and N N N closing bracket and sequence is not N N N periodic modulo 1000000007 ( 1 0 9 + 7 ) 1000000007(10^9+7) 1000000007(109+7).

Print the answers to the tests in the order in which the tests are given in the input.

Example
Input:
4
1
2
3
4
Output:
1
1
5
12
Note
In the first query — The regular bracket sequence which can be formed by 1 1 1 opening bracket and 1 1 1 closing bracket is “()”.

In the second query — The regular bracket sequence which can be formed by 2 2 2 opening bracket and 2 2 2 closing bracket are “(())” and “()()”. We discard second one because “()()” is 2 2 2 periodic.

In the third query — The regular bracket sequence which can be formed by 3 3 3 opening bracket and 3 3 3 closing bracket are “((()))” , “(())()” , “()(())” , “()()()” and “(()())”. No one is 3 3 3 periodic.

貌似与卡特兰数有关,由于没做出来所以不写。


H - Singhal and GCD Gym - 102767C

Problem Description
For the multiset of positive integers a = ( a 1 , a 2 , … , a k ) a=(a_1,a_2,…,a_k) a=(a1,a2,,ak) define the Greatest Common Divisor ( G C D GCD GCD) of a as:

g c d ( a ) gcd(a) gcd(a) is the maximum positive integer x x x, such that all integers in multiset a a a are divisible on x x x.

For example, g c d ( 8 , 12 , 16 ) = 4 gcd(8,12,16)=4 gcd(8,12,16)=4.

Singhal has a sequence consisting of n n n integers. He wants to find the subarray of (length> 1 1 1) whose g c d gcd gcd is maximum possible. If there exist multiple subarrays then choose the largest in length. Help him to find the maximum g c d gcd gcd he can obtain and length of this subarray.

A subarray is the sequence of consecutive elements of the array.

Input
The first line contains a single integer t ( 1 ≤ t ≤ 1 0 5 ) t(1≤t≤10^5) t(1t105) — the number of test cases in the input. Then t t t test cases follow.

Each query contains two lines. The first line contains one integer n ( 2 ≤ n ≤ 1 0 5 ) n(2≤n≤10^5) n(2n105): the number of integers in the sequence, and the second line contains n n n integers a 1 , … , a n ( 1 ≤ a i ≤ 1 0 9 ) a_1,…,a_n(1≤a_i≤10^9) a1,,an(1ai109).

It is guaranteed that the total sum of n is at most 1 0 5 10^5 105.

Output
Print t t t answers to the test cases. Each answer must be consisting of two integers separate by a space — the maximum g c d gcd gcd he can obtain and the length of the maximum subarray.

没做出来。


I - Super Jumping! Jumping! Jumping! HDU - 1087

Problem Description
Nowadays, a kind of chess game called “Super Jumping! Jumping!
Jumping!” is very popular in HDU. Maybe you are a good boy, and know
little about this game, so I introduce it to you now.

The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given chessmen list.
Input
Input contains multiple test cases. Each test case is described in a line as follow: N N N v a l u e 1 value_1 value1 v a l u e 2 value_2 value2 … … v a l u e N value_N valueN It is guarantied that N N N is not more than 1000 1000 1000 and all v a l u e i value_i valuei are in the range of 32 32 32-int. A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the maximum according to rules, and one line one case.

看了半天发现是个最长上升子序列裸题, O ( n 2 ) O(n^2) O(n2) 即可解。


J - Max Sum HDU - 1231

给定K个整数的序列 N 1 , N 2 , . . . , N K { N_1, N_2, ..., N_K } N1,N2,...,NK,其任意连续子序列可表示为 N i , N i + 1 , . . . , N j { N_i, N_{i+1}, ..., N_j } Ni,Ni+1,...,Nj,其中 1 < = i < = j < = K 1 <= i <= j <= K 1<=i<=j<=K。最大连续子序列是所有连续子序列中元素和最大的一个, 例如给定序列 − 2 , 11 , − 4 , 13 , − 5 , − 2 { -2, 11, -4, 13, -5, -2} 2,11,4,13,5,2,其最大连续子序列为 11 , − 4 , 13 { 11, -4, 13 } 11,4,13,最大和 为 20 20 20
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 子序列的第一个和最后一个元素。
Input
测试输入包含若干测试用例,每个测试用例占 2 2 2 行,第 1 1 1 行给出正整数 K ( < 10000 ) K( < 10000) K(<10000),第 2 2 2 行给出 K K K 个整数,中间用空格分隔。当 K K K 0 0 0 时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第 2 2 2 3 3 3 组)。若所有 K K K 个元素都是负数,则定义其最大和为 0 0 0,输出整个序列的首尾元素。
Sample Input
6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
Sample Output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0

边界条件加上 0 0 0 的存在有点烦人,没调出来。大致上是先做前缀和,然后取 m a x { s j − s i ∣ 0 < = i < j < = n } max\{s_j - s_i|0<=i<j<=n\} max{sjsi0<=i<j<=n},方法是扫一遍最大值,然后在最大值左边扫最小值,更新一次差值,然后往右边重复这个步骤,直到没法再往右或者差值不再变大。

while (curj < k) {
	for(int j = curi + 1; j <= k; ++j)
		if (sum[j] > sum[curj])
			curj = j;
	for(int i = curi + 1; i <= curj - 1; ++i)
		if (sum[i] < sum[curi])
			curi = i;
	if (sum[curj] - sum[curi] > sum[maxj] - sum[mini])
		mini = curi, maxj = curj;
	else break;
}
		

上面这段代码只是大概思路,具体的边界条件还是有错。


PS:题面处理有点烦人,下次干脆传图片好了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值