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或 ...
随机推荐
- day5-列表专区
list 列表.类li = [1, 12, 9, "age", ["88", ["19", 10], "方法"], &q ...
- 使用CXF发布webservice服务及注意要点
一.概念 1.什么是webservice Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用 ...
- python快速获取网页标准表格内容
from html_table_parser import HTMLTableParser def tableParse(value): p = HTMLTableParser() p.feed(va ...
- Swoole跟thinkphp5结合开发WebSocket在线聊天通讯系统
ThinkPHP使用Swoole需要安装 think-swoole Composer包,前提系统已经安装好了Swoole PECL 拓展* tp5的项目根目录下执行composer命令安装think- ...
- linux网络测试命令
一.ping 它通过向目标主机发送一个个数据包以及接受数据包的回应来判断主机和目标主机之间网络连接情况.ping的两个功能:判断网络是否可达.网络性能统计. ping使用的是网络层的ICMP协议. p ...
- WeTest明星工具-移动端性能测试PerfDog初探
在十一月初,腾讯就官宣了一则消息,腾讯WeTest明星工具-PerfDog面向全球发布.官宣介绍如下:https://wetest.qq.com/lab/view/475.html.我在看到该新闻时, ...
- (三十五)golang--面向对象之多态
多态:变量具有多种形态,可以用统一的接口来调用不同的实现. 接口体现多态特征: (1)多态参数:之前所讲的Usb接口案例,既可以接受手机变量,也可以接受相机变量,就体现了usb接口的多态: (2)多台 ...
- .NET高级特性-Emit(1)
在这个大数据/云计算/人工智能研发普及的时代,Python的崛起以及Javascript的前后端的侵略,程序员与企业似乎越来越青睐动态语言所带来的便捷性与高效性,即使静态语言在性能,错误检查等方面的优 ...
- java的回调和C#的委托
曾经有人对我说java的回调很巧妙. 今天我自己看了一下,回调的关键就是一个接口的事情. 也许是因为用了一定的手法,一开始不好懂吧,所以看懂了会感觉巧妙. 但是我心里的想法却是,真啰嗦! 回调的实例 ...
- 【Luogu P1439】最长公共子序列(LCS)
Luogu P1439 令f[i][j]表示a的前i个元素与b的前j个元素的最长公共子序列 可以得到状态转移方程: if (a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; d ...