问题 F: Low Range-Sum Matrix
时间限制: 1 Sec 内存限制: 128 MB
提交: 83 解决: 9
[提交] [状态] [命题人:admin]题目描述
You received a card at a banquet. On the card, a matrix of N rows and M columns and two integers K and S are written. All the elements in the matrix are integers, and an integer at the i-th row from the top and the j-th column from the left is denoted by Ai,j.
You can select up to K elements from the matrix and invert the sign of the elements. If you can make a matrix such that there is no vertical or horizontal contiguous subsequence whose sum is greater than S, you can exchange your card for a prize.
Your task is to determine if you can exchange a given card for a prize.
输入
The input consists of a single test case of the following form.
N M K S
A1,1 A1,2 ... A1,M
:
AN,1 AN,2 ... AN,M
The first line consists of four integers N,M,K and S (1≤N,M≤10,1≤K≤5,1≤S≤106). The following N lines represent the matrix in your card. The (i+1)-th line consists of M integers Ai,1,Ai,2,...,Ai,M(−105≤Ai,j≤105).
输出
If you can exchange your card for a prize, print 'Yes'. Otherwise, print 'No'.
样例输入
复制样例数据
3 3 2 10 5 3 7 2 6 1 3 4 1样例输出
Yes
提示
The sum of a horizontal contiguous subsequence from A1,1 to A1,3 is 15. The sum of a vertical contiguous subsequence from A1,2 to A3,2 is 13. If you flip the sign of A1,2, there is no vertical or horizontal contiguous subsequence whose sum is greater than S.
atcoder链接
来,我教你怎么写随机骗测评机(upc500ms,atcoder600ms)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
int A[15][15];
int Flag[10][2];
int n,m,k,s;
bool isok(){
for(int i=1;i<=n;i++){
ll sum = 0;
for(int j=1;j<=m;j++){
sum = (sum+A[i][j])>A[i][j]?(sum+A[i][j]):A[i][j];
if(sum>s)
return false;
}
}
for(int i=1;i<=m;i++){
ll sum = 0;
for(int j=1;j<=n;j++){
sum = (sum+A[j][i])>A[j][i]?(sum+A[j][i]):A[j][i];
if(sum>s)
return false;
}
}
return true;
}
int main(){
scanf("%d%d%d%d",&n,&m,&k,&s);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
scanf("%d",&A[i][j]);
if(isok())return 0*puts("Yes");
int t = 1e6;
while(t--){
for(int i=1;i<=k;i++){
bool vis[150] = {0};
for(int j=1;j<=i;j++){
int posx = rand()%n+1;// [1,n]
int posy = rand()%m+1;// [1,m]
while(vis[posx*m+posy]){
posx = rand()%n+1;
posy = rand()%m+1;
}
Flag[j][0] = posx;
Flag[j][1] = posy;
vis[posx*m+posy] = 1;
A[posx][posy] *= -1;
}
if(isok())
return 0*puts("Yes");
for(int j=1;j<=i;j++)A[Flag[j][0]][Flag[j][1]] *= -1;
}
}
puts("No");
return 0;
}
本文详细解析了一道涉及矩阵操作的编程竞赛题,探讨如何通过翻转矩阵元素的符号,确保任意连续子序列的和不超过预设阈值,旨在帮助读者理解并掌握此类问题的解题思路。

215

被折叠的 条评论
为什么被折叠?



