hdu4585 STL水题
生活随笔
收集整理的這篇文章主要介紹了
hdu4585 STL水题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 成立少林寺,剛開始有一個大師,id是1,攻擊力是10E,現在陸續來人,每個人有自己的id,和自己的攻擊力,但是每一個新來的要和之前的和尚pk,他必須選擇和他攻擊力差值最小的那個,如果有兩個差值一樣的話選擇攻擊力比他小的那個,輸出pk組合..
思路:
? ? ? 成立少林寺,剛開始有一個大師,id是1,攻擊力是10E,現在陸續來人,每個人有自己的id,和自己的攻擊力,但是每一個新來的要和之前的和尚pk,他必須選擇和他攻擊力差值最小的那個,如果有兩個差值一樣的話選擇攻擊力比他小的那個,輸出pk組合..
思路:
? ? ?一開始還以為線段樹呢,哎! 水題,我用的set找最接近的,map哈希他們的id了,其實直接一個map就行了,自己的STL用的不是很熟,所以開了兩個set一個map才AC,總之這個是水題,不多解釋..
#include<stdio.h> #include<set> #include<map> using namespace std; map<int ,int>hash_id; set<int>st1 ,st2;int abss(int x) {return x > 0 ? x : -x; }int main () {int n ,i ,a, b;while(~scanf("%d" ,&n) && n){hash_id.clear();st1.clear();st2.clear();hash_id[1000000000] = 1;st1.insert(1000000000);st2.insert(-1000000000);st2.insert(1);for(i = 1 ;i <= n ;i ++){scanf("%d %d" ,&a ,&b);int aa = *st1.lower_bound(b);int bb = *st2.lower_bound(-b) * -1;if(bb < 0)printf("%d %d\n" ,a ,hash_id[aa]);else{if(abss(b - bb) <= abss(b - aa))printf("%d %d\n" ,a ,hash_id[bb]);elseprintf("%d %d\n" ,a ,hash_id[aa]);}st1.insert(b);st2.insert(-b);hash_id[b] = a;}}return 0; }
總結
以上是生活随笔為你收集整理的hdu4585 STL水题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu1287 破译密码
- 下一篇: hdu1839 二分最短路