A. Best Subsegment

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given array a1,a2,…,ana1,a2,…,an. Find the subsegment al,al+1,…,aral,al+1,…,ar (1≤l≤r≤n1≤l≤r≤n) with maximum arithmetic mean 1r−l+1∑i=lrai1r−l+1∑i=lrai(in floating-point numbers, i.e. without any rounding).

If there are many such subsegments find the longest one.

Input

The first line contains single integer nn (1≤n≤1051≤n≤105) — length of the array aa.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the array aa.

Output

Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.

Example
input

Copy
5
6 1 6 6 0
output

Copy
2

The subsegment [3,4][3,4] is the longest among all subsegments with maximum arithmetic mean.

题解:题目意思就是让你找一个序列的子序列平均值最大的(如果均值相同,则找最长的)

这题是水题,其实就是找到最大的那个数Maxnum,然后统计Maxnum连续出现的最多次数;

参考代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
int n,a[maxn];
int main()
{
scanf("%d",&n);
int Max=-,ans=-,cnt=;
for(int i=;i<=n;++i) scanf("%d",a+i),Max=max(Max,a[i]);
for(int i=;i<=n;++i)
{
if(a[i]==Max) cnt++;
else ans=max(ans,cnt),cnt=;
}
ans=max(ans,cnt);
cout<<ans<<endl; return ;
}

B. Emotes

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The ii-th emote increases the opponent's happiness by aiai units (we all know that emotes in this game are used to make opponents happy).

