当前位置:网站首页>2022-07-18 至 2022-07-25 记录
2022-07-18 至 2022-07-25 记录
2022-07-20 03:36:00 【nth2000】
Zero Path(1700)
题目大意:给定n*m方阵,每个格点上有1或-1。每次只能向右移动一步或者向下移动一步。问是否存在从起始点(1,1)到(n,m)一条和为0的路径。
思路:
关键在于充分利用方阵上的-1,1,增量时仅会造成很小变化,可能在变化的途中达到目标值。(先找到某个范围)
从起点到终点走的格点数是固定的,都是n+m-1。因此n+m-1为奇数时,不可能达成目标。而n+m-1为偶数时,由于奇+奇=偶,奇-奇=偶,因此到达(n,m)点的所有路径和都为偶数。
使用dp容易求得路径和的最大值和最小值(必要条件)。如果[最小值,最大值]包含0,则最终答案包含0。这是因为可以移动每次上下水平向右移动的步骤使得路径和变为最大值。使用方阵上的-1,1的条件,每次移动改变路径和-2或2。于是一定某次移动后路径和变为0
反思:这种思想以前是遇到过的,见那个很妙的滑动窗口均值的题(juju and binary string 2800)。
另一种思路:运用bitset dp记录和。
#include <bits/stdc++.h>
using namespace std;
vector<bool> vis;
int main()
{
int t; cin>>t;
for(int i =0 ;i<t;i++)
{
int n,m;
scanf("%d %d",&n,&m);
int d[n][m];
for(int k = 0;k<n;k++) for(int q = 0;q<m;q++) scanf("%d",&d[k][q]);
if((n+m-1)%2==1) {
printf("NO\n");continue;}
int dpmin[n][m],dpmax[n][m];
dpmin[0][0]=dpmax[0][0]=d[0][0];
for(int i = 0;i<n;i++)
{
for(int j = 0;j<m;j++)
{
if(i==0&&j==0) continue;
int a = INT_MIN; //用于更新最大值
int b = INT_MAX; //用于更新最小值
if(i-1>=0) {
a = max(a,dpmax[i-1][j]); b = min(b,dpmin[i-1][j]);}
if(j-1>=0) {
a = max(a,dpmax[i][j-1]); b = min(b,dpmin[i][j-1]);}
dpmax[i][j] = a + d[i][j];
dpmin[i][j] = b + d[i][j];
}
}
if(dpmin[n-1][m-1]<=0 && dpmax[n-1][m-1]>=0) printf("YES\n"); else printf("NO\n");
}
system("pause");
}
边栏推荐
- 自定义MVC增查
- [special for training course] Introduction to storage API
- vscode setting
- 系统学习cv-pytorch进阶
- int类型转为double
- [upload range 12-16] cut, picture horse
- 技术干货 | 基于 MindSpore 实现图像分割之平均表面距离
- What parameters do you need to see when buying a server and how to see the server configuration
- DTOS帝拓思的3D引擎将取代游戏引擎巨兽,实现国产化替代
- 网络原理之协议详解
猜你喜欢
随机推荐
[pygame Learning notes] 8. Elfe.
【C语言进阶】---- 自定义类型详解
C language binary tree + queue to realize hierarchical traversal
(PC+WAP)织梦模板会计服务类网站
What do 1U, 2U and 4U of the server mean?
【已解决】org.apache.catalina.LifecycleException: 无法启动组件[StandardEngine[Catalina].StandardHost[localhost]
Redhat 7网络服务无法启动问题(“Device does not seem to be present, delaying initialization”)处理
[leetcode] sword finger offer 53 - I. find the number I in the sorted array
href和src、div和span的区别
如何安装scons低版本
简略break、continue、return的区别
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
技术干货 | 基于 MindSpore 实现 CosineSimilarity
kettle
PPT简明
DenseNet学习笔记(核心与resnet进行对比):
scala Breaks.break()、Breaks.breakable()、控制抽象
Mysql -分区列(横向分区 纵向分区)
DP背包问题
JTAG debugging command line debugging of arm bare board debugging