Counting Pair
Time Limit:1000 msMemory Limit:65535 kBSolved:112Tried:1209
Submit
Status
Best Solution
Back
Description
Bob hosts a party and invites N boys and M girls. He gives every boy here a unique number Ni(1 <= Ni <= N). And for the girl, everyone holds a unique number Mi(1 <= Mi <= M), too.
Now when Bob name a number X, if a boy and a girl wants and their numbers' sum equals to X, they can get in pair and dance.
At this night, Bob will name Q numbers, and wants to know the maxinum pairs could dance in each time. Can you help him?
Input
First line of the input is a single integer T(1 <= T <= 30), indicating there are T test cases.
The first line of each test case contains two numbers N and M(1 <= N,M <= 100000).
The second line contains a single number Q(1 <= Q <= 100000).
Each of the next Q lines contains one number X(0 <= X <= 10^9), indicating the number Bob names.
Output
For each test case, print "Case #t:" first, in which t is the number of the test case starting from 1.
Then for each number Bob names, output a single num in each line, which shows the maxinum pairs that could dance together.
Sample Input
1
4 5
3
1
2
3
Sample Output
Case #1:
0
1
2
Hint
This problem has very large input data. scanf and printf are recommended for C++ I/O.
Source
Sichuan State Programming Contest 2012
看代码就懂了
1 #include <iostream>
2 #include <queue>
3 #include <vector>
4 #include <map>
5 #include <string>
6 #include <algorithm>
7 #include <cstdio>
8 #include <cstring>
9 #include <cmath>
10
11 using namespace std;
12
13 int T, N, M, Q, s, cnt;
14
15 int main()
16 {
17 scanf("%d", &T);
18 for(int ca = 1; ca <= T; ca++)
19 {
20 scanf("%d %d", &N, &M);
21 scanf("%d", &Q);
22 printf("Case #%d:\n", ca);
23 while(Q--)
24 {
25 scanf("%d", &s);
26 if(s <= 1 || s > M + N) cnt = 0;
27 else
28 {
29 if(N < M) swap(M, N);
30 if(s <= M) cnt = s - 1;
31 else if(s > M && s <= N) cnt = M;
32 else if(s > N) cnt = M + N - s + 1;
33 }
34 printf("%d\n",cnt);
35 }
36 }
37 return 0;
38 }
原文链接: https://www.cnblogs.com/cszlg/p/3220713.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/97622
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!