Educational Codeforces Round 60 (Rated for Div. 2)

题目链接https://codeforces.com/contest/1117

A. Best Subsegment

题意:

给出n个数,选取一段区间[l,r],满足(al+...+ar)/(r-l+1)最大,这里l<=r,并且满足区间长度尽可能大。

题解:

因为l可以等于r,所以我们可以直接考虑最大值,因为题目要求,直接求连续的最大值的长度就是了。

代码如下:

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

B. Emotes

题意:

输入n,m,k,n表示元素个数,每个元素都有其权值;m表示最多可以选取的个数;k表示同一个元素最多被连续选取多少次。

这里每种元素都有无限多个,问怎样选可以使得最终获得权值和最大。

题解:

这个直接贪心就好了。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
ll n,m,k;
ll a[N];
int main(){
cin>>n>>m>>k;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
}
sort(a+,a+n+);
reverse(a+,a+n+);
ll fir = a[],sec = a[];
ll ans=;
if(fir==sec){
cout<<m*fir;
}else{
ans = k*fir+sec;
ll tmp = m/(k+);
ans*=tmp;
tmp*=(k+);
ans+=(m-tmp)*fir;
cout<<ans;
}
return ;
}

C. Magic Ship

题意:

在二维坐标轴上给出起点和终点的坐标,然后会给n天的天气预报,表示风向,不同的风向对应那一天会多往哪个方向走。天气情况是循环来的 ,循环节为n。

现在问最少要多少天,可以让船从起点走到终点。如果无论如何走不到终点,输出-1。

题解:

这个题我一开始考虑复杂了。其实这里风向带来的位置变化,和船开动的位置变化,可以分开来,也就是说,如果考虑n天的位置变化,可以先看风向给船带来的影响(船会被吹到哪去),再考虑船自身的航行方向。如果想清楚了这一点,那么直接二分天数就好了,最后通过曼哈顿距离来判断可行性。

这里单调性的证明也有点意思,很显然地,时间越短越不可能到终点;另一个方面,假设船在x时间可以到终点,那么时间越长,也更有可能到终点,因为船可以借助风向,在一个位置保持不变。

细节见代码吧(注意二分天数):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+;
struct Point{
ll x,y;
}st,ed,cur;
char s[N];
int n;
ll prex[N],prey[N];
int check(ll x){
ll d = x/n;
cur.x=st.x+d*prex[n];
cur.y=st.y+d*prey[n];
cur.x=cur.x+prex[x%n];
cur.y=cur.y+prey[x%n];
ll dis=abs(cur.x-ed.x)+abs(cur.y-ed.y);
return dis<=x;
}
int main(){
cin>>st.x>>st.y>>ed.x>>ed.y;
cin>>n;
scanf("%s",s+);
for(int i=;i<=n;i++){
prex[i]=prex[i-];prey[i]=prey[i-];
prex[i]+=(s[i]=='R');
prex[i]-=(s[i]=='L');
prey[i]+=(s[i]=='U');
prey[i]-=(s[i]=='D');
}
ll l=,r=1e15,mid;
while(l<r){
mid=l+r>>;
if(check(mid)) r=mid;
else l=mid+;
}
if(l==1e15) cout<<-;
else cout<<l;
return ;
}

D. Magic Gems

题意:

给出n和m,然后有n个可分解物品,连续的m个可分解物品可以被分解成m个不可分解物品。现在问一共有多少种分解方式,可以让最后都有n个物品(包含可分解与不可分解)。

题解:

这题可以考虑组合数来求解,枚举分解i组物品,那么答案就是C(n-i*(m-1),i),但是这个题行不通,枚举i就爆掉了。

通过考虑第i个数的状态,可以考虑递推:设fi为前i个物品的分解总数,那么fi=fi-1+fi-m,分别对应第i个物品不参与分解以及参与分解。

结合题目数据范围,要用矩阵乘法来加速,具体矩阵构造什么的看代码吧:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ,MOD = 1e9+;
struct matrix{
ll A[N][N];
int n,m;
matrix(){
memset(A,,sizeof(A));
}
void Print(){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}
};
matrix operator * (const matrix &a,const matrix &b){
matrix ans;
ans.n=a.n;ans.m=b.m;
for(int i=;i<=ans.n;i++){
for(int j=;j<=ans.m;j++){
for(int k=;k<=b.n;k++){
ans.A[i][j]=(ans.A[i][j]+a.A[i][k]*b.A[k][j]%MOD)%MOD;
}
}
}
return ans ;
}
matrix operator + (const matrix &a,const matrix &b){
matrix ans;
ans.n=a.n;ans.m=a.m;
for(int i=;i<=ans.n;i++){
for(int j=;j<=ans.m;j++){
ans.A[i][j]=(a.A[i][j]+b.A[i][j])%MOD;
}
}
return ans ;
}
matrix qp_Mat(matrix a,ll b){
matrix ans;
ans.n=ans.m=a.n;
for(int i=;i<=ans.n;i++) ans.A[i][i]=;
while(b){
if(b&) ans=ans*a;
a=a*a;
b>>=;
}
return ans ;
}
int main(){
ll n,m;
cin>>n>>m;
matrix trans;
trans.n=m;trans.m=m;
trans.A[][]=trans.A[][m]=;
for(int i=;i<=m;i++) trans.A[i][i-]=;
matrix ans;
ans.n=m;ans.m=m;
for(int i=;i<=m;i++) ans.A[i][]=;
if(n<m){
cout<<;
}else{
matrix Ans = qp_Mat(trans,n-m+);
Ans=Ans*ans;
cout<<Ans.A[][];
}
return ;
}

