快速排序模板
// 注意cmp按sort的写 等号的时候一律交换
// 比如降序 return a<=b; sort中是 return a<b
bool cmp1(node a, node b) {
if (a.wi < b.wi)
return true;
else if (a.wi == b.wi) {
if (a.pi >= b.pi)
return true;
else
return false;
}
return false;
}
void mysort(int left, int right, node *a, bool (*cmp)()) {
int i = left, j = right;
node temp = a[i];
if (left >= right) return;
while (i < j) {
while (i < j && cmp(temp, a[j]) == true) {
j--;
}
a[i] = a[j];
while (i < j && cmp(a[i], temp) == true) {
i++;
}
a[j] = a[i];
}
a[i] = temp;
mysort(left, i - 1, a);
mysort(i + 1, right, a);
}
// 调用:
int main() {
mysort(0, n-1, a, cmp1);
return 0;
}
删数问题:
#include <cstring>
#include <iostream> //百度解
using namespace std;
int main() {
char str[111]; //用字符串存储数字
int s, i, j; //删掉的数字个数
while (~scanf("%s %d", str, &s)) {
while (s--) {
for (i = 0; i < strlen(str) - 1; i++) {
if (str[i] > str[i + 1]) break;
}
//删除前一个数
for (j = i; j < strlen(str); j++)
str[j] = str[j + 1]; //从该数开始,后一个数字覆盖前一个
}
//跳过前几位的'0'
for (i = 0; i < strlen(str) - 1 && str[i] == '0'; i++)
;
for (; i < strlen(str); i++) printf("%c", str[i]);
printf("\n");
}
return 0;
}
懒虫小鑫:
// #include <algorithm>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int wi;
int pi;
} node;
int main() {
int n, m, i, j;
node temp, a[10001];
while (~scanf("%d %d", &n, &m)) {
for (i = 0; i < n; i++) scanf("%d %d", &a[i].wi, &a[i].pi);
for (i = 0; i < n - 1; i++) {
int k = i;
for (j = i + 1; j < n; j++) {
if (a[k].wi > a[j].wi)
k = j;
else if (a[k].wi == a[j].wi && a[k].pi < a[j].pi)
k = j;
}
if (i != k) {
node t = a[i];
a[i] = a[k];
a[k] = t;
}
}
int cnt = 0;
i = 0;
while (m--) {
cnt += a[i].pi;
i++;
}
printf("%d\n", cnt);
}
return 0;
}
活动选择
#include <algorithm>
#include <iostream>
using namespace std;
struct node {
int start;
int tend;
int id; //活动序号
int select; //是否选择该活动
};
int main() {
node t, act[111];
int i, j, n;
cin >> n;
for (i = 0; i < n; i++) {
cin >> act[i].start >> act[i].tend;
act[i].id = i + 1;
act[i].select = 0;
}
//以结束时间的大小对活动进行排序
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (act[i].tend > act[j].tend) {
t = act[i];
act[i] = act[j];
act[j] = t;
}
}
}
int temp = 0; //初始化第一个活动的前一活动(假设)结束时间为0
for (i = 0; i < n; i++) {
if (act[i].start >= temp) {
act[i].select = 1;
temp = act[i].tend;
}
}
for (i = 0; i < n; i++) {
if (i == 0)
cout << act[i].id;
else if (act[i].select == 1)
cout << ',' << act[i].id;
}
return 0;
}
区间覆盖:
#include <algorithm>
#include <iostream>
using namespace std;
struct node {
int start;
int tend;
};
bool down(int a, int b) { return a > b; }
int main() {
int i, n, t, m;
int a[522], b[5222];
node len[5222];
while (cin >> n >> m) {
for (i = 0; i < n; i++) cin >> a[i];
//对输入的数字进行升序排序
sort(a, a + n);
int t = a[n - 1] - a[0] + 1;
for (i = 0; i < n; i++) {
len[i].start = a[i] - 1;
len[i].tend = a[i];
}
for (i = 0; i < n - 1; i++) b[i] = len[i + 1].start - len[i].tend;
sort(b, b + n - 1, down); //对间隔距离进行降序排序
for (i = 0; i < m - 1;
i++) // m条线就要进行m-1次分割,每一次分割删除最大的间隔区间
t -= b[i];
cout << t << endl;
}
return 0;
}
最少拦截:
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int c, i, j;
while (~scanf("%d", &c)) {
int flag = 0, a[30001], b[30001] = {0};
int cnt = 1;
for (i = 0; i < c; i++) {
scanf("%d", &a[i]);
}
b[0] = a[0];
for (i = 1; i < c; i++) {
for (j = 0; b[j] != 0; j++) {
if (a[i] <= b[j]) {
flag = 1;
break;
} else if (a[i] > b[j])
flag = 0;
}
b[j] = a[i];
if (!flag) {
cnt++;
}
}
printf("%d\n", cnt);
}
return 0;
}
最长上升子序列
#include <algorithm>
#include <iostream>
using namespace std;
int n;
int a[1001], dp[1001];
void solve() {
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int j = 1; j < i; j++) {
if (a[j] < a[i]) {
dp[i] = max(dp[i], dp[j] + 1); //化解最小问题解决
}
}
}
sort(dp + 1, dp + n + 1);
cout << dp[n] << endl;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
solve();
return 0;
}
最长公共子序列:
#include <cstring>
#include <iostream>
using namespace std;
char str1[1000], str2[1000];
int dp[1000][1000];
void solve() {
for (int i = 0; i < strlen(str1); i++) {
for (int j = 0; j < strlen(str2); j++) {
if (str1[i] == str2[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
}
}
cout << dp[strlen(str1)][strlen(str2)] << endl;
}
int main() {
while (~scanf("%s %s", str1, str2)) {
solve();
}
return 0;
}
递归的函数
#include <iostream>
using namespace std;
int s[31][31][31] = {0};
int f(int a, int b, int c);
int main() {
int a, b, c;
while (cin >> a >> b >> c) {
// memset(s, 0, sizeof(s));
cout << f(a, b, c) << endl;
}
return 0;
}
int f(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) return 1;
if (a > 20 || b > 20 || c > 20) return f(20, 20, 20);
if (s[a][b][c]) return s[a][b][c];
if (a < b && b < c)
s[a][b][c] = f(a, b, c - 1) + f(a, b - 1, c - 1) - f(a, b - 1, c);
else
s[a][b][c] = f(a - 1, b, c) + f(a - 1, b - 1, c) + f(a - 1, b, c - 1) -
f(a - 1, b - 1, c - 1);
return s[a][b][c];
}