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或 ...
随机推荐
- Forsaken Mail创建临时邮箱系统| 手把手教程
场景需求 不需要长时间使用的邮箱 需要大量创建临时邮箱 使用匿名邮箱 环境说明 **` Forsaken Mail是一个临时邮箱系统,可以供任何人接受邮件,即收即毁,支持自定义邮箱地址前缀,这里就说下 ...
- Windows系统下搭建WAMP环境
Wamp就是Windos Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件.其中php环境配置是至关重要的一部分,本文就针对php在本地的 ...
- jdk8 函数式编程概念
yls 2019/11/7 函数式接口 如果一个接口只有一个抽象方法,那么该接口就是函数式接口 如果我们在某接口上声明了FunctionalInterface注解,那么编译器就会按照函数式接口的定义来 ...
- idea2017建立jsp工程及tomcat等配置
1:建立工程,选择Java Enterprise,可能需要选择tomcat路径和jdk路径: =============================== 以下作为第二种方式参考: 1:创建新工程 ...
- mysql5.7下载与安装
一.完全卸载旧版mysql 参考文章:https://blog.csdn.net/hui1setouxiang/article/details/89816176 二.win10中下载安装mysql5. ...
- libdispatch.dylib中dispatch_group的实现
semaphore和group都是libdispatch提供的基于信号量的同步机制,dispatch_group继承自dispatch_semaphore,使用libdispatch层的信号量算法.d ...
- JavaWeb01-常识
软件系统体系结构 1 常见软件系统体系结构B/S.C/S 1.1 C/S l C/S结构即客户端/服务器(Client/Server),例如QQ: l 需要编写服务器端程序,以及客户 ...
- ansible start canal
- name: Start canal server shell: source /etc/profile && nohup /opt/canal/bin/startup.sh
- SpringSecurity动态加载用户角色权限实现登录及鉴权
很多人觉得Spring Security实现登录验证很难,我最开始学习的时候也这样觉得.因为我好久都没看懂我该怎么样将自己写的用于接收用户名密码的Controller与Spring Security结 ...
- Java流程控制之(四)中断
目录 break continue return 标签 在程序设计时,循环直接的跳转显得十分重要,虽然Java没有提供goto语句去控制程序的跳转,但为了控制循环,Java提供了continue,br ...