Lecture Sleep(尺取+前缀和)
Description
Input
Output
只打印一个整数 - 只用一次技巧来唤醒Mishka,他可以记下的定理的最大数量。
Sample Input
Input
6 3
1 3 5 2 5 4
1 1 0 1 0 0
Output
16
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,k,sum=,tmp=,ans=-,x,j,i;
int a[];
int vis[];
scanf("%d%d",&n,&k);
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=;i<n;i++)
{
scanf("%d",&vis[i]);
if(vis[i])///只要是醒着的时候都能记住定理
{
sum=sum+a[i];
}
}
for(i=;i<k;i++)
{
if(!vis[i])
{
tmp=tmp+a[i];///tmp储存的是使用某种方法之后能记住的定理的增量
}
}
i=;
j=i+k-;///以使用某种方法的作用时间作为尺取的尺度
while()
{
ans=max(ans,sum+tmp);
if(!vis[i])
{
tmp=tmp-a[i];
}
i++;
j++;
if(j==n)///尺取到头就要终止遍历
{
break;
}
if(!vis[j])
{
tmp=tmp+a[j];
}
}
printf("%d\n",ans);
return ;
}
前缀和的方法
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[];
int vis[];
int sum[];
int per[];
int main()
{
int n,k,i,j,maxt;
int x,y,z;
scanf("%d%d",&n,&k);
for(i=; i<=n; i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];///sum储存的是前i项老师讲的定理和
}
for(i=; i<=n; i++)
{
scanf("%d",&vis[i]);
if(vis[i])
{
per[i]=per[i-]+a[i];///per储存的是前i项mishka听到的定理和
}
else
{
per[i]=per[i-];
}
}
maxt=;
for(i=; i<=n-k+; i++)
{
x=per[i-];///之前听到的定理
y=sum[i+k-]-sum[i-];///中间老师讲到的定理,在这里可以使用某一种方法,使得可以听到老师所讲的内容
z=per[n]-per[i+k-];///后面听到的定理
maxt=max(maxt,x+y+z);
}
printf("%d\n",maxt);
return ;
}
看到一个大神有更为优化的前缀和的方法
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[];
int main()
{
int n,k,i,j,maxt,ans,b;
int x,y,z;
scanf("%d%d",&n,&k);
ans=;
maxt=;
for(i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(i=; i<=n; i++)
{
scanf("%d",&b);
if(b)
{
ans=ans+a[i];///ans里面储存的是上课能听到的所有定理
a[i]=;///听到了的重置为0
}
a[i]=a[i-]+a[i];///a[i]///里面储存的是前i项上课没有听到的
}
for(i=k; i<=n; i++)
{
maxt=max(maxt,a[i]-a[i-k]);
}
printf("%d\n",maxt+ans);
return ;
}
Lecture Sleep(尺取+前缀和)的更多相关文章
- Intense Heat(前缀和或尺取)
The heat during the last few days has been really intense. Scientists from all over the Berland stud ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...
- POJ:2566-Bound Found(尺取变形好题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...
- 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取
题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...
- POJ-3061 Subsequence 二分或尺取
题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...
- Gym 101257G:24(尺取)
http://codeforces.com/gym/101257/problem/GGym 101257G 题意:给出n个人,和一个数s,接下来给出每个人当前的分数和输掉的概率.当一个人输了之后就会掉 ...
- Codeforces - 1191E - Tokitsukaze and Duel - 博弈论 - 尺取
https://codeforc.es/contest/1191/problem/E 参考自:http://www.mamicode.com/info-detail-2726030.html 和官方题 ...
随机推荐
- 500. Keyboard Row (5月26日)
解答 class Solution { public: vector<string> findWords(vector<string>& words) { vector ...
- windows下labelme的安装
1.安装Anaconda 2.进入Anaconda文件夹下 3.输入conda create --name=labelme python=3.5 4.输入activate labelme 然后建立的l ...
- exsi6.0远程修改密码
-------------------------------esxi远程修改root密码--------------------------- 在不接触物理机时,通过启动ssh服务,远程修改密码,具 ...
- $.extend() 合并问题
- 第6章 HDFS HA配置
目录 6.1 hdfs-site.xml文件配置 6.2 core-site.xml文件配置 6.3 启动与测试 6.4 结合ZooKeeper进行自动故障转移 在Hadoop 2.0.0之前,一个H ...
- golang 正则表达式 匹配局域网
做一个微服务,需要对http头域里的remoteip做访问限制:所有局域网都要鉴权,其中一些特殊ip,如网关地址,直接拒绝,防止公网访问.正则表达式很好的解决了这个,直接贴代码,读者拿来直接改改就能用 ...
- Grep/find查找文件
1. 查找secret 函数所在的文件位置grep -rn secret * grep -rn "secret" * 2. find 查找当前目录下,比while2 时间新并且名字 ...
- 03以太网帧结构(链路层 IEEE802.3)
OSI七层模型:从底往上记(研究细致时用) 物理层:单位bit,字节byte,同轴电缆,光纤,二进制,比特流 数据链路层:帧,16进制,0-9,A-FMac地址->全网唯一性 mac地址 ...
- Chip-seq peak annontation
Chip-seq peak annontation Chip-seq peak annontation PeRl narrowPeak/boardPeak narrowPeak/boardPeak 是 ...
- 使用cgroups来控制磁盘IO带宽
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackga ...