接上篇
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;
}