AGC016C +/- Rectangle

题意简述:给\(H , W , h, w\)。构造一个\(H*W\)的矩阵,满足矩阵元素之和为正数,且每个\(h*w\)的子矩阵元素之和是负数。

感觉是比较简单且比较巧妙的构造题,可惜自己还是太弱,没能做出来。orz wsq

首先考虑第一个样例给出的提示,我们可以在一般位置放1,在满足\(i\ \%\ h==0,j\ \%\ w==0\)的位置\((i,j)\)放\(-(w*h)\)。

然后我们考虑有这么一种情况,就是剩下部分的1无法补满之前矩阵贡献的-1,如:\(H=1,W=4,h=1,w=3\)

我们按照我们的思路就会出现:\(1,1,-3,1\),然后我们发现总和是小于0的,就会输出no,然而有如下一组可行解:\(2,2,-5,2\)。

所以我们考虑改进上述构造方法,我们可以在一般位置放k,在满足\(i\ \%\ h==0,j\ \%\ w==0\)的位置\((i,j)\)放\(-k*(w*h-1)-1\),那么我们只要使\(k\)尽量大,就可以尽可能地补足之前产生的\(-1\)了。

还有特判\(H%h==0,W%w==0\)时无解,因为这样只会构造出\(\frac{H*W}{h*w}\)个\(-1\)子矩阵,那么权值和就小于0了。

#include<cstdio>
#include<algorithm>
using namespace std;
int W,H,w,h,sum,ans[510][510],v;
int main(){
    scanf("%d%d%d%d",&H,&W,&h,&w);
    if(H%h==0&&W%w==0){puts("No");return 0;}
    v=500*500/(w*h-1);
    for(int i=1;i<=H;i++)
        for(int j=1;j<=W;j++){
            if(i%h==0&&j%w==0)ans[i][j]=-(w*h-1)*v-1;
            else ans[i][j]=v;
            sum+=ans[i][j];
        }
    if(sum>0){
        puts("Yes");
        for(int i=1;i<=H;i++,puts(""))
            for(int j=1;j<=W;j++)
                printf("%d ",ans[i][j]);
    }
    else puts("No");
}

原文地址:https://www.cnblogs.com/yxc2003/p/10730512.html

时间: 2024-10-11 11:06:57

AGC016C +/- Rectangle的相关文章

AGC016C +/- Rectangle(构造)

题目大意:给定H,W,h,w四个数,求是否满足矩阵的全部数之和和正数,h行w列之和为负数 如果h和w恰好是H,W的约数,则肯定不存在 否则肯定存在 只需要把h,w内每个元素填的足够大,然后小矩形的最后一个元素为负,且保持整个小矩形为负即可(可用不等式证明) #include <iostream> #include <cstring> #include <cstdio> using namespace std; long long Mat[505][505]; long

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

Maximal Rectangle

题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 方法 使用两个矩阵,分别记录每一行连续的1的个数以及每一列连续的1的个数. public int maximalRectangle(char[][] matrix) { int lenX = matrix.length; if (lenX == 0) { r

【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3]. The largest r

Tutorial: Generate BBox or Rectangle to locate the target obejct

1 clc;close all;clear all; 2 Img=imread('/home/wangxiao/Documents/files/Visual_Tracking/MDNet-CVPR2016/MDNet-master/attentionMap/Basketball/0001.png'); 3 if ndims(Img)==3 4 I=rgb2gray(Img); 5 else 6 I=Img; 7 end 8 I=im2bw(I,graythresh(I)); 9 [m,n]=si

CSU 1547 Rectangle(dp、01背包)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(th

84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

LeetCode 85. Maximal Rectangle

1 class Solution { 2 public: 3 int maximalRectangle(vector<vector<char>>& matrix) { 4 /** largest rectangle based solution **/ 5 if(matrix.size()<=0 || matrix[0].size()<=0) 6 return 0; 7 int m=matrix.size(); 8 int n=matrix[0].size()+