周报4.10-4.17
作者:互联网
最近在做认识实习的期末小结,里面刚好有关于构造的题目。
构造这方面的内容我一直觉得很抽象,之前没接触过的时候(其实是我不知道自己用到了构造)觉得是类似于数学论证里的“不妨设。。。”,没人说理。不过我做了几道题发现还是比我想象中的有头有尾。
有些题目没什么难度,基本就是稍加思索,可能在分类上很是麻烦,有些就显得比较抽象。
一题之前没做出来,但我个人觉得是自己的问题,题目不算很抽象的那部分。
直接上题:
You are given an array aa of n elements.
Your can perform the following operation no more than n times: Select three indices x,y,z (1<=x<y<z<=n) and replace ax with ay−az. After the operation, ∣ax∣ need to be less than 10^{18}.
Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well.
Input:
Each test contains multiple test cases. The first line will contain a single integer tt (1 <= t <= 10000) — the number of test cases. Then tt test cases follow.
The first line of each test case contains a single integer n (3≤n≤2⋅105) — the size of the array a.
The second line of each test case contains nn integers a1,a2,…,an (−109≤ai≤109), the elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 2*10^5.
Output:
For each test case, print -1 in a single line if there is no solution. Otherwise in the first line you should print a single integer m (0≤m≤n) — number of operations you performed.
Then the i-th of the following mm lines should contain three integers x,y,z (1≤x<y<z≤n)— description of the i-th operation.
If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task.
这道题目如果不用构造,那必然超时毫无疑问,但是构造是要找到一个切入点的,这道题里面的关键点事实上是非递减这个条件。非递减是包含了相等的,如果想不到这一点就难以展开下去。但实际上这还不够,因为构造的框架定下以后的细节处理也很重要,选用一个定值作为元素的值,也需要先判断是否能够这样做,考虑到最后两项的特殊性,如果a[n]>=a[n-1]不成立,那数组就不可能递减,因此大可以先判定最后两项的情况,也顺便将这两个值的差作为代替其他元素的值,一举两得,时间复杂度O(c);
另外一题就比较抽象,更加容易走偏:
You are given three integers n, a, b. Determine if there exists a permutation p1,p2,…,pn of integers from 1 to n, such that:
-
There are exactly a integers i with 2≤i≤n−1 such that pi−1<pi>pi+1 (in other words, there are exactly a local maximums).
-
There are exactly b integers i with 2≤i≤n−1 such that pi−1>pi<pi+1 (in other words, there are exactly b local minimums).
If such permutations exist, find any such permutation.
Input:
The first line of the input contains a single integer t (1≤t≤10^4) — the number of test cases. The description of test cases follows.
The only line of each test case contains three integers n, a and b (2≤n≤10^5, 0≤a,b≤n).
The sum of n over all test cases doesn't exceed 10^5.
Output:
For each test case, if there is no permutation with the requested properties, output -1.
Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
这道题要求构造出一个数组,其中包括了1到n的每一个整数,然后要求里面含有任意的项,他们有些要大于两边的项,有些要小于两边的项。
因为这道题一开始就限定死了这些数的全集,因此在构造的时候总是处处受限,比如说我本来要排列数据,那么排到最后几位的时候,就发现傻了,剩下的数总是会导致存在超出要求数量的特殊项,这就不得不逼着我去把前面排好的给拆散,顺带一提我一开始打算182736这样排,但是就像前面说的心力交瘁也归结不出一个规律,总是会被后续的排列打乱。
查阅了答案,我发现他的做法是将特殊项比作凹凸,因此凹凸的差不可能大于1(惭愧表示这一点也没想到),这大大缩小了需要构造的范围,而后他采用了一种交换的手法,交换递增数列前面的数,那么可能构造出小于两边项的项,反之亦然,就这样依靠交换前面交换后面这样并不复杂的手法,解决了这个十分抽象的问题,重点就在于他能够归结出计算机方便操作的规律。
构造的思想其实就在于归结,在很多不需要求取全部输出的题目中,出题人很可能会把数据范围做的很大,只有避开题目所引导的解题思路,重新归结一条直通路,才能够化繁为简。
标签:4.10,4.17,integers,there,构造,test,such,line,周报 来源: https://www.cnblogs.com/riiichard/p/16157937.html