2019.5.11 Cross The Threshold Play 下

接上篇
H
CodeForces - 1117B Emotes

There are
n
n
emotes in very popular digital collectible card game (the game is pretty famous so we won’t say its name). The
i
i
-th emote increases the opponent’s happiness by
a
i
ai
units (we all know that emotes in this game are used to make opponents happy).
You have time to use some emotes only
m
m
times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than
k
k
times in a row (otherwise the opponent will think that you’re trolling him).
Note that two emotes
i
i
and
j
j
(
i≠j
i≠j
) such that
a
i

a
j
ai=aj
are considered different.
You have to make your opponent as happy as possible. Find the maximum possible opponent’s happiness.
Input
The first line of the input contains three integers
n,m
n,m
and
k
k
(
2≤n≤2⋅
10
5
2≤n≤2⋅105
,
1≤k≤m≤2⋅
10
9
1≤k≤m≤2⋅109
) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.
The second line of the input contains
n
n
integers
a
1
,
a
2
,…,
a
n
a1,a2,…,an
(
1≤
a
i

10
9
1≤ai≤109
), where
a
i
ai
is value of the happiness of the
i
i
-th emote.
Output
Print one integer — the maximum opponent’s happiness if you use emotes in a way satisfying the problem statement.
Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence:
4,4,5,4,4,5,4,4,5
4,4,5,4,4,5,4,4,5
.
問題鏈接: http://codeforces.com/problemset/problem/1117/B
問題簡述: 大意是給定一個大小爲n的序列,一共需要m個數字,每次取數字不能連續超過k次,求這m個數字最大權值合
問題分析: 說是n的序列,其實只需要最大跟次大的數,判斷k是否大於m,如果k>m則只需要連續取最大的就行了,否則每取k次最大的就取一次次大的,模擬即可
AC通過的C++語言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

ll a[1000000];

bool cmp(ll x,ll y)
{
    return x>y;
}

int main()
{
    ios::sync_with_stdio(false);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n,cmp);
    if(m<=k)
    {
        cout<<a[0]*m;
    }
    else
    {
        int x=m/(k+1);
        int y=m%(k+1);
        ll p=y*a[0];
        cout<<a[0]*k*x+p+a[1]*x;
    }
    return 0;
}

I
CodeForces - 1117A Best Subsegment

題目不給了…原網址看吧…博客寫不下了
問題鏈接: http://codeforces.com/problemset/problem/1117/A
問題簡述: 給定一個n的序列,求擁有最大算數平均數的子序列長度
問題分析: 求連續最大數的最大數量即可,因爲單個最大數爲最大平均值,每次加一個比最大數小的即減小了平均值
AC通過的C++語言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

ll k[1000000];

int main()
{
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    ll maxn=0;
    for(int i=0;i<n;i++)
    {
        cin>>k[i];
        maxn=max(k[i],maxn);
    }
    ll ans=0,t=0;
    for(int i=0;i<n;i++)
    {
        if(k[i]!=maxn)
        {
            ans=max(t,ans);
            t=0;
        }
        else t++;
    }
    cout<<max(ans,t);
    return 0;
}

J
CodeForces - 1119A Ilya and a Colorful Walk

Ilya lives in a beautiful city of Chordalsk.
There are
n
n
houses on the street Ilya lives, they are numerated from
1
1
to
n
n
from left to right; the distance between every two neighboring houses is equal to
1
1
unit. The neighboring houses are
1
1
and
2
2
,
2
2
and
3
3
, …,
n−1
n−1
and
n
n
. The houses
n
n
and
1
1
are not neighboring.
The houses are colored in colors
c
1
,
c
2
,…,
c
n
c1,c2,…,cn
so that the
i
i
-th house is colored in the color
c
i
ci
. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses
i
i
and
j
j
so that
1≤i<j≤n
1≤i<j≤n
, and they have different colors:
c
i

