题解 CF950B 【Intercepted Message】
2024-08-28 18:24:58
先吐槽一番:本宝宝好久没写过题解了。。。
首先我们想一个贪心策咯。
就是我们预处理出前缀和,然后一边扫过去,记录一个l1,l2和一个n1,n2。
分别表示我们现在第一个数组切到l1,上一次切是在n1处。l2,n2是表示第二个数组。
如果$ans1[l1]-ans1[n1]$ $=$ $ans2[l2]-ans2[n2]$ 就切一刀。(具体见代码)。
否则,如果$ans1[l1]-ans1[n1]$ $>$ $ans2[l2]-ans2[n2]$ 就把l2++。反之,l1++
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int m,n;//长度
int x[];
int y[];//储存序列
int anx[];//前缀和
int any[];
int l1=,n1;//如上所说,
int l2=,n2;//初始化,l1,l2均为一。n1,n2因为没切过,所以均为0;
int ans;//记录答案
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&x[i]);
anx[i]=anx[i-]+x[i];
}
for(int i=;i<=m;i++)
{
scanf("%d",&y[i]);
any[i]=any[i-]+y[i];
}//输入并处理前缀和。
while(l1<=n&&l2<=m)//判断有没有切完整个序列。
{
// printf("%d %d %d %d %d %d\n",l1,n1,l2,n2,(anx[l1]-anx[n1]),(any[l2]-any[n2]));
if((anx[l1]-anx[n1])>(any[l2]-any[n2])) l2++;
else if((anx[l1]-anx[n1])<(any[l2]-any[n2])) l1++;//如上所说。
else if((anx[l1]-anx[n1])==(any[l2]-any[n2]))
{
n1=l1;n2=l2;//第一个序列从l1切,第二个从l2切,更新n1,n2;
ans++;//答案+1;
l1++;//然后继续向后扫。
l2++;
}
}
printf("%d",ans);//输出答案
return ;//程序拜拜~
}
//样例在此
/*
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8 3 3 3
1 10 100
1 100 10 2 1 4
4
1 1 1 1 1
*/
如果对大家有帮助的话,希望大家留言支持呦~
最新文章
- css实现文本框和下拉框结合的案例
- 2、Runtime Area Data
- windows自定义命令的创建
- iOS UIAlertController
- 使用max() 函数
- google maps js v3 api教程(1) -- 创建一个地图
- 【原创】30分钟入门 github
- MVC5+EF6+BootStrap3.3.5 博客系统之EF(一)
- [转]如何理解c和c ++的复杂类型声明
- ThinkPHP 3.1.2 视图-1
- js面试题知识点全解(一作用域和闭包)
- MicroPython教程之TPYBoard开发板DIY小型家庭气象站
- nyoj1246 逃离妖洞 BFS
- profile.go
- [poj P2976] Dropping tests
- 使用jsp实现文件上传的功能
- MySQL优化Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available
- CF 277.5 B.BerSU Ball 二分图的最大匹配 模版题
- C#三大特性之 封装、继承、多态
- python新建一个表格xls并写入数据
热门文章
- python&#39;s seventeenth day for me 面向对象
- oracle——存储过程参数
- For input String:";"; 异常记录
- Spring Bean定义的三种方式
- PHP网站
- springboot @Value 类中读取配置文件 .properties null 原因和解决方案
- MySQL5.7插入中文乱码
- SQLAlchemy(ORM框架)
- mac安装nose,command not found:nosetests
- css总结13:CSS 伪类(Pseudo-classes)