poj 1009(注意数组的大小)

复制代码
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm> 
using namespace std;
#define maxn 1005
int inPair[maxn][2],width,total,cup;
typedef struct outPair{
    int pos;
    int value;
}Pix;
Pix pix[maxn*100];
int getValue(int pos){
    int i = -1,j = 0;
    while(i<pos){
        i += inPair[j++][1];
    }
    return inPair[j-1][0];
}
int getCode(int pos){
    int i,j,row,col,tmp,code = 0;
    row = pos/width;
    col = pos%width;
    for(i=row-1;i<=row+1;i++){
        for(j=col-1;j<=col+1;j++){
            cup = i*width+j;
            if(i<0||j<0||j>=width||cup>=total)    //!!!!!!!!!
                continue;
            tmp = abs(getValue(pos)-getValue(cup));
            if(tmp>code)
                code = tmp;
        }
    }
    return code;
}
int cmp(const void*a,const void*b){
    Pix*x = (Pix*)a;
    Pix*y = (Pix*)b;
    return x->pos-y->pos;
}
int main(){
    int a,b,k,i,j,p,pos,row,col,count;
    while(scanf("%d",&width)==1&&width){
        k = 0;
        total = 0;
        memset(inPair,0,sizeof(inPair)); 
        while(scanf("%d%d",&a,&b)==2&&b){
            inPair[k][0] = a;
            inPair[k][1] = b;
            k++;
            total += b;
        }
        pos = 0;
        count = 0;
        for(p=0;p<k;p++){
            row = pos/width;
            col = pos%width;
            for(i=row-1;i<=row+1;i++){
                for(j=col-1;j<=col+1;j++){
                    cup = i*width+j;
                    if(i<0||cup>=total||j<0||j>=width)
                        continue;
                    pix[count].pos = cup;
                    pix[count++].value = getCode(cup);
                }
            }
            pos += inPair[p][1];
        }
        pos = 0;
        for(p=0;p<k;p++){
            row = pos/width;
            for(i=row-2;i<=row+2;i++){
                cup = i*width;
                if(i<0||cup>=total)
                    continue;
                pix[count].pos = cup;
                pix[count++].value = getCode(cup);
            }
            pos += inPair[p][1];
        }
        pos = 0;
        for(p=0;p<k;p++){
            row = pos/width;
            for(i=row-2;i<=row+2;i++){
                cup = (i+1)*width-1;
                if(i<0||cup>=total)
                    continue;
                pix[count].pos = cup;
                pix[count++].value = getCode(cup);
            }
            pos += inPair[p][1];
        }
        qsort(pix,count,sizeof(Pix),cmp);
//        for(i=0;i<count;i++){
//            printf("%d %d %d\n",i,pix[i].value,pix[i].pos);
//        }
        printf("%d\n",width);
        Pix tmp = pix[0];
        for(i=0;i<count;i++){
            if(tmp.value!=pix[i].value){
                printf("%d %d\n",tmp.value,pix[i].pos-tmp.pos);
                tmp = pix[i];
            }
        }
        printf("%d %d\n",tmp.value,total-tmp.pos);
        printf("0 0\n");
    }
    printf("0\n");
    return 0;
}
复制代码

 

posted @   智人心  阅读(42)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示