c
j
ci≠cj
. He will then walk from the house
i
i
to the house
j
j
the distance of
(j−i)
(j−i)
units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer
n
n
(
3≤n≤300000
3≤n≤300000
) — the number of cities on the street.
The second line contains
n
n
integers
c
1
,
c
2
,…,
c
n
c1,c2,…,cn
(
1≤
c
i
≤n
1≤ci≤n
) — the colors of the houses.
It is guaranteed that there is at least one pair of indices
i
i
and
j
j
so that
1≤i<j≤n
1≤i<j≤n
and
c
i

c
j
ci≠cj
.
Output
Print a single integer — the maximum possible distance Ilya can walk.
Examples
Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of
5−1=4
5−1=4
units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of
1
1
unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of
7−3=4
7−3=4
units.
問題鏈接: http://codeforces.com/problemset/problem/1119/A
問題簡述: 有一排n個房子,每個房子相鄰1m,每個房子給一個數字表示顏色,求從一個房子到另一個不同顏色的房子的最大距離
問題分析: 從第一個房子掃到最後一個房子掃一遍,再從最後的房子向前掃一遍,找到最大的距離即可,二重循環超時不必說
AC通過的C++語言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

int k[600000];

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>k[i];
    }
    int ans=0;
    for(int i=1;i<n;i++)
    {
        if(k[i]!=k[0])
            ans=max(ans,i-0);
    }
    for(int i=n-2;i>=0;i--)
    {
        if(k[i]!=k[n-1])
            ans=max(ans,n-1-i);
    }
    cout<<ans;
    return 0;
}

K
CodeForces - 1119B Alyona and a Narrow Fridge

Alyona has recently bought a miniature fridge that can be represented as a matrix with
h
h
rows and
2
2
columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.
在這裏插入圖片描述
An example of a fridge with
h=7
h=7
and two shelves. The shelves are shown in black. The picture corresponds to the first example.
Alyona has
n
n
bottles of milk that she wants to put in the fridge. The
i
i
-th bottle is
a
i
ai
cells tall and
1
1
cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.
Alyona is interested in the largest integer
k
k
such that she can put bottles
1
1
,
2
2
, …,
k
k
in the fridge at the same time. Find this largest
k
k
.
Input
The first line contains two integers
n
n
and
h
h
(
1≤n≤
10
3
1≤n≤103
,
1≤h≤
10
9
1≤h≤109
) — the number of bottles and the height of the fridge.
The second line contains
n
n
integers
a
1
a1
,
a
2
a2
, …,
a
n
an
(
1≤
a
i
≤h
1≤ai≤h
) — the heights of the bottles.
Output
Print the single integer
k
k
— the maximum integer such that Alyona can put the bottles
1
1
,
2
2
, …,
k
k
in the fridge at the same time. If Alyona can put all bottles in the fridge, print
n
n
. It is easy to see that Alyona can always put at least one bottle in the fridge.
Examples
Input
5 7
2 3 5 4 1
Output
3
Input
10 10
9 1 1 1 1 1 1 1 1 1
Output
4
Input
5 10
3 1 4 2 4
Output
5
Note
One of optimal locations in the first example is shown on the picture in the statement.
One of optimal locations in the second example is shown on the picture below.
在這裏插入圖片描述
One of optimal locations in the third example is shown on the picture below.
在這裏插入圖片描述
問題鏈接: http://codeforces.com/problemset/problem/1119/B
問題簡述: 有n瓶牛奶,和一個m格高的冰箱,冰箱每層能放兩瓶牛奶,給出每個牛奶的高度,判斷最多能放多少瓶牛奶(只能先放序列前面的牛奶才能放後面的)
問題分析: 開始沒清楚題目(只能先放前面的條件坑死我了),每次輸入一個數據排序一次,從最高的開始放,每次加2,當瓶子高度加起來超過m就輸出答案即可
AC通過的C++語言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

ll k[100000];

int main()
{
    ios::sync_with_stdio(false);
    ll n,h,ans;
    cin>>n>>h;
    for(int i=1;i<=n;i++)
    {
        cin>>k[i];
        sort(k,k+i+1);
        ll r=0,x=i;
        ans=i;
        while(x>=0)
        {
            r+=k[x];
            x-=2;
        }
        if(r>h)
        {
            ans-=1;
            break;
        }
    }
    cout<<ans;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章