A. A Compatible Pair
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print a single integer — the brightness of the chosen pair.

Examples
input
Copy
2 2
20 18
2 14
output
252
input
Copy
5 3
-1 0 1 2 3
-1 0 1
output
2
Note

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.

 

 

这场我竟然是涨分的
直接暴力啊,n^3的枚举,枚举去掉一个数,然后每次取组合的的最大值,再取最大值的最小值

就是按照题目顺序来

#include<bits/stdc++.h>
using 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<n; i++)
        cin>>a[i];
    for(int i=0; i<m; i++)
        cin>>b[i];
    ll ans=INF,t;
    for(int i=0; i<n; i++)
    {
        t=-INF;
        for(int j=0; j<n; j++)
        {
            if(j==i)continue;
            for(int k=0; k<m; k++)t=max(t,a[j]*b[k]);
        }
        ans=min(ans,t);
    }
    cout<<ans;
    return 0;
}
B. A Prosperous Lot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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.

Codeforces Round #462 (Div. 2)
Input

The first and only line contains an integer k (1 ≤ k ≤ 106) — the desired number of loops.

Output

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.

Examples
input
Copy
2
output
462
input
Copy
6
output
8080
B是个找规律的构造
>36,也就是2*18就不行了,所以这个题目dfs应该也可做吧
#include<bits/stdc++.h>
using 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<n/2;i++)
            cout<<8;
       }
       else
       {
           cout<<6;
            for(int i=0;i<n/2;i++)cout<<8;
       }
   }
    return 0;
}
C. A Twisty Movement
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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).

Output

Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.

Examples
input
Copy
4
1 2 1 2
output
4
input
Copy
10
1 1 2 2 2 1 1 2 2 1
output
9
Note

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

#include<bits/stdc++.h>
using 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<<ma;
    return 0;
}

也可以直接贪心处理,就像上次?处理一样,能很巧妙的贪心过去,这个做法是O(n)的

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a=0,ab=0,aba=0,abab=0;
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        if(x==1)a++,aba++;
        else ab++,abab++;
        ab=max(ab,a);
        aba=max(aba,ab);
        abab=max(abab,aba);
    }
    cout<<abab;
    return 0;
}

 

相关文章:

  • 2021-12-04
  • 2021-11-19
  • 2021-04-27
  • 2021-08-14
  • 2021-08-20
  • 2021-06-21
  • 2021-07-31
猜你喜欢
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-12
  • 2021-04-24
相关资源
相似解决方案