Rday6
Connecting Vertices
There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counterclockwise order). You can draw n  1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).
But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).
How many ways are there to connect all vertices with n  1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 109 + 7.
Input
The first line contains one number n (3 ≤ n ≤ 500) — the number of marked points.
Then n lines follow, each containing n elements. ai, j (jth element of line i) is equal to 1 iff you can connect points i and j directly (otherwise ai, j = 0). It is guaranteed that for any pair of points ai, j = aj, i, and for any point ai, i = 0.
Output
Print the number of ways to connect points modulo 109 + 7.
1  Examples 
题解
:
题解：
区间dpdp
f[i][j]f[i][j]表示ii到jj有边且构成树的方案数
g[i][j]g[i][j]表示ii到jj无边且构成树的方案数
转移：枚举i,ji,j左右端点，kk是断点
f[i][j]+=∑j−1k=i(f[i][k]+g[i][k])∗(f[k+1][j]+g[k+1][j])f[i][j]+=∑k=ij−1(f[i][k]+g[i][k])∗(f[k+1][j]+g[k+1][j])
g[i][j]+=∑j−1k=i+1f[k][j]∗(f[i][k]+g[i][k])g[i][j]+=∑k=i+1j−1f[k][j]∗(f[i][k]+g[i][k])
初始化只要f[i][j]=1f[i][j]=1就可以了
code:
1  #include <cstdio> 
Local Extrema
You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai  1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai  1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.
An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.
Input
The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a.
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 1000) — the elements of array a.
Output
Print the number of local extrema in the given array.
1  Examples 
题解
:
签到题，按照题意写就行了
code:
1 

XorMST
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.
Calculate the weight of the minimum spanning tree in this graph.
Input
The first line contains n (1 ≤ n ≤ 200000) — the number of vertices in the graph.
The second line contains n integers a1, a2, …, an (0 ≤ ai < 230) — the numbers assigned to the vertices.
Output
Print one number — the weight of the minimum spanning tree in the graph.
1  Examples 
题意
:
不妨从高到低贪心，我们把最高位按01分开两半分治，跨越两半的就在trie上贪心，这样做是O(nlog2n)O(n\log^2n)O(nlog 2n)的
dalao’s code:
1 

Buggy Robot
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:
U — move from the cell (x, y) to (x, y + 1);
D — move from (x, y) to (x, y  1);
L — move from (x, y) to (x  1, y);
R — move from (x, y) to (x + 1, y).
Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!
Input
The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).
The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.
Output
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
1  Examples 
题意
:
最大能够执行的命令数量
code:
1 

KDominant Character
You are given a string s consisting of lowercase Latin letters. Character c is called kdominant iff each substring of s with length at least k contains this character c.
You have to find minimum k such that there exists at least one kdominant character.
Input
The first line contains string s consisting of lowercase Latin letters (1 ≤ s ≤ 100000).
Output
Print one number — the minimum value of k such that there exists at least one kdominant character.
1  Examples 
题意
:
给你一个长度为n字符串，求最小的长度m，使得字符串中所有长度为m的子字符串中均包含某一种字符。
1 

Maximum Subsequence
You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, …, bk (1 ≤ b1 < b2 < … < bk ≤ n) in such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value of .
Input
The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109).
Output
Print the maximum possible value of .
1  Examples 
题意
:
给你一个大小为n的数组和一个数m，求从数组中挑出任意多个数（可以为零，不可以重复），计算出
的最大值，其中k为选出的数的个数。
超大背包，将所有物品平均分成两部分，然后枚举所有状态。再枚举某一堆物品的所有值，二分从另一堆查找最优解即可。
1  #include<bits/stdc++.h> 
Alomost Identity Permutations
A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.
Let’s call a permutation an almost identity permutation iff there exist at least n  k indices i (1 ≤ i ≤ n) such that pi = i.
Your task is to count the number of almost identity permutations for given numbers n and k.
Input
The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).
Output
Print the number of almost identity permutations for given n and k.
1  Examples 
题意
:
问n个数的全排列中，有多少排列满足∑pi=i的值大于等于n−k。
k≤4，枚举即可。
对于k=1 , res=1;
对于k=2 , res=C2n
对于k=3 , res=C3n∗2
对于k=4 , res=C4n∗9
上面每一行的意义在于：从n个数中挑出k个数，每种选法又分别多少种方案使得∑ki=1pi=1 的值为0。
code:
1  #include<bits/stdc++.h> 
Alyona and Spreadsheet
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.
Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the ith row and the jth column. We say that the table is sorted in nondecreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n  1.
Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in nondecreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r  1 inclusive.
Alyona is too small to deal with this task and asks you to help!
Input
The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following n lines contains m integers. The jth integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).
The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.
The ith of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).
Output
Print “Yes” to the ith line of the output if the table consisting of rows from li to ri inclusive is sorted in nondecreasing order in at least one column. Otherwise, print “No”.
1  Example 
题意
:
从n∗m的矩阵中挑出从第l行到第r行，问在这(r−l+1)∗m的矩阵中是否存在某一列，使得这一列的元素按序号从小到大非递减。
前缀和即可。计算每一行某个元素前面的所有的元素能够非递增的最小的起点，由于n，m的值未定，所以必须用一维数组来保存。
在查找[l,r]
行时，只需要看第r行的m个元素是否存在某个元素，他所在的非递减序列的起点小于等于l即可，由于不能用for循环查询，所以需要前缀和。
1 

Shell Game
Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.
Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).
Let’s number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?
Input
The first line of the input contains an integer n (1 ≤ n ≤ 2·109) — the number of movements made by the operator.
The second line contains a single integer x (0 ≤ x ≤ 2) — the index of the shell where the ball was found after n movements.
Output
Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.
1  Examples 
题意
:
三个杯子，初始某个杯子中有一个小球，经过n次下列操作后，小球在第x号杯子（0，1，2号）：
此次操作次数的序号为第奇数次，交换0号和1号杯子。
此次操作次数的序号为第偶数次，交换1号和2号杯子。
问初始小球在那个杯子。
模拟后发现，每6次操作一个循环，因此n mod 6后模拟计算即可。
code:
1 

Hanoi Factory
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.
There are n rings in factory’s stock. The ith ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:
Outer radiuses form a nonincreasing sequence, i.e. one can put the jth ring on the ith ring only if bj ≤ bi.
Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
The total height of all rings used should be maximum possible.Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory’s stock.
The ith of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the ith ring respectively.
Output
Print one integer — the maximum height of the tower that can be obtained.
1  Examples 
题意
:
给你n个圆环，每个圆环有一个外径和内径，现在将这些圆环堆起来，满足上面的圆环外径bi≤bj且bi>aj 其中aj,bj分别为下面一个圆环的内径和外径。问最高能将这些圆环堆多高。
一个简单的模拟题。。。（我居然写了一下午的区间更新查询线段树）
按照圆环外径从大到小排序，如果外径相同按照内径从大到小排序，然后按照顺序模拟即可，如果当前这个圆环放不到前面这个圆环，就继续往前面找，直到找到为止（这里可以用一个数组保存每个圆环找到的位置。
code:
1 

Cloud of Hashtags
Vasya is an administrator of a public page of organization “Mouse and keyboard” and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol ‘#’ located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol ‘#’.
The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).
Vasya is lazy so he doesn’t want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol ‘#’. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible. If there are several optimal solutions, he is fine with any of them.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of hashtags being edited now.
Each of the next n lines contains exactly one hashtag of positive length.
It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters ‘#’) won’t exceed 500 000.
Output
Print the resulting hashtags in any of the optimal solutions.
1  Examples 
题意
:
有n个新闻标题，你要删除最少的字符，使得最后所有的标题按照输入顺序字典序升序排列。并输出删除后的新闻标题。
首先知道如果两个标题的字典序比较为降序，那么一定是要删除前面的那个标题，使得那个标题字典序变小。所以我们从后往前删除，能够保证每个标题删除的字符数量最少。
code:
1 

Game of Credit Cards
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite ndigit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock’s card is 123 and Moriarty’s card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains n digits — Sherlock’s credit card number.
The third line contains n digits — Moriarty’s credit card number.
Output
First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
1  Examples 
题意
:
S 某,M某分别有一张序号长度为n的信用卡，他们定了一个规则：比赛分为n局，每局S某，M某从n个数字中分别不重复的取出一个数字，谁的数字小谁得一分，平局不算分。
问如果随机排列这些数字，S某可能的最高得分和M某可能的最低得分分别为多少。
模拟题，算最高分时只要从小到大让S某牌去压M某小于自己当前牌大小的牌即可。算最低分时，只要从大到小让M某去压S某大于等于自己当前牌大小的牌即可。
code:
1 
