Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
过了。。。贴代码得瑟一下~~欢迎提出修改建议!#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct d{ int value; long int start_loc; long int end_loc; }DATA; int get_value(DATA data[] , int loc){ int i; for (i = 0 ; data[i].value != -1 ; i++){ if(loc >= data[i].start_loc && loc <= data[i].end_loc) break; } return data[i].value; } int calculate(int val1 , int val2 , int result){ int x; if (val1 >= val2) x = val1 - val2; else x = val2 - val1; if (x > result) return x; else return result; } int get_det(DATA data[] , long int n , long int loc , long int len){ int result = 0 , loc_val = get_value(data,loc); if (loc - n >= 0) result = calculate(get_value(data,loc - n) , loc_val , result); if (loc + n <= len) result = calculate(get_value(data,loc + n) , loc_val , result); if (loc % n != 0){ if (loc - 1 >= 0) result = calculate(get_value(data,loc - 1) , loc_val , result); if (loc - n - 1 >= 0) result = calculate(get_value(data,loc - n - 1) , loc_val , result); if (loc + n - 1 <= len) result = calculate(get_value(data,loc + n - 1) , loc_val , result); } if ((loc + 1) % n != 0){ if (loc + 1 <= len) result = calculate(get_value(data,loc + 1) , loc_val , result); if (loc - n + 1 >= 0) result = calculate(get_value(data,loc - n + 1) , loc_val , result); if (loc + n + 1 <= len) result = calculate(get_value(data,loc + n + 1) , loc_val , result); } return result; } void sift(long int endpoint[] , int low , int high){ int i = low , j = 2 * i; long int tmp = endpoint[i]; while (j <= high){ if (j < high && endpoint[j] > endpoint[j + 1]) j++; if (tmp > endpoint[j]){ endpoint[i] = endpoint[j]; i = j; j = 2 * i; } else break; } endpoint[i] = tmp; } void Heapsort(long int endpoint[] , long int sorted_ep[] , int n){ long int tmp; int i , se_top = 0; for (i = n / 2 ; i >= 1 ; i--) sift(endpoint , i , n); for (i = n ; i >= 2 ; i--){ tmp = endpoint[1]; endpoint[1] = endpoint[i]; endpoint[i] = tmp; sift(endpoint , 1 , i - 1); if (se_top == 0) sorted_ep[se_top++] = tmp; else if (tmp != sorted_ep[se_top - 1]) sorted_ep[se_top++] = tmp; } if (endpoint[1] != sorted_ep[se_top - 1]) sorted_ep[se_top++] = endpoint[1]; } int main(){ DATA data[1005]; int count , ep_top , i , value , det_value; long int n , len , m , endpoint[6000] , sorted_ep[6000] , num; while (1){ scanf("%ld",&n); if (n == 0) break; len = -1; count = -1; memset(data,-1,sizeof(data)); memset(endpoint,-1,sizeof(endpoint)); memset(sorted_ep,-1,sizeof(sorted_ep)); while (1){ scanf("%d %ld",&value,&m); if (value == 0 && m == 0) break; count++; data[count].value = value; len++; data[count].start_loc = len; len += (m - 1); data[count].end_loc = len; } for (i = 0 , ep_top = 1 ; data[i].value != -1 ; i++){ endpoint[ep_top++] = data[i].start_loc; if (data[i].start_loc - n >= 0 && get_value(data,data[i].start_loc - n) != -1) endpoint[ep_top++] = data[i].start_loc - n; if (data[i].start_loc + n <= 1000000000 && get_value(data,data[i].start_loc + n) != -1) endpoint[ep_top++] = data[i].start_loc + n; endpoint[ep_top++] = data[i].end_loc; if (data[i].end_loc - n >= 0 && get_value(data,data[i].end_loc - n) != -1) endpoint[ep_top++] = data[i].end_loc - n; if (data[i].end_loc + n <= 1000000000 && get_value(data,data[i].end_loc + n) != -1) endpoint[ep_top++] = data[i].end_loc + n; } Heapsort(endpoint , sorted_ep , ep_top - 1); /*for (i = 1 ; endpoint[i] != -1 ; i++) printf(" %-ld",endpoint[i]); printf("\n"); for (i = 0 ; sorted_ep[i] != -1 ; i++) printf(" %-ld",sorted_ep[i]); printf("\n"); printf("%ld\n",len);*/ printf("%ld\n",n); det_value = get_det(data , n , sorted_ep[0] , len); num = 1; for (i = 1 ; sorted_ep[i] != -1 ; i++){ if (sorted_ep[i] - sorted_ep[i - 1] > 1){ if (det_value != get_det(data , n , sorted_ep[i] - 1 , len)){ printf("%d %ld\n",det_value,num); det_value = get_det(data , n , sorted_ep[i] - 1 , len); num = sorted_ep[i] - sorted_ep[i - 1] - 1; } else{ num += (sorted_ep[i] - sorted_ep[i - 1] - 1); } } if (det_value != get_det(data , n , sorted_ep[i] , len)){ printf("%d %ld\n",det_value,num); det_value = get_det(data , n , sorted_ep[i] , len); num = 1; } else{ num++; } } printf("%d %ld\n",det_value,num); printf("0 0\n"); } printf("0\n"); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator