Poj 1974 The Happy Worm(sort结构体二级排序)

来源:互联网 发布:js实现倒计时时分秒 编辑:程序博客网 时间:2024/05/13 04:20

原题地址

这个A了一天,也没有搞定,查不出自己代码的不过在哪,题意,思路都知道

我的代码

sort结构体的二级排序,记下,备用。。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef struct array{int x;int y;}arr;bool cmpx(arr a, arr b) {    if(a.x == b.x) return a.y < b.y;    else return a.x < b.x;}bool cmpy(arr a, arr b) {    if(a.y == b.y) return a.x < b.x;    else return a.y < b.y;}int main(){int t;scanf("%d",&t);while(t--){arr a[140000] = {0};memset(&a,0,sizeof(arr));int m,n,k;int i,j,sum = 0,bleg = 0;scanf("%d %d %d",&m,&n,&k);for(i=0;i<k;i++){scanf("%d %d",&a[i].x,&a[i].y);}sort(a,a+k,cmpx);/*for(i=0;i<k;i++){printf("%d %d\n",a[i].x,a[i].y);}printf("\n");*/for(i=1;i<=n;i++){bleg = 0;for(j=0;j<k;j++)if(i == a[j].y) { bleg = 1; break;} if(bleg == 0 && m>1) sum++;}//printf("0// %d\n",sum);for(i=1;i<=m;i++){bleg = 0;for(j=0;j<k;j++)if(i == a[j].x) {bleg = 1;break;} if(bleg == 0 && n>1) sum++;}//printf("1// %d\n",sum);for(i=0;i<k;i++){if(i == 0){if(a[i].y - 1 >= 2) sum++;}if(a[i].x != a[i-1].x && i>0){if(a[i].y - 1 >= 2) sum++;}if(a[i].x != a[i+1].x){if(m - a[i].y >= 2) sum++;}else{if(a[i+1].x - a[i].x > 2) sum++;}}//printf("hang %d\n",sum);sort(a,a+k,cmpy);/*for(i=0;i<k;i++){printf("%d %d\n",a[i].x,a[i].y);}*/for(i=0;i<k;i++){if(i == 0){if(a[i].x - 1 >= 2) sum++;}if(a[i].y != a[i-1].y && i>0){if(a[i].x - 1 >= 2) sum++;}if(a[i].y != a[i+1].y){if(m - a[i].x >= 2) sum++;}else if(a[i].y == a[i+1].y){if(a[i+1].x- a[i].x > 2) sum++;}//printf("sum %d = %d\n",i,sum);}        printf("%d\n",sum);}return 0;}

别人正确的代码(朋友的)

#include <stdio.h>#include <algorithm>#include <iostream>using namespace std;typedef struct name{int x;int y;}name;name str[140000];int cmp(name a,name b){if(a.x!=b.x)return a.x<b.x;elsereturn a.y<b.y;}int comp(name a,name b){if(a.y!=b.y)return a.y<b.y;elsereturn a.x<b.x;}int main(){int t,n,m;int sum;int k;int count;int i,j;int flat=0;int flat1=0;scanf("%d",&t);for(i=0;i<t;i++){sum=0;count=1;flat=0;flat1=0;scanf("%d %d %d",&n,&m,&k);for(j=0;j<k;j++){str[j] .x=0;str[j] .y=0;scanf("%d %d",&str[j] .x,&str[j] .y);}sort(str,str+k,cmp);str[k] .x=0;str[k] .y=0;for(j=0;j<k;j++){if(str[j] .x!=str[j+1].x){if(count==1){if((str[j] .y-1)>=2)sum++;if((m-str[j] .y)>=2)sum++;count=1;}else if(count>=2){if((str[j-count+1].y-1)>=2)sum++;if((m-str[j] .y)>=2)sum++;for(int q=(j-count+1);q<j;q++){if((str[q+1].y-str[q] .y-1)>=2)sum++;}count=1;}if(m>=2)flat++;elseflat=n;}else if(str[j] .x==str[j+1].x){count++;continue;}}sort(str,str+k,comp);for(j=0;j<k;j++){if(str[j] .y!=str[j+1].y){if(count==1){if((str[j] .x-1)>=2)sum++;if((n-str[j] .x)>=2)sum++;count=1;}else if(count>=2){if((str[j-count+1].x-1)>=2)sum++;if((n-str[j] .x)>=2)sum++;for(int q=(j-count+1);q<j;q++){if(((str[q+1].x-str[q] .x)-1)>=2)sum++;}count=1;}if(n>=2)flat1++;elseflat1=m;}else if(str[j] .y==str[j+1].y){count++;continue;}}if(k!=0)printf("%d\n",(sum+(n-flat)+(m-flat1)));else{if(n>=2&&m>=2)printf("%d\n",n+m);else if(n>=2&&m<2)printf("%d\n",m);else if(n<2&&m>=2)printf("%d\n",n);elseprintf("0\n");}}return 0;} 

百度的

#include <stdio.h>#include <algorithm>#include <iostream>using namespace std;typedef struct name{int x;int y;} name;name str[140000];int cmp(name a, name b){if (a.x != b.x){return a.x < b.x;}else{return a.y < b.y;}}int comp(name a, name b){if (a.y != b.y){return a.y < b.y;}else{return a.x < b.x;}}int main(){int t, n, m;int sum;int k;int count;int i, j;int flat = 0;int flat1 = 0;scanf("%d", &t);for (i = 0; i < t; i++){sum = 0;count = 1;flat = 0;flat1 = 0;scanf("%d %d %d", &n, &m, &k);for (j = 0; j < k; j++){scanf("%d %d", &str[j] .x, &str[j] .y);}sort(str, str + k, cmp);str[k] .x = 0;str[k] .y = 0;for (j = 0; j < k; j++){if (str[j] .x != str[j + 1].x){if (count == 1){if ((str[j] .y - 1) >= 2){sum++;}if ((m - str[j] .y) >= 2){sum++;}count = 1;}elseif (count >= 2){if ((str[j - count + 1].y - 1) >= 2){sum++;}if ((m - str[j] .y) >= 2){sum++;}for (int q = (j - count + 1); q < j; q++){if ((str[q + 1].y - str[q] .y - 1) >= 2){sum++;}}count = 1;}if (m >= 2){flat++;}else{flat = n;}}elseif (str[j] .x == str[j + 1].x){count++;continue;}}sort(str, str + k, comp);for (j = 0; j < k; j++){if (str[j] .y != str[j + 1].y){if (count == 1){if ((str[j] .x - 1) >= 2){sum++;}if ((n - str[j] .x) >= 2){sum++;}count = 1;}elseif (count >= 2){if ((str[j - count + 1].x - 1) >= 2){sum++;}if ((n - str[j] .x) >= 2){sum++;}for (int q = (j - count + 1); q < j; q++){if (((str[q + 1].x - str[q] .x) - 1) >= 2){sum++;}}count = 1;}if (n >= 2){flat1++;}else{flat1 = m;}}elseif (str[j] .y == str[j + 1].y){count++;continue;}}if (k != 0){printf("%d\n", (sum + (n - flat) + (m - flat1)));}else{if (n >= 2 && m >= 2){printf("%d\n", n + m);}elseif (n >= 2 && m < 2){printf("%d\n", m);}elseif (n < 2 && m >= 2){printf("%d\n", n);}else{printf("0\n");}}}return 0;}



0 0
原创粉丝点击