Poj--1009(模拟,技巧)

2014-11-27 02:44:34

思路:这题写了好久....感觉最后也没写的好。

  处理方法就是处理每段的起点,及其周围8个点的值,最后将答案按位置排序,中间的位置的值和离他最近的前面的答案值一样,注意还要处理总长+1的位置!(思考)

  二分这个问题,还是要考虑清楚,比较特殊的二分要在while里面加特判条件。

  1 /*************************************************************************
  2     > File Name: p1009.cpp
  3     > Author: Nature
  4     > Mail: 564374850@qq.com
  5     > Created Time: Tue 25 Nov 2014 07:41:53 PM CST
  6 ************************************************************************/
  7 
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <cstdlib>
 11 #include <cmath>
 12 #include <vector>
 13 #include <map>
 14 #include <set>
 15 #include <stack>
 16 #include <queue>
 17 #include <iostream>
 18 #include <algorithm>
 19 using namespace std;
 20 #define lp (p << 1)
 21 #define rp (p << 1|1)
 22 #define getmid(l,r) (l + (r - l) / 2)
 23 #define MP(a,b) make_pair(a,b)
 24 typedef long long ll;
 25 const int INF = 1 << 30;
 26 
 27 struct node{
 28     int pos,val;
 29     node(int a,int b) : pos(a),val(b) {}
 30 };
 31 
 32 vector<node> mp;
 33 vector<node> ans;
 34 int n,v[1010],len[1010],sum;
 35 
 36 int B_search(int val){
 37     int l = 0,r = mp.size() - 1;
 38     while(l <= r){
 39         int mid = getmid(l,r);
 40         if(l + 1 >= r){
 41             if(mp[r].pos <= val) return mp[r].val;
 42             else return mp[l].val;
 43         }
 44         if(mp[mid].pos <= val)
 45             l = mid;
 46         else
 47             r = mid - 1;
 48     }
 49     return mp[l].val;
 50 }
 51 
 52 void Cal(int num,int val){
 53     if(num > sum){
 54         return;
 55     }
 56     int res = 0;
 57     if(num > n){
 58         res = max(res,abs(val - B_search(num - n)));
 59         if((num - n) % n != 0) res = max(res,abs(val - B_search(num - n + 1)));
 60         if((num - n) % n != 1) res = max(res,abs(val - B_search(num - n - 1)));
 61     }
 62     if(num % n != 0) res = max(res,abs(val - B_search(num + 1)));
 63     if(num % n != 1) res = max(res,abs(val - B_search(num - 1)));
 64     if(num + n <= sum){
 65         res = max(res,abs(val - B_search(num + n)));
 66         if((num + n) % n != 0) res = max(res,abs(val - B_search(num + n + 1)));
 67         if((num + n) % n != 1) res = max(res,abs(val - B_search(num + n - 1)));
 68     }
 69     //printf("Plus : %d %d\n",num,res);
 70     ans.push_back(node(num,res)); //position and maximum absolute value.
 71 }
 72 
 73 void Solve(int num,int val){
 74     int tmp;
 75     Cal(num,val);
 76     if(num > n){
 77         tmp = B_search(num - n);
 78         Cal(num - n,tmp);
 79         if((num - n) % n != 0){
 80             tmp = B_search(num - n + 1);
 81             Cal(num - n + 1,tmp);
 82         }
 83         if((num - n) % n != 1){
 84             tmp = B_search(num - n - 1);
 85             Cal(num - n - 1,tmp);
 86         }
 87     }
 88     if(num % n != 0){
 89         tmp = B_search(num + 1);
 90         Cal(num + 1,tmp);
 91     }
 92     if(num % n != 1){
 93         tmp = B_search(num - 1);
 94         Cal(num - 1,tmp);
 95     }
 96     if(num + n <= sum){
 97         tmp = B_search(num + n);
 98         Cal(num + n,tmp);
 99         if((num + n) % n != 0){
100             tmp = B_search(num + n + 1);
101             Cal(num + n + 1,tmp);
102         }
103         if((num + n) % n != 1){
104             tmp = B_search(num + n - 1);
105             Cal(num + n - 1,tmp);
106         }
107     }
108 }
109 
110 bool cmp(node a,node b){
111     return a.pos < b.pos;
112 }
113 
114 int main(){
115     int a,b;
116     while(scanf("%d",&n) != EOF && n){
117         mp.clear();
118         ans.clear();
119         sum = 0;
120         int pos = 1,cnt = 0;
121         while(scanf("%d%d",&a,&b) != EOF){
122             if(!a && !b)
123                 break;
124             sum += b;
125             mp.push_back(node(pos,a));
126             mp.push_back(node(pos + b - 1,a));
127             pos += b;
128             v[++cnt] = a;
129             len[cnt] = b;
130         }
131         int tot = 1;
132         for(int i = 1; i <= cnt; ++i){
133             Solve(tot,v[i]);
134             tot += len[i];
135         }
136         Solve(tot,0);
137         sort(ans.begin(),ans.end(),cmp);
138         int pre = 1,sz = ans.size();
139         printf("%d\n",n);
140         for(int i = 0; i < sz - 1; ++i){
141             if(ans[i].val != ans[i + 1].val){
142                 printf("%d %d\n",ans[i].val,ans[i + 1].pos - pre);
143                 pre = ans[i + 1].pos;
144             }
145         }
146         printf("%d %d\n",ans[sz - 1].val,sum - pre + 1);
147         printf("0 0 \n");
148     }
149     printf("0\n");
150     return 0;
151 }

 

 

 

posted @ 2014-11-27 02:47  Naturain  阅读(143)  评论(0编辑  收藏  举报