HDU 4585 Shaolin(STL map),hdu4585
Shaolin
Time Limit:1000MS
Memory Limit:32768KB
64bit IO Format:%I64d & %I64uSubmit Status Practice HDU 4585
Description
Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account.
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
Input
There are several test cases.
In each test case:
The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000)
The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.
Output
A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.
Sample Input
32 13 34 20
Sample Output
2 13 24 2
大致題意:很久以前,少林寺裡只有一個等級10^9為方丈,他編號為1,少林寺裡有個規矩,每個新來的和尚都必須和一位等級最接近自己的老(就是比他先來的)和尚進行戰鬥,後來又來了n個和尚,並且他們按入寺時間依次編號2~n+1,但是時間太久,方丈已經不知道每個和尚來的時候是跟哪個和尚戰鬥了,但是他還是記得哪個和尚比哪個和尚先來的。輸入給出n個和尚的編號和他們的等級。下面就靠你了,讓你依次求出每個和尚進寺是是跟誰戰鬥的,並輸出。
解題思路:直接開數組的話,每次找跟他等級最接近的肯定要排序,目前知道的最快的快排的也是O(n*lg n)的,由於資料給的是10^5,再加上還要操作,時間已經超了。所以,得換個思路,那就得用到STL中的map的優勢了,也好久沒用map做題了。map裡的資料會自動按鍵值升序排列,據說map內部的排序是用紅/黑樹狀結構(一種不明覺厲的資料結構)實現的,其時間複雜度是O(n)的,同時,map裡還可以直接用find()按鍵值尋找鍵值所在的迭代器,這就太方便了。
具體實現步驟:1.先開個map<int, int> monk, 把方丈的編號id和等級grad在map裡建立映射,這裡用到了一個小細節,
建立映射可以直接這樣寫,monk[grad] = id.
2.在迴圈輸入n個和尚的資訊,
1)每次輸入一個和尚的資訊,然後在map裡建立映射;
2)此時用find(grad)尋找當前加入的和尚所在的迭代器;
3)然後,考慮當前的迭代器的位置,若是最後一個,就直接輸出當前迭代器的前一個;
要是第一個,就輸出當前迭代器的後面一個;
要既不是第一個也不是最後一個的話,就比較當前迭代器的first和它前後兩迭代器first之間的差值哪個更小,小的輸出;
要是兩差值一樣的話,輸出前一個迭代器。
AC代碼:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7fffffffmap<int, int> a; //聲明一個map對象int main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int n, k, g; while(scanf("%d",&n)!=EOF && n) { a[1e9] = 1; //處理方丈 for(int i=0; i<n; i++){ scanf("%d%d", &k, &g); a[g] = k; map<int, int>::iterator temp = a.find(g); //按grad尋找當前和尚所在的迭代器 if(temp == a.begin()) printf("%d %d\n", k, (++temp)->second); else if(temp == a.end()) printf("%d %d\n", k, (--temp)->second); else{ if(abs((--temp)->first - (++temp)->first) > abs((++temp)->first - (--temp)->first)) printf("%d %d\n", k, (++temp)->second); else printf("%d %d\n", k, (--temp)->second); } } a.clear(); } return 0;}
hdu3823,暴力題,不過要開2000W的數組,C++中怎開那大的數組,而不越界呀?
額,c++裡用int * ary = new int[2000000];開在自由儲存區裡,或者直接和C一樣開成全域變數,應該都是可以的。
LZ說到的vector是C++裡STL的一個容器,它的記憶體管理方法並不是LZ想的那樣,而是這樣的:
vector維護一個數組作為它儲存資料的地方,這個數組是動態,也就是用new來開出來的,並且有一個預設大小(當然也可以用參數來制定)。當這個數組因為插入而滿了的時候,就再開一個大小為原數組的2倍再加1的數組,然後把原來的數組元素逐一複製到新數組中,最後再刪除舊數組。 所以它要維護2000W個元素,很有可能開出4000w大小的數組來額....
話說C裡面能做的C++都可以做啊,就是在自由儲存區開空間的方法不同,不適用malloc而是用new,簡單來說就是這樣。
哪位幫我看一下hdu acm 1040這樣編為何不行?
//代碼已經AC,去掉部分多餘花括符..^_^
#include<iostream>
using namespace std;
int main()
{
int T,N,n,i,j;
long int a[1002]; //題目有說明輸入的資料類型均為int ,所以沒必要定義為long
cin>>T; //已經有多組資料測試,添加while()反而無法AC
while (T--)
{
cin>>N;
n=N;
i=0;
while (n--)
cin>>a[i++];
for (j=1;j<N;j++)
for (i=N-1;i>=j;i--)
if (a[i]<a[i-1])
{
n=a[i];
a[i]=a[i-1];
a[i-1]=n;
}
for (i=0;i<N;i++)
{
cout<<a[i];
if (i==(N-1)) //貌似對最後一個輸出後要求直接換行,加空格後
cout<<endl; //再換行就提示 Presentation Error
else
cout<<" ";
}
}
return 0;
}