PAT1048

注意一个坑,就是当M是偶数的时候可能有两个M >> 1的值,这种时候就只能数数了..

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <fstream>
 5
 6 using namespace std;
 7
 8 #define OJ
 9
10 #ifdef OJ
11 #define fin cin
12 #else
13 ifstream fin("in.data");
14 #endif
15
16 int main(){
17     int N, M, n;
18     fin >> N >> M;
19
20     vector<int> money(1001, 0);
21     for (int i = 0; i < N; i++){
22         fin >> n;
23         if (n >= M) continue;
24         money[n]++;
25     }
26
27     int half_M = (M >> 1) + 1;
28     for (int i = 1; i < half_M; i++){
29         if (money[i] > 0)
30             money[i]--;
31         else
32             continue;
33         if (money[M - i] > 0){
34             cout << i << " " << M - i << endl;
35             return 0;
36         }
37     }
38
39     cout << "No Solution" << endl;
40
41     return 0;
42 }
时间: 2024-10-21 01:48:22

PAT1048的相关文章

PAT1048. Find Coins

Eva loves to collect coins from all over the universe, including some other planets like Mars.  One day she visited a universal shopping mall which could accept all kinds of coins as payments.  However, there was a special requirement of the payment:

PAT-1048 Find Coins (25)

这道题目需要用二分查找,否则可能超时,时间复杂度应嘎是n.lgn.可以通过. 二分查找末班 int find(l,r) { int mid=(l+r)/2; if(data[mid]==num) return mid; else if ... return find(l,mid-1) //注意是return find(l,mid-1)而不是find(l,mid-1)否则返回值会丢掉. else return find(mid+1,r) } // 1048.cpp : 定义控制台应用程序的入口点.

pat1048. Find Coins (25)

1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept a