CoderForces Round60-(1117A,1117B,1117C题解)
A. Best Subsegment
1 second
256 megabytes
standard input
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.
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.
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
5
6 1 6 6 0
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
1 second
256 megabytes
standard input
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.
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.
Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.
6 9 2
1 3 3 7 4 2
54
3 1000000000 1
1000000000 987654321 1000000000
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
2 seconds
256 megabytes
standard input
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).
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.
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".
0 0
4 6
3
UUU
5
0 3
0 0
3
UDD
3
0 0
0 1
1
L
-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题解)的更多相关文章
- CoderForces Round526 (A~E)题解
A. The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes input ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
随机推荐
- 微信小程序api封装(promise)
顺带这是我平时公司切换改变网络环境 直接上代码,我相信就可以懂了, //app.js function fetchApi(url, type, params, method) { return new ...
- spring boot集成shiro-redis时,分布式根据seesionId获取session报错排查总结
昨天在集成shiro-redis的时候,使用sessionId在其他微服务获取用户的session时,发生错误:There is no session with id [xxx]. 查遍了所有资料,基 ...
- 投票通过,PHP 8 确认引入 Union Types 2.0
关于是否要在 PHP 8 中引入 Union Types 的投票已于近日结束,投票结果显示有 61 名 PHP 开发组成员投了赞成票,5 名投了反对票. 还留意到鸟哥在投票中投了反对票~) 因此根据投 ...
- 🔥《手把手教你》系列练习篇之1-python+ selenium自动化测试(详细教程)
1.简介 相信各位小伙伴或者同学们通过前面已经介绍了的Python+Selenium基础篇,通过前面几篇文章的介绍和练习,Selenium+Python的webUI自动化测试算是 一只脚已经迈入这个门 ...
- Parallel Feature Pyramid Network for Object Detection
Parallel Feature Pyramid Network for Object Detection ECCV2018 总结: 文章借鉴了SPP的思想并通过MSCA(multi-scale co ...
- 2019-10-16:渗透测试,基础学习,burpsuit笔记
maccms10后门分析下载网址,是假官网http://www.maccmsv10.com/download.htmlMaccms10基于php+mysql的maccms,是苹果的内容管理,方便使用, ...
- Liunx 安装配置zsh和oh-my-zsh 替换 bash
一.前言 本文将基于 Liunx 环境安装配置zsh 和 oh-my-zsh 替换 bash oh my zsh Liunx默认shell是单调的bash,而zsh比较高大上,bash有的功能,zsh ...
- linux bash编程之算数运算和测试类型(第二篇)
写在最前边:在bash中数据类型有两种,分别是数值型和字符型.其中字符型是默认的. 1.算数运算 · 运算符 · 语法 1.1.运算符:+.-.*./.%.** 注意:有些时候 *(乘号)需要转义 1 ...
- 公众号在线Markdown编辑器,支持公式
公众号排版不支持Markdown,用自带的富文本编辑器排版出来的格式十分丑陋,尤其是公式,竟然连"Mathjax"都不支持,但好在支持"带格式复制",也即可以将 ...
- Linux I/O复用 —— epoll 部分源码剖析
epoll 相关的系统调用有以下三个,这里简述下当调用对应函数后,内核的具体实现 epoll_creat( ) 在内核注册文件系统 eventpollfs,挂载此文件系统 (linux一切皆文件,便于 ...