第12周第13周
12&13:STL
Standard Template Library
sort, binary_search/lower_bound/upper_bound, multiset, set, multimap, map.
?
作業
1.sort簡單題
Description:程序填空,產生指定輸出結果。
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 int main() 6 { 7 int a[8] = {6,5,14,23,2,9,87,10 }; 8 sort( 9 // Your Code Here 10 ); 11 for(int i = 0;i < 8; ++i) 12 cout << a[i] << "," ; 13 return 0; 14 }Input:(無)
Output:6,87,23,14,9,5,2,10,
Sample Input:(無)
Sample Output:6,87,23,14,9,5,2,10,
a+1, a+7, greater<int>()2.還是sort簡單題
Description:程序填空,產生指定輸出結果。
1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 6 struct Point{ 7 int x; 8 int y; 9 }; 10 // Your Code Here 11 int main() 12 { 13 int a[8] = {6,5,55,23,3,9,87,10 }; 14 sort(a,a+8,Rule1()); 15 for(int i = 0;i < 8; ++i) 16 cout << a[i] << "," ; 17 cout << endl; 18 Point ps[8] = {{1,0},{0,1},{0,-1},{-1,0},{1,-1},{1,1},{2,0},{-2,0} } ; 19 sort(ps,ps+8,Rule2()); 20 for(int i = 0;i < 8; ++i) 21 cout << "(" << ps[i].x << "," << ps[i].y << ")"; 22 return 0; 23 }Input:(無)
Output:
10,23,3,55,5,6,87,9,
(-1,0)(0,-1)(0,1)(1,0)(1,-1)(1,1)(-2,0)(2,0)
整數按照個位數從小到大排。個位數相同,則大的排前面。點按照離原點從近到遠排。距離相同,則按x坐標從小到大排。x坐標也相同,則按y坐標從小到大排。
Sample Input:(無)
Sample Output:
10,23,3,55,5,6,87,9,
(-1,0)(0,-1)(0,1)(1,0)(1,-1)(1,1)(-2,0)(2,0)
3.點集的查詢
Description:先給定平面上一些點的坐標 然后給定一些查詢命令,根據查詢命令輸出Yes或No,要求使用sort和binary_search實現。
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 using namespace std; 5 struct Point 6 { 7 int x; 8 int y; 9 }points[100100]; 10 bool func(const Point & P1,const Point & p2); 11 struct Rule { 12 bool operator () ( const Point & p1,const Point & p2) { 13 return func(p1,p2); 14 } 15 }; 16 int main() 17 { 18 int n,m; 19 scanf("%d%d",&n,&m); 20 for(int i = 0;i < n; ++i) 21 scanf("%d%d",&points[i].x, &points[i].y); 22 // Your Code HereInput:
第一行是整數n和m,0<n,m<=100000。接下來是n行,每行兩個整數x,y代表一個點的坐標。0<x,y<=10^9。
數據確保對于任意兩點 (x1,y1)和(x2,y2)表達式x1>x2 && y1<y2 || x1<x2 && y1>y2一定為真。
再接下來是m行,每行格式為x n或y n,分別表示詢問x坐標為n的點是否存在和y坐標為n的點是否存在。 0<n<=10^9。
Output:對每個詢問,輸出 "Yes"或"No"。
Sample Input:
5 6
8 22
12 20
5 24
13 19
2 30
x 12
x 7
y 20
y 7
y 19
x 8
Sample Output:
Yes
No
Yes
No
Yes
Yes
4.set
Description:
現有一整數集(允許有重復元素),初始為空。我們定義如下操作:
add x 把x加入集合;del x 把集合中所有與x相等的元素刪除;ask x 對集合中元素x的情況詢問。對每種操作,我們要求進行如下輸出:
add 輸出操作后集合中x的個數;del 輸出操作前集合中x的個數;ask 先輸出0或1表示x是否曾被加入集合(0表示不曾加入),再輸出當前集合中x的個數,中間用空格格開。
Input:第一行是一個整數n,表示命令數。0<=n<=100000。后面n行命令,如Description中所述。
Output:共n行,每行按要求輸出。
Sample Input:
7
add 1
add 1
ask 1
ask 2
del 2
del 1
ask 1
Sample Output:
1
2
1 2
0 0
0
2
1 0
5.熱血格斗場
Description:
為了迎接08年的奧運會,讓大家更加了解各種格斗運動,facer新開了一家熱血格斗場。格斗場實行會員制,但是新來的會員不需要交入會費,而只要同一名老會員打一場表演賽,證明自己的實力。我們假設格斗的實力可以用一個正整數表示,成為實力值。另外,每個人都有一個唯一的id,也是一個正整數。為了使得比賽更好看,每一個新隊員都會選擇與他實力最為接近的人比賽,即比賽雙方的實力值之差的絕對值越小越好,如果有兩個人的實力值與他差別相同,則他會選擇比他弱的那個(顯然,虐人必被虐好)。不幸的是,Facer一不小心把比賽記錄弄丟了,但是他還保留著會員的注冊記錄。現在請你幫facer恢復比賽紀錄,按照時間順序依次輸出每場比賽雙方的id。
Input:
第一行一個數n(0 < n <=100000),表示格斗場新來的會員數(不包括facer)。以后n行每一行兩個數,按照入會的時間給出會員的id和實力值。一開始,facer就算是會員,id為1,實力值1000000000。輸入保證兩人的實力值不同。
Output:N行,每行兩個數,為每場比賽雙方的id,新手的id寫在前面。
Sample Input:
3
2 1
3 3
4 2
Sample Output:
2 1
3 2?
4 2
1 #include <iostream> 2 #include <set> 3 #include <cstdio> 4 #include <cmath> 5 6 using namespace std; 7 8 struct Fighter 9 { 10 int id; 11 int power; 12 }; 13 struct Rule 14 { 15 bool operator()(const Fighter &f1, const Fighter &f2) { 16 return f1.power < f2.power; 17 } 18 }; 19 20 int main() 21 { 22 int n; 23 scanf("%d", &n); 24 set<Fighter, Rule> st; 25 Fighter ft; 26 ft.id = 1; 27 ft.power = 1000000000; 28 st.insert(ft); 29 for(int i=0; i<n; ++i) { 30 scanf("%d %d", &ft.id, &ft.power); 31 set<Fighter>::iterator pl = st.lower_bound(ft); 32 set<Fighter>::iterator pu = st.upper_bound(ft); 33 if(pl == st.begin()) 34 printf("%d %d\n", ft.id, pl->id); 35 else if(pu == st.end()){ 36 --pu; 37 printf("%d %d\n", ft.id, pu->id); 38 } 39 else { 40 --pl; 41 if(ft.power-pl->power <= pu->power-ft.power) 42 printf("%d %d\n", ft.id, pl->id); 43 else 44 printf("%d %d\n", ft.id, pu->id); 45 } 46 st.insert(ft); 47 } 48 49 return 0; 50 }6.冷血格斗場
Description:
為了迎接08年的奧運會,讓大家更加了解各種格斗運動,facer新開了一家冷血格斗場。格斗場實行會員制,但是新來的會員不需要交入會費,而只要同一名老會員打一場表演賽,證明自己的實力。我們假設格斗的實力可以用一個正整數表示,成為實力值,兩人的實力值可以相同。另外,每個人都有一個唯一的id,也是一個正整數。為了使得比賽更好看,每一個新隊員都會選擇與他實力最為接近的人比賽,即比賽雙方的實力值之差的絕對值越小越好,如果有多個人的實力值與他差別相同,則他會選擇id最小的那個。不幸的是,Facer一不小心把比賽記錄弄丟了,但是他還保留著會員的注冊記錄。現在請你幫facer恢復比賽紀錄,按照時間順序依次輸出每場比賽雙方的id。
Input:
第一行一個數n(0 < n <=100000),表示格斗場新來的會員數(不包括facer)。以后n行每一行兩個數,按照入會的時間給出會員的id和實力值。一開始,facer就算是會員,id為1,實力值1000000000。
Output:N行,每行兩個數,為每場比賽雙方的id,新手的id寫在前面。
Sample Input:
3
2 3
3 1
4 2
Sample Output:
2 1
3 2
4 2
?
轉載于:https://www.cnblogs.com/VincentValentine/p/5678485.html
總結
- 上一篇: 解决Ubuntu 14下,PhpStor
- 下一篇: Bash shell的内建命令:type