算法入门【[NOIP2011]铺地毯】

[NOIP2011]铺地毯

https://ac.nowcoder.com/acm/contest/20960/1016

Thinking process

I use a two dimension array to save the code of the highest carpet.
code[x][y] represents the (x, y) point's code of the highest carpet. So the solution comes to me. What i need to do is solving the problem on how to judge the code of the highest carpet. when given (x, y), length on x and height on y, I am sure that each points on (x -> x+length, y + height) is updated their code[each_x][each_y]. so let's do it.

#include<stdio.h>

int code[10][10];

int main() {
    for(int i = 1;i < 10; ++ i) 
        for(int j = 1;j < 10; ++ j)
            code[i][j] = -1;
    
    int n;
    scanf("%d", &n);
    
    for(int i = 1; i <=n ; i ++) {
        int a, b, g, k;
        scanf("%d%d%d%d", &a, &b, &g, &k);
        for(int x = a; x <= a + g; x ++) {
            for(int y = b;y <= b + k; ++ y) {
                code[x][y] = i;
            }
        }
    }
    
    int x,y;
    scanf("%d%d", &x, &y);
    printf("%d", code[x][y]);
    
}

although it can pass the samples, it doesn't work when it comes to the big chess. so quit. have another algorithm to solve the problem.
Please read the description about the title again. I found that it concerns about the only one pair of (x, y). So it's enough to maintain (x, y) poins. When given the (a, b) and g,k respectively on x and y, i can judge whether (x, y) is contained in (a, b) to (a + g, b + k). each a,b,k,g arrays are created by 100,010 of one dimension.It perfectly solves the space overflow. To some extend, I thought it works. so let's code!

#include<stdio.h>

const int N = 100010;
int a[N], b[N], k[N], g[N];

int main() {
    
    int n;
    scanf("%d", &n);
    
    for(int i = 1; i <=n ; i ++) {
        scanf("%d%d%d%d", &a[i], &b[i], &g[i], &k[i]);
    }
    int x,y;
    scanf("%d%d", &x, &y);
    
    int code = -1;
    for(int i = 1;i <= n;i ++) {
        int righttop_x = a[i] + g[i], righttop_y = b[i] + k[i];
        if(righttop_x >= x && a[i] <= x && righttop_y >= y && b[i] <= y)
            code = i;
    }
    printf("%d", code);
}

ok it's good. we also can think about the situation when i reserve the order to check the code of the highest carpet. what will happen? If the carpet covered on the (x, y) point is back-end in the array, program runs fast. If not, runs slowly.

全部评论

相关推荐

12-06 01:10
已编辑
哈尔滨工程大学 Java
一面问的真细,二面不知为啥变双机位。9.29快手主站平时怎么学习&nbsp;AI&nbsp;的,国内外知名大模型,实习公司都用的什么大模型,怎么评估效果的java池化思想,线程池构造方法的核心参数,线程池中阻塞队列注意事项,submit方法参数和执行逻辑,shutdown和shutdownnow,核心线程允许过期吗threadlocal底层,为什么key是弱引用,key回收了再get或者set这个value会怎样aqs,如何保证公平性java代理java堆划分,新生代还有别的晋升老年代的情况吗,什么时候触发gc,gc失败抛什么异常,如何排查oom,导出dump命令redis数据结构,哪个底层是跳表,和其他数据结构对比布隆过滤器会出现大key问题吗,你咋实现的布隆过滤器你怎么实现redis分布式锁,可重入,续期聚簇索引非聚簇索引select语句会加锁吗,怎么实现的不加锁undolog&nbsp;redolog&nbsp;binlog怎么能让select加锁,update这个范围加的什么锁,update一条呢手撕简单01背包,接雨水10.10快手主站意图识别用的哪个大模型,走到意图和rag的比例,faq是点击的吗自然语言怎么识别的gap一年干啥了,转正怎么样没跟组里提意向吗,研究生研究方向是传统算法吗,会大模型微调吗注册场景为什么用布隆过滤器,原理分布式锁底层的key怎么拼的,value里是什么redis持久化zset底层mysql索引结构,一个表三个字段有主键唯一索引和没索引的字段会有几个b+树,聚簇索引非聚簇索引存的啥无手撕
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务