7-35 h0154.加勒比海盗船——最优装载问题
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 1000005; int t; int main() { cin >> t; while (t --) { double c; vector<double> a; int m, cnt = 0; cin >> c >> m; for (int i = 0; i < m; i ++) { double x; cin >> x; a.push_back(x); } sort(a.begin(), a.end()); double ans = 0; for (int i = 0; i < m; i ++) { ans += a[i]; if (ans <= c) cnt ++; else break; } cout << cnt << endl; } return 0; }
7-36 会场安排问题
#include <iostream> #include <algorithm> using namespace std; struct activity { int b, e; // bool operator < (activity & t) { // if (e != t.e) return e < t.e; // } }a[10010]; int n; int st[10010]; bool cmp (activity x, activity y) { return x.b < y.b; // if (x.e != y.e) return x.e < y.e; // else return x.b < y.b; } int main() { cin >> n; int cnt = 0; for (int i = 0; i < n; i ++) { cin >> a[i].b >> a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; } sort(a, a + n, cmp); for (int i = 0; i < n; i ++) // cout << a[i].b << ' ' << a[i].e << endl; if (!st[a[i].e]) { cnt ++; st[a[i].e] = 1; int pre = a[i].e; for (int j = i + 1; j < n; j ++) if (!st[a[j].e] && a[j].b >= pre) { pre = a[j].e; st[a[j].e] = 1; } } cout << cnt << endl; return 0; }
7-37 h0145. 会议安排
#include <iostream> #include <algorithm> using namespace std; struct activity { int b, e, no; bool operator < (activity & t) { if (e != t.e) return e < t.e; else return b < t.b; } }a[10010]; int n, x ,y; // bool cmp (activity x, activity y) { // if (x.b != x.b) return x.b < y.b; // else return x.e < y.e; // } int main() { int t; cin >> t; while (t --) { cin >> n; int cnt = 0, pre = 0; for (int i = 0; i < n; i ++) { cin >> a[i].b >> a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; } sort(a, a + n); for (int i = 0; i < n; i ++) { // cout << a[i].b << ' ' << a[i].e << endl; if (a[i].b >= pre) { pre = a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; cnt ++; } } cout << cnt << endl; } return 0; }
7-38 最少失约
#include <iostream> #include <algorithm> using namespace std; struct activity { int b, e, no; bool operator < (activity & t) { if (e != t.e) return e < t.e; else return b < t.b; } }a[10010]; int n, x ,y; // bool cmp (activity x, activity y) { // if (x.b != x.b) return x.b < y.b; // else return x.e < y.e; // } int main() { int t; cin >> t; while (t --) { cin >> n; int cnt = 0, pre = 0; for (int i = 0; i < n; i ++) { cin >> a[i].b >> a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; } sort(a, a + n); for (int i = 0; i < n; i ++) { // cout << a[i].b << ' ' << a[i].e << endl; if (a[i].b >= pre) { pre = a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; cnt ++; } } cout << n - cnt << endl; } return 0; }
7-39 活动选择问题
#include <iostream> #include <algorithm> using namespace std; struct activity { int b, e, no; bool operator < (activity & t) { if (e != t.e) return e < t.e; else return b < t.b; } }a[10010]; int n; // bool cmp (activity x, activity y) { // if (x.b != x.b) return x.b < y.b; // else return x.e < y.e; // } int main() { while (cin >> n) { int cnt = 0, pre = 0; for (int i = 0; i < n; i ++) { cin >> a[i].b >> a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; } sort(a, a + n); for (int i = 0; i < n; i ++) { // cout << a[i].b << ' ' << a[i].e << endl; if (a[i].b >= pre) { pre = a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; cnt ++; } } cout << cnt << endl; } return 0; }
7-40 删数问题
// 贪心 #include <iostream> using namespace std; int k; string str; int main() { cin >> str >> k; for (int i = 0; i < k; i ++) for (int j = 0; j < str.size(); j ++) if (str[j] > str[j + 1]) { str.erase(str.begin() + j); break; } cout << str << endl; return 0; }
7-41 最短路径条数
#include <iostream> using namespace std; const int N = 1010; int g[N][N]; int main() { int n, m, x1, y1, x2, y2; cin >> n >> m >> x1 >> y1 >> x2 >> y2; if (x1 >= x2) swap(x1, x2); if (y1 >= y2) swap(y1, y2); for (int i = x1; i <= n; i ++) { for (int j = y1; j <= m; j ++) { if (x1 == i || y1 == i) g[i][j] = 1; else g[i][j] = g[i - 1][j] + g[i][j - 1]; } } cout << g[x2][y2] << endl; return 0; }
7-42 一步两步
#include <iostream> using namespace std; const int N = 1010; int f[N], n; int main() { cin >> n; f[0] = 1; f[1] = 2; for (int i = 2; i < n; i ++) f[i] = f[i - 1] + f[i - 2]; cout << f[n - 1]; return 0; }
7-43 青蛙跳台阶
#include <iostream> using namespace std; const int N = 1010; int f[N], n; int main() { cin >> n; f[0] = 1; f[1] = 2; for (int i = 2; i < 60; i ++) f[i] = f[i - 1] + f[i - 2]; while (n --) { int x; cin >> x; cout << f[x - 1] << endl; } return 0; }
7-45 让人头疼的“双十一”
#include <iostream> #include <cstring> using namespace std; const int N = 3010; int m ,n; int v[N], w[N]; int f[N]; int main() { int t; cin >> t; for (int k = 1; k <= t; k ++) { cin >> m >> n; for (int i = 1; i <= n; i ++) cin >> v[i]; for (int i = 1; i <= n; i ++) cin >> w[i]; for (int i = 1; i <= n; i ++) for (int j = m; j >= v[i]; j --) f[j] = max(f[j], f[j - v[i]] + w[i]); printf("Case #%d: %d\n", k, f[m]); memset(f, 0, sizeof f); } return 0; }
7-46 h0173. 01背包问题
#include <iostream> using namespace std; const int N = 1010; int n, m; int v[N], w[N]; int f[N]; int main() { cin >> n >> m; for (int i = 0; i < n; i ++) cin >> v[i + 1] >> w[i + 1]; for (int i = 1; i <= n; i ++) for (int j = m; j >= v[i]; j --) f[j] = max(f[j], f[j - v[i]] + w[i]); cout << f[m] << endl; return 0; }
7-47 最长公共子序列长度
#include <iostream> using namespace std; const int N = 1010; string str1, str2; int f[N][N]; int main() { cin >> str1 >> str2; for (int i = 1; i <= str1.size(); i ++) { for (int j = 1; j <= str2.size(); j ++) { if (str1[i - 1] == str2[j - 1]) f[i][j] = f[i - 1][j - 1] + 1; else f[i][j] = max(f[i - 1][j], f[i][j - 1]); } } cout << f[str1.size()][str2.size()] << endl; return 0; }
7-48 h0215.闭区间问题
#include <iostream> #include <algorithm> using namespace std; struct activity { int b, e, no; // bool operator < (activity & t) { // if (e != t.e) return e < t.e; // } }a[1010]; int n, x ,y; bool cmp (activity x, activity y) { return x.e < y.e; // if (x.e != y.e) return x.e < y.e; // else return x.b < y.b; } int main() { cin >> n; int cnt = 0, pre = 0; for (int i = 0; i < n; i ++) { cin >> a[i].b >> a[i].e; if (a[i].b > a[i].e) { int t = a[i].e; a[i].e = a[i].b; a[i].b = t; } } sort(a, a + n, cmp); for (int i = 0; i < n; i ++) { // cout << a[i].b << ' ' << a[i].e << endl; if (a[i].b > pre) { pre = a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; cnt ++; } } cout << n - cnt; return 0; }
7-49 h0206. 区间选点
#include <iostream> #include <algorithm> using namespace std; struct activity { int b, e, no; // bool operator < (activity & t) { // if (e != t.e) return e < t.e; // } }a[1010]; int n, x ,y; bool cmp (activity x, activity y) { return x.e < y.e; // if (x.e != y.e) return x.e < y.e; // else return x.b < y.b; } int main() { cin >> n; int cnt = 0, pre = -0x3f3f3f3f; for (int i = 0; i < n; i ++) { cin >> a[i].b >> a[i].e; // if (a[i].b > a[i].e) { // int t = a[i].e; // a[i].e = a[i].b; // a[i].b = t; // } } sort(a, a + n, cmp); for (int i = 0; i < n; i ++) { // cout << a[i].b << ' ' << a[i].e << endl; if (a[i].b > pre) { pre = a[i].e; // cout << a[i].b << ' ' << a[i].e << endl; cnt ++; } } cout << cnt; return 0; } Sdadaf #include <iostream> #include <map> #include <queue> using namespace std; const int MAX = 1010; int n; struct HTreeNode { char data; int weight; int parent; int lchild; int rchild; }; HTreeNode ht[MAX]; map<char, string> mp; struct NodeType { int no; char data; int weight; bool operator < (const NodeType &s) { return s.weight < weight; } }; void init() { int i; map<char, int> mp; for (int i = 0; i < n; i ++) mp[str[i]] ++; n = mp.size(); i = 0; for (auto it : mp) { ht[i].data = it.first; ht[i].weight = it.second; i ++; } for (int j = 0; j < 2 * n - 1; j ++) { ht[j].lchild = ht[j].rchild = ht[j].parent = -1; } } void createHTree() { NodeType e, e1, e2; priority_qeue<NodeType> qu; for (int i = 0; i < n; i ++) { e.no = i; e.data = ht[i].data; e.weight = ht[i].weight; qu.push(e); } for (int j = n; j < 2 * n - 1; j ++) { e1 = qu.top(); qu.pop(); e2 = qu.top(); qu.pop(); ht[j].weight = e1.weight + e2.weight; ht[j].lchild = e1.no; ht[j].rchild = e2.no; ht[e2.no].parent = j; ht[e2.no].parent = j; e.no = j; e.weight = e1.weight + e2.weight; qu.push(e); } } void CreateHCode() { string code; code reserve(MAX); } int main() { cin >> n; return 0; }