E. Decypher the String

题意:

每组数据会有n个交换操作,但是这是个交互题不会告诉你。他只会告诉你f(长度为n的串),表示将串进行n次交换操作过后得到的串。

然后你只有三次询问机会,最后输出原串是什么。

题解:

这是一个很有意思的交互题。n最大只有10000,通过观察26^2<n<26^3,那么可以往26这方面考虑一下。

注意到当n<=26时,我们只需要一个所有26个字母的顺序排列,就很容易知道位置的变化情况。

然后构造这样的字符串:aaa...aaa(26*26)bbb....,并且称26*26为一个大段,那么类比上面的情况,通过询问,很容易知道目前第i个位置在原来哪个大段。

然后构造:aaa..aa(26)bbb...bb...zz....,上面每个大段中有26*26个数,也就是我们将每个数的范围限定在了某个长度为26*26的区间中,现在我们构造的字符串,可以进一步将第i个数范围缩小到长度为26的区间。

最后再构造ab..zabc...这样的串,就可以找到第i个数原来的位置在哪里了。

这三个操作的本质其实就是求a*262+b*261+c中,每个位置i对应的a,b,c值,这里的a,b,c都是不超过25的。

是不是感觉十分巧妙...将问题转化为26进制的问题,据说还可以通过crt来搞,但目前我的姿势水平还不够呜呜。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n;
char str[N],s[N],ans[N];
ll f[N];
int main(){
scanf("%s",str);
int len=strlen(str);
for(int i=;i<;i++){
for(int j=;j<len;j++){
if(i==) s[j]='a'+j%;
if(i==) s[j]='a'+j/%;
if(i==) s[j]='a'+j//%;
}
printf("? %s\n",s);
fflush(stdout);
char tmp[N];
scanf("%s",tmp);
for(int j=;j<len;j++){
if(i==) f[j]+=tmp[j]-'a';
if(i==) f[j]+=(tmp[j]-'a')*;
if(i==) f[j]+=(tmp[j]-'a')**;
}
}
char ans[N];
for(int i=;i<len;i++){
ans[f[i]]=str[i];
}
printf("! ");
printf("%s",ans);
return ;
}

Educational Codeforces Round 60 (Rated for Div. 2) 题解的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  4. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  5. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  7. Educational Codeforces Round 60 (Rated for Div. 2)

    A. Best Subsegment 题意 找 连续区间的平均值  满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...

  8. Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)

    #include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long ...

  9. Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)

    #include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size() ...

随机推荐

  1. linux学习总结----redis总结

    安装 下载:打开redis官方网站,推荐下载稳定版本(stable) 解压 tar zxvf redis-3.2.5.tar.gz 复制:推荐放到usr/local目录下 sudo mv -r ...

  2. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  3. 深度学习笔记 (一) 卷积神经网络基础 (Foundation of Convolutional Neural Networks)

    一.卷积 卷积神经网络(Convolutional Neural Networks)是一种在空间上共享参数的神经网络.使用数层卷积,而不是数层的矩阵相乘.在图像的处理过程中,每一张图片都可以看成一张“ ...

  4. HADOOP (十一).安装hbase

    下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...

  5. 计算器软件实现系列(六)windowform窗体+SQL+策略模式

    一 整体概述 这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是txt文件保存,现在更正用SQL数据库,现在更改了以前的文件保存形式,是三层架构中数据层的更换, ...

  6. iOS开发NS_ENUM和NS_OPTIONS区别

    OC里枚举的两种类型.NS_ENUM和NS_OPTIONS本质上是一样的都是枚举. typedef NS_ENUM(NSInteger, UIViewAnimationTransition) { UI ...

  7. TCP系列31—窗口管理&流控—5、TCP流控与滑窗

    一.TCP流控 之前我们介绍过TCP是基于窗口的流量控制,在TCP的发送端会维持一个发送窗口,我们假设发送窗口的大小为N比特,网络环回时延为RTT,那么在网络状况良好没有发生拥塞的情况下,发送端每个R ...

  8. <Android>资源的访问,颜色、字符串、尺寸、XML、DRAWABLES资源分使用

    1.资源的访问 代码中使用Context的getResources()方法得到Resources对象,访问自己定义的资源R.资源文件类型.资源文件名称,访问系统定义的资源android.R. 资源文件 ...

  9. 软工网络15团队作业4——Alpha阶段敏捷冲刺-4

    一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 完成程序副界面的设计与信息的输入统计 明天计划完成的工作: 日期等细致信息的处理 工作中遇到的困难: 对微信小程序开发的代码构成有了一些了解 ...

  10. ES6之 =>箭头函数

    原文,请点此链接http://www.cnblogs.com/allenxieyusheng/p/5784728.html 1. 第一个函数 ()=>1 解析:其实这是一个匿名函数直接执行 (f ...