日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第12周第13周

發布時間:2024/4/17 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第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)

1 struct Rule1 { 2 bool operator() (const int &n1, const int &n2) { 3 if(n1%10 != n2%10) 4 return (n1%10) < (n2%10); 5 else 6 return n1 > n2; 7 } 8 }; 9 10 double distance(Point p) 11 { 12 return sqrt(p.x*p.x+p.y*p.y); 13 } 14 15 struct Rule2 { 16 bool operator() (const Point &p1, const Point &p2) { 17 if(abs(distance(p1)-distance(p2)) < 1e-6) { 18 if(p1.x != p2.x) 19 return p1.x < p2.x; 20 else 21 return p1.y < p2.y; 22 } 23 else 24 return distance(p1) < distance(p2); 25 } 26 };

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 Here

Input:

第一行是整數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

1 sort(points, points+n, Rule()); 2 for(int i=0; i<m; ++i) { 3 int a; 4 char cmd[10]; 5 scanf("%s %d", cmd, &a); 6 Point pt; 7 if(cmd[0] == 'x') { 8 pt.x = a; 9 if(binary_search(points, points+n, pt, Rule())) 10 printf("Yes\n"); 11 else 12 printf("No\n"); 13 } 14 else { 15 pt.x = -1; 16 pt.y = a; 17 if(binary_search(points, points+n, pt, Rule())) 18 printf("Yes\n"); 19 else 20 printf("No\n"); 21 } 22 } 23 return 0; 24 } 25 26 bool func(const Point &p1, const Point &p2) 27 { 28 if(p1.x!=-1 && p2.x!=-1) 29 return p1.x < p2.x; 30 else 31 return p1.y > p2.y; 32 }

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

1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 #include <string> 5 #include <cstring> 6 7 using namespace std; 8 9 typedef multiset<int>::iterator PTR; 10 11 int main() 12 { 13 int n; 14 multiset<int> mst; 15 set<int> st; 16 PTR p; 17 scanf("%d", &n); 18 19 for(int i=0; i<n; ++i) { 20 char cmd[20]; 21 int x; 22 scanf("%s %d", cmd, &x); 23 switch(cmd[1]) { 24 case 'd': 25 st.insert(x); 26 mst.insert(x); 27 printf("%d\n", mst.count(x)); 28 break; 29 case 'e': 30 printf("%d\n", mst.count(x)); 31 p = mst.find(x); 32 if(p != mst.end()) { 33 pair<PTR,PTR> range = mst.equal_range(x); 34 mst.erase(range.first, range.second); 35 } 36 break; 37 case 's': 38 set<int>::iterator pp = st.find(x); 39 if(pp != st.end()) 40 printf("1 %d\n", mst.count(x)); 41 else 42 printf("0 %d\n", mst.count(x)); 43 break; 44 } 45 } 46 47 return 0; 48 }

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

1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <cmath> 5 6 using namespace std; 7 8 multimap<int, int> mp; 9 10 void toErase(multimap<int, int>::iterator p1, multimap<int, int>::iterator p2) 11 { 12 if(p1->first == p2->first) { 13 if(p1->second < p2->second) 14 mp.erase(p2); 15 else 16 mp.erase(p1); 17 } 18 } 19 20 int main() 21 { 22 mp.insert(make_pair(1000000000,1)); 23 int n; 24 scanf("%d",&n); 25 26 for(int i=0; i<n; ++i) { 27 int id, power; 28 scanf("%d %d",&id, &power); 29 multimap<int,int>::iterator p = mp.insert(make_pair(power,id)); 30 multimap<int,int>::iterator pL = p; -- pL; 31 multimap<int,int>::iterator pR = p; ++ pR; 32 if(p == mp.begin()) { 33 printf("%d %d\n",id, pR->second); 34 toErase(p,pR); 35 } 36 else if(p == --mp.end()) { 37 printf("%d %d\n", id, pL->second); 38 toErase(p,pL); 39 } 40 else { 41 int dL = abs(p->first-pL->first); 42 int dR = abs(p->first-pR->first); 43 if(dL<dR || dL==dR && pL->second<pR->second) { 44 printf("%d %d\n", id, pL->second); 45 toErase(p,pL); 46 } 47 else if(dL>dR || dL==dR && pL->second>pR->second) { 48 printf("%d %d\n", id, pR->second); 49 toErase(p,pR); 50 } 51 } 52 } 53 54 return 0; 55 } 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 8 using namespace std; 9 10 map<int,set<int> > mp; 11 12 int main() 13 { 14 set<int> st; 15 st.insert(1); 16 mp.insert(make_pair(1000000000, st)); 17 int n; 18 scanf("%d", &n); 19 20 for(int i=0; i<n; ++i) { 21 int id,power; 22 scanf("%d %d",&id, &power); 23 map<int,set<int> >::iterator pL = mp.lower_bound(power); 24 if(pL == mp.end()) 25 printf("%d %d\n", id, *(mp.rbegin()->second.begin())); 26 else if(pL == mp.begin()) 27 printf("%d %d\n", id, *(mp.begin()->second.begin())); 28 else { 29 if(pL->first == power) 30 printf("%d %d\n", id, *(pL->second.begin())); 31 else { 32 int d1 = abs(pL->first-power); 33 map<int,set<int> >::iterator p = pL; 34 --p; 35 int d2 = abs(p->first-power); 36 if(d1 < d2) 37 printf("%d %d\n", id, *(pL->second.begin())); 38 else if(d1 > d2) 39 printf("%d %d\n", id, *(p->second.begin())); 40 else { 41 printf("%d %d\n", id, min(*(pL->second.begin()), *(p->second.begin()))); 42 } 43 } 44 } 45 mp[power].insert(id); 46 } 47 48 return 0; 49 }

?

轉載于:https://www.cnblogs.com/VincentValentine/p/5678485.html

總結

以上是生活随笔為你收集整理的第12周第13周的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。