首页 > 其他分享 >43(map)

43(map)

时间:2022-11-23 20:33:18浏览次数:41  
标签:pre map power 43 next second members id

 1 #include <iostream>
 2 #include <map>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 //key:实力值power  value:id号 
 8 int main(){
 9     map<int,int> members;
10     members.insert(make_pair(1000000000,1));
11     int n,id,power;
12     scanf("%d",&n);
13     map<int,int>::iterator pre,next;
14     while(n --){
15         scanf("%d%d",&id,&power);
16         pre = next = members.lower_bound(power);
17         if(pre == members.begin()) //第一个人实力大于等于power 
18           printf("%d %d\n",id,next -> second);
19         else if(next == members.end()){//所有人实力都小于power
20           pre --;
21           printf("%d %d\n",id,pre -> second);
22         }
23         else{
24           pre --; //1.next == power,输出就会是next(power) 2.next > power, 输出就会是pre和next中的一个 
25           int abs1 = abs(pre -> first - power);
26           int abs2 = abs(next -> first - power);
27           if(abs1 > abs2)
28             printf("%d %d\n",id,next -> second);
29           else if(abs1 < abs2)
30             printf("%d %d\n",id,pre -> second);
31           else
32             printf("%d %d\n",id,pre -> second > next -> second ? next -> second : pre -> second);
33         }
34         if(members.count(power))
35           members[power] = min(members[power],id);
36         else
37           members.insert(make_pair(power,id));
38     }
39     return 0;
40 }

 

标签:pre,map,power,43,next,second,members,id
From: https://www.cnblogs.com/balabalabubalabala/p/16919710.html

相关文章