You have time to use some emotes only mm 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 kk times in a row (otherwise the opponent will think that you're trolling him).

Note that two emotes ii and jj (i≠ji≠j) such that ai=ajai=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,mn,m and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤k≤m≤2⋅1091≤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 nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is value of the happiness of the ii-th emote.

Output

Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.

Examples
input

Copy
6 9 2
1 3 3 7 4 2
output

Copy
54
input

Copy
3 1000000000 1
1000000000 987654321 1000000000
output

Copy
1000000000000000000

In the first example you may use emotes in the following sequence: 4,4,5,4,4,5,4,4,54,4,5,4,4,5,4,4,5.

题解:

意思就是给你n个数,然后现在要从这n个数里面挑选m个,每个数可以多次挑选,但不能连续挑选超过k次,(不同位置的数大小相同也是不同的数)

这题就是找到最大和次大的那个数和最大的数出现的次数,然后判断  k==1? 等于一的话,就是最大的数*m, 不等同于1的话,如果最大的数出现的次数

大于1的话,答案也是最大的数*m,如果等于1的话,答案就是k个最大的数,1个次大的数,交替,就是ans=(m-m/(k+1))*Max1+m/(k+1)*Max2;

参考代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,k,val,ans;
int main()
{
cin>>n>>m>>k;
ll Max1=,Max2=,cnt=;
for(int i=;i<=n;++i)
{
cin>>val;
if(val>Max1) Max2=Max1,Max1=val,cnt=;
else if(val>Max2) Max2=val;
else if(val==Max1) cnt++;
}
if(k==)
{
if(cnt==) ans=(m-m/)*Max1+(m/)*Max2;
else if(cnt>) ans=m*Max1;
}
else
{
if(cnt>) ans=m*Max1;
else if(cnt==) ans=m/(k+)*Max2+(m-m/(k+))*Max1;
}
cout<<ans<<endl;
///////////////////////////////////
return ;
}

C. Magic Ship

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You a captain of a ship. Initially you are standing in a point (x1,y1)(x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2)(x2,y2).

You know the weather forecast — the string ss of length nn, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s1s1, the second day — s2s2, the nn-th day — snsn and (n+1)(n+1)-th day — s1s1 again and so on.

Ship coordinates change the following way:

  • if wind blows the direction U, then the ship moves from (x,y)(x,y) to (x,y+1)(x,y+1);
  • if wind blows the direction D, then the ship moves from (x,y)(x,y) to (x,y−1)(x,y−1);
  • if wind blows the direction L, then the ship moves from (x,y)(x,y) to (x−1,y)(x−1,y);
  • if wind blows the direction R, then the ship moves from (x,y)(x,y) to (x+1,y)(x+1,y).

The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction Uand the ship moves the direction L, then from point (x,y)(x,y) it will move to the point (x−1,y+1)(x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2)(x,y+2).

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

Input

The first line contains two integers x1,y1x1,y1 (0≤x1,y1≤1090≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers x2,y2x2,y2 (0≤x2,y2≤1090≤x2,y2≤109) — the coordinates of the destination point.

It is guaranteed that the initial coordinates and destination point coordinates are different.

The third line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the string ss.

The fourth line contains the string ss itself, consisting only of letters U, D, L and R.

Output

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

If it's impossible then print "-1".

Examples
input

Copy
0 0
4 6
3
UUU
output

Copy
5
input

Copy
0 3
0 0
3
UDD
output

Copy
3
input

Copy
0 0
0 1
1
L
output

Copy
-1
题解:
  题目意思就是一艘船要从起点(x1,y1)到达终点(x2,y2),每天的风向是不同的,给出了n天的风向信息(每n天具有周期性),且每天小船可以往一个方向移动一个单位或者不移动; 让你求到达终点的最短时间,如果不能到达就返回-; 这题由于答案具有单调性,因此可以二分答案m;对于风的速度和船的速度可以分开计算; 我们先计算出在m天风可以使小船移动到的位置,然后其和终点(x2,y2)的曼哈顿距离如果大于天数,则不能,小于等于天数则满足。一直二分答案,求出最小的天数;
参考代码:
 #include<bits/stdc++.h>
#define maxl 100010
using namespace std;
typedef long long ll;
#define clr(a,val) memset(a,val,sizeof(a))
#define fi first
#define se second
#define pb push_back
const int INF=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[maxl];
ll sx,sy,ex,ey,cx=,cy=;
ll tx[maxl],ty[maxl],sumx[maxl],sumy[maxl];
char ch[maxl];
ll ans;
bool judge(ll mid)
{
ll fx=mid/n*sumx[n]+sumx[mid%n];
ll fy=mid/n*sumy[n]+sumy[mid%n];
if(abs(ex-(sx+fx))+abs(ey-(sy+fy))<=mid) return ;
else return ;
}
void solve()
{
ll resx=ex-sx,resy=ey-sy,d=,res=abs(resx)+abs(resy);
for(int i=;i<=n;i++)
{
if(tx[i]*resx>) res--;
else res++;
if(ty[i]*resy>) res--;
else res++;
if(res<=i){ans=i;return;}
}
if(resx*cx<= && cx!=) d+=cx;
if(resy*cy<= && cy!=) d+=cy;
if(d>=n){ans=-;return;}
ll l=,r=1ll<<,mid;
while(l+<r)
{
mid=(l+r)>>;
if(judge(mid)) r=mid;
else l=mid;
}
if(judge(l)) ans=l;
else if(judge(r)) ans=r;
else ans=-;
}
int main()
{
sx=read();sy=read();ex=read();ey=read();
scanf("%d",&n);
scanf("%s",ch+);
for(int i=;i<=n;i++)
{
if(ch[i]=='U') tx[i]=,ty[i]=;
if(ch[i]=='D') tx[i]=,ty[i]=-;
if(ch[i]=='L') tx[i]=-,ty[i]=;
if(ch[i]=='R') tx[i]=,ty[i]=;
sumx[i]=sumx[i-]+tx[i];
sumy[i]=sumy[i-]+ty[i];
cx+=tx[i];cy+=ty[i];
}
solve();
cout<<ans<<endl;
return ;
}

CoderForces Round60-(1117A,1117B,1117C题解)的更多相关文章

  1. CoderForces Round526 (A~E)题解

    A. The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes input ...

  2. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  3. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  4. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  5. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  6. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  7. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  8. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  9. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

随机推荐

  1. pip的简单用法

    pip的用法: 其实跟linux的yum很像,它可以帮我们安装python所需要的环境包,并且可以包解决依赖关系 eg: 列出已安装的包 pip list 安装要安装的包 pip install xx ...

  2. element - ui tree

    一行代码两行泪,没有外网真可怕!好久没写博客了,更新一把. <template> <div> <el-tree :data="data" :props ...

  3. C# Web分页功能实现

    无论是网站还是APP分页功能都是必不可少的.为什么使用分页呢? 1,加载速度快,不会占用服务器太多资源,减少服务器压力. 2,减少数据库压力. 3,提升用户体验. 那么我们常用的分页方法有两种. 1, ...

  4. JVM浅谈

    **前言** 由于先前也遇到过一些性能问题,OOM算是其中的一大类了.因此也对jvm产生了一些兴趣.自己对jvm略做了些研究.后续继续补充. **从oom引申出去** 既然说到oom,首先需要知道oo ...

  5. django 中 css文件的调用

    Django: 配置css文件 晚上搞了好久的css文件的调用,发现,我根本文件位置都放错了. 接下来要更改settings.py 和 urls.py 的设定. Settings.py 中应该: ur ...

  6. PHP的global和$GLOBALS的区别

    global是关键字,通常添加在变量前,可以使变量的作用域为全局. $GLOBALS预定义的超全局变量,把变量扔到里面一样可以变成全局变量. $GLOBALS 是一个关联数组,每一个变量为一个元素,键 ...

  7. 用例图浅谈以及OOA再到情景分析的面向对象电梯的设计(慕课东北大学)面向对象设计思维模式

    上班初期还不太适应,平时学习进度也跟不上,节奏慢下来会有时间更新的了. Diagram  这边以学生课程报名系统为例    这就是一种简单的用例图 用例图可以给读者提供的信息非常丰富,但是缺点是都是概 ...

  8. ubuntu 16.04上源码编译dlib教程 | compile dlib on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/c6ead512/,欢迎阅读! compile dlib on ubuntu 16.04 Series Part 1: compil ...

  9. python进程与线程的操作

    进程操作: # project :Python_Script # -*- coding = UTF-8 -*- # Autohr :XingHeYang # File :processTest.py ...

  10. 微服务中的Kafka与Micronaut

    今天,我们将通过Apache Kafka主题构建一些彼此异步通信的微服务.我们使用Micronaut框架,它为与Kafka集成提供专门的库.让我们简要介绍一下示例系统的体系结构.我们有四个微型服务:订 ...