1.机械加工
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int d, w, p;
}a[200010];
int T, n, m, k;
bool cmp(node x, node y)
{
return x.p < y.p;
}
int main()
{
cin >> T >> m >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i].d >> a[i].w >> a[i].p;
if (k) a[i].p += T - a[i].d;
}
sort(a + 1, a + n + 1, cmp);
long long ans = 0;
for (int i = 1; i <= n; i++)
{
if (m > a[i].w)
{
ans += 1ll * a[i].p * a[i].w;
m -= a[i].w;
}
else
{
ans += m * a[i].p;
break;
}
}
cout << ans << endl;
return 0;
}
2.猫咪照相
#include <iostream>
#include <algorithm>
using namespace std;
int n, k, a[110], b[110], w[20], ans;
bool check()
{
for (int r = 2; r <= n; r++)
{
int x = w[r], y = w[r - 1];
for (int i = 1; i <= k; i++)
if (a[i] == x && b[i] == y || b[i] == x && a[i] == y)
return false;
}
return true;
}
int main()
{
cin >> n >> k;
for (int i = 1; i <= k; i++)
{
cin >> a[i] >> b[i];
}
for (int i = 1; i <= n; i++)
{
w[i] = i;
}
do
{
if (check())
{
ans++;
}
}while (next_permutation(w + 1, w + n + 1));
cout << ans << endl;
return 0;
}
3.愚公移山
#include <bits/stdc++.h>
using namespace std;
int n, ans;
void dfs(int x, int last, int sum)
{
if (sum==n)
{
if (x-1<2)
{
return;
}
ans++;
return;
}
for (int i=last+1; i<=n-sum; i++)
{
dfs(x+1, i, sum+i);
}
}
int main()
{
cin >> n;
dfs(1, 0, 0);
cout << ans << endl;
return 0;
}
4.爱探险的小明
#include <iostream>
#include <algorithm>
using namespace std;
int n;
string s, t[100010];
bool cmp(string x, string y)
{
return x + y > y + x;
}
int main()
{
cin >> s;
s = "a" + s + "a";
for (int i = 1; i < s.size(); i++)
if (isdigit(s[i]))
{
if (islower(s[i - 1])) n++;
t[n] += s[i];
}
sort(t + 1, t + n + 1, cmp);
for (int i = 1; i <= n; i++) cout << t[i];
cout << endl;
return 0;
}
5.递增三元组
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[100010], b[100010], c[100010];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
cin >> b[i];
}
for (int i = 1; i <= n; i++)
{
cin >> c[i];
}
sort(a + 1, a + n + 1);
sort(c + 1, c + n + 1);
long long ans = 0;
for (int i = 1; i <= n; i++)
{
int x = upper_bound(a + 1, a + n + 1, b[i]) - a;
int y = lower_bound(c + 1, c + n + 1, b[i]) - c;
ans += 1ll * (x - 1) * (n - y + 1);
}
cout << ans << endl;
return 0;
}