Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.
Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1, a2, ..., an, and Banban's have brightness b1, b2, ..., bm respectively.
Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.
Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.
You are asked to find the brightness of the chosen pair if both of them choose optimally.
The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50).
The second line contains n space-separated integers a1, a2, ..., an.
The third line contains m space-separated integers b1, b2, ..., bm.
All the integers range from - 109 to 109.
Print a single integer — the brightness of the chosen pair.
2 2 20 18 2 14
252
5 3 -1 0 1 2 3 -1 0 1
2
In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.
In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.
就是按照题目顺序来
#includeusing namespace std;typedef long long ll;const ll INF=1e18;ll a[55],b[55];int main(){ ios::sync_with_stdio(false); int n,m; cin>>n>>m; for(int i=0; i >a[i]; for(int i=0; i >b[i]; ll ans=INF,t; for(int i=0; i
Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.
Big Banban is hesitating over the amount of money to give out. He considers loops to be lucky since it symbolizes unity and harmony.
He would like to find a positive integer n not greater than 1018, such that there are exactly k loops in the decimal representation of n, or determine that such n does not exist.
A loop is a planar area enclosed by lines in the digits' decimal representation written in Arabic numerals. For example, there is one loop in digit 4, two loops in 8 and no loops in 5. Refer to the figure below for all exact forms.
The first and only line contains an integer k (1 ≤ k ≤ 106) — the desired number of loops.
Output an integer — if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positivedecimal integer not exceeding 1018.
2
462
6
8080
#includeusing namespace std;const int INF=1e9;int main(){ ios::sync_with_stdio(false); int n; cin>>n; if(n>36) cout<<-1; else { if(!(n&1)) { for(int i=0;i
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
4 1 2 1 2
4
10 1 1 2 2 2 1 1 2 2 1
9
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
给你1 2序列,让你旋转其中的一个区段,得到最长非递减序列的长度最大
可以后缀和dp
#includeusing namespace std;const int N=2005;int n,a[N],dp[2][N];int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)dp[0][i]=dp[0][i-1]+(a[i]==1); for(int i=n;i>0;i--)dp[1][i]=dp[1][i+1]+(a[i]==2); int ma=0; for(int i=1;i<=n;i++)ma=max(ma,dp[0][i]+dp[1][i]); for(int i=1;i<=n;i++) { int l=0,r=0; for(int j=i;j<=n;j++) { if(a[j]==1)l++; else r++,l=max(0,--l); ma=max(ma,dp[0][i-1]+l+r+dp[1][j+1]); } } cout<
也可以直接贪心处理,就像上次?处理一样,能很巧妙的贪心过去,这个做法是O(n)的
#includeusing namespace std;int main(){ int n; cin>>n; int a=0,ab=0,aba=0,abab=0; for(int i=0,x;i