#include<bits/stdc++.h>usingnamespace std;intmain(){int n, k;
cin >> n >> k;
vector<int> v;int x;for(int i =0; i < n; i ++) cin >> x, v.push_back(x);sort(v.begin(), v.end());
cout << v[k -1]<< endl;return0;}
1.2 找数组中的最小差值
#include<bits/stdc++.h>usingnamespace std;intmain(){int n;
cin >> n;int x, res =2e9;
vector<int> diff;for(int i =0; i < n; i ++) cin >> x, diff.push_back(x);sort(diff.begin(), diff.end());for(int i = diff.size()-1; i; i --) diff[i]-= diff[i -1], res =min(diff[i], res);
cout << res << endl;return0;}
1.3 最大子列和问题
#include<bits/stdc++.h>usingnamespace std;typedeflonglong LL;intmain(){int n;
cin >> n;
vector<LL>a(n +1);for(int i =1; i <= n; i ++) cin >> a[i], a[i]+= a[i -1];
LL res =0, minv = a[0];for(int i =1; i <= n; i ++){
res =max(res, a[i]- minv);
minv =min(minv, a[i]);}
cout << res << endl;}
#include<bits/stdc++.h>usingnamespace std;intmain(){int n, m;
cin >> n >> m;int cnt =0;
vector<bool>used(n +1);
vector<int> path;
function<void()> dfs =[&](){if(path.size()== m){
cnt ++;for(auto&x : path) cout << x <<' ';
cout << endl;return;}for(int i =1; i <= n; i ++)if(!used[i]){
used[i]=true;
path.push_back(i);dfs();
path.pop_back();
used[i]=false;}};dfs();
cout << cnt << endl;return0;}
1.6 最小总体等待时间
#include<bits/stdc++.h>usingnamespace std;intmain(){int n;
cin >> n;
vector<int>goods(n);for(int i =0; i < n; i ++) cin >> goods[i];sort(goods.begin(), goods.end());int res =0;for(int i =0; i < goods.size()-1; i ++)
res +=(goods.size()- i -1)* goods[i];
cout << res << endl;return0;}
1.7最少加油数
#include<bits/stdc++.h>usingnamespace std;intmain(){int m, n, k;
cin >> m >> n >> k;
vector<int>dis(k);for(int i =0; i < k; i ++) cin >> dis[i];int cnt =0, cur =0;while(cur < m){int newCur = cur + n;if(newCur >= m)break;// 找到没油之前最近的一个加油站int l =0, r = dis.size()-1;while(l < r){int mid = l + r +1>>1;if(dis[mid]<= newCur) l = mid;else r = mid -1;}
cnt ++;
cur = dis[r];}
cout << cnt << endl;return0;}
#include<iostream>#include<vector>#include<algorithm>usingnamespace std;constint N=105;structMovie{int start_time;int end_time;}movies[N];boolcmp( Movie a, Movie b){return a.end_time < b.end_time;}intmain(){int n;while(cin >> n && n !=0){for(int i =0; i < n; i++){
cin >> movies[i].start_time >> movies[i].end_time;}sort(movies, movies+n, cmp);int end_time =0;int count =0;for(int i =0; i < n; i++){if(movies[i].start_time >= end_time){
end_time = movies[i].end_time;
count++;}}
cout << count << endl;}return0;}
1.10生成最小数
#include<bits/stdc++.h>usingnamespace std;typedeflonglong LL;
LL n, k;
vector<int>query(int x){
vector<int> res;for(int i =0; i < n; i ++)if(x >> i &1)
res.push_back(i);return res;}intmain(){
cin >> n >> k;
vector<int> nums;while(n) nums.push_back(n %10), n /=10;reverse(nums.begin(), nums.end());
n = nums.size();
LL res =1e10;for(int i =0; i <1<< n; i ++){
vector<int> q =query(i);if(q.size()== n - k){
LL sum =0;for(int j =0; j < q.size(); j ++)
sum = sum *10+ nums[q[j]];
res =min(sum, res);}}
cout << res << endl;return0;}