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 和官方题 ...
随机推荐
- 编译升级至openssh7.6
1.概述 目的:下载源码包(https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-7.6p1.tar.gz),编译升级为openssh为7.6 ...
- laravel4.2 union联合,join关联分组查询最新记录时,查询条件不对,解决方案
需求: 分组联合查询,或者最新记录. 问题: mysql分组的时候默认会查询第一条记录,存在gourp by时 order by 无效. 一般解决办法就是 ,select * from ( sele ...
- day 26 网络知识 01
一. c/s 架构: 客户端(client)/服务端(server)架构 服务端: 提供服务的 客户端: 享受服务的 B/S 架构:浏览器(browser)/服务端 ...
- Hadoop2学习路程-HDFS
什么是Hadoop HDFS? Hadoop 分布式文件系统是世界上最可靠的文件系统.HDFS可以再大量硬件组成的集群中存储大文件. 它的设计原则是趋向于存储少量的大文件,而不是存储大量的小文件. 即 ...
- transform Vs Udf
在鞋厂的第一个任务,拆表.需要把订单表按照开始日期和结束日期拆分成多条记录,挺新鲜的~ transform方式,使用到了python. (1)把hive表的数据传入,通过python按照日期循环处理, ...
- php html 静态化 缓存
<?php // // ob_start(); $cache_name = md5(__FILE__). '.html'; $cache_lifetime = 3600; // echo fil ...
- Leecode刷题之旅-C语言/python-203移除链表元素
/* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...
- Java中的File.separator用法
在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...
- 我和Python的Py交易》》》》》》 命名空间的小弟作用域
命名空间的小弟作用域 在这要明确一个观点,在Python中万物皆对象,而变量指向的就是对象. 变量可以是 类名,函数名,储存数据的变量…… 对象可以是 类 ,被封装的一段代码(函数),数据…… 命名 ...
- Android零碎知识点
1.android:foreground="?attr/selectableItemBackground" ###设置水波纹效果 2.android:contentDescri ...