Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 38
1 /* ********************************************** 2 Author : kuangbin 3 Created Time: 2013/8/10 11:55:20 4 File Name : F:\2013ACM练习\比赛练习\2013杭州邀请赛重现\1011.cpp 5 *********************************************** */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 using namespace std; 19 20 int main() 21 { 22 //freopen("in.txt","r",stdin); 23 //freopen("out.txt","w",stdout); 24 set<int>st; 25 map<int,int>mp; 26 int n; 27 while(scanf("%d",&n) == 1 && n) 28 { 29 st.clear(); 30 mp.clear(); 31 st.insert(1000000000); 32 mp[1000000000] = 1; 33 int u,v; 34 while(n--) 35 { 36 scanf("%d%d",&u,&v); 37 printf("%d ",u); 38 set<int>::iterator it = st.lower_bound(v); 39 if(it == st.end()) 40 { 41 it--; 42 printf("%d\n",mp[*it]); 43 } 44 else 45 { 46 int t1 = (*it); 47 if(it != st.begin()) 48 { 49 it--; 50 if(v - (*it) <= t1 - v) 51 { 52 printf("%d\n",mp[*it]); 53 } 54 else printf("%d\n",mp[t1]); 55 } 56 else printf("%d\n",mp[*it]); 57 } 58 mp[v] = u; 59 st.insert(v); 60 } 61 } 62 return 0; 63 }
今天才知道原来直接用map也可以实现。
原来map也是排序了的,Orz....
1 /* ********************************************** 2 Author : kuangbin 3 Created Time: 2013/8/10 11:55:20 4 File Name : F:\2013ACM练习\比赛练习\2013杭州邀请赛重现\1011.cpp 5 *********************************************** */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 using namespace std; 19 20 int main() 21 { 22 //freopen("in.txt","r",stdin); 23 //freopen("out.txt","w",stdout); 24 map<int,int>mp; 25 int n; 26 while(scanf("%d",&n) == 1 && n) 27 { 28 mp.clear(); 29 mp[1000000000] = 1; 30 int u,v; 31 while(n--) 32 { 33 scanf("%d%d",&u,&v); 34 printf("%d ",u); 35 map<int,int>::iterator it = mp.lower_bound(v); 36 if(it == mp.end()) 37 { 38 it--; 39 printf("%d\n",it->second); 40 } 41 else 42 { 43 int t1 = it->first; 44 int tmp = it->second; 45 if(it != mp.begin()) 46 { 47 it--; 48 if(v - it->first <= t1 - v) 49 { 50 printf("%d\n",it->second); 51 } 52 else printf("%d\n",tmp); 53 } 54 else printf("%d\n",it->second); 55 } 56 mp[v] = u; 57 } 58 } 59 return 0; 60 }