1.节日礼物
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int p, a;
} t[10010];
int n, m, ans;
bool cmp(node x, node y)
{
return x.p < y.p;
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= n; i++)
cin >> t[i].p >> t[i].a;
sort(t + 1, t + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
if (m > t[i].p * t[i].a)
{
ans += t[i].a;
m -= t[i].p * t[i].a;
}
else
{
ans += m / t[i].p;
break;
}
}
cout << ans << endl;
return 0;
}
2.股票价格
#include <iostream>
using namespace std;
int n, m, a[200010], s[200010];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
s[i] = s[i - 1] + (a[i - 1] > a[i] && a[i] < a[i + 1]);
}
cin >> m;
while (m--)
{
int l, r;
cin >> l >> r;
if (l == r)
{
cout << 0 << endl;
}
else
{
cout << s[r - 1] - s[l] << endl;
}
}
return 0;
}
3.排队办理
#include <iostream>
#include <queue>
using namespace std;
int n, m, k, t;
queue<long long> q[10010];
long long s[10010], ans;
int main()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> t;
int idx = 1;
for (int j = 1; j <= m; j++)
{
if (i <= m*k && q[j].size() < q[idx].size())
{
idx = j;
}
if (i > m*k && q[j].front() < q[idx].front())
{
idx = j;
}
}
s[idx] += t;
q[idx].push(s[idx]);
if (i > m*k)
{
q[idx].pop();
}
ans = max(ans, s[idx]);
}
cout << ans << endl;
return 0;
}
4.礼物交换
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n, k, a[N], ans[N];
int t[N];
bool vis[N];
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
if (vis[i])
{
continue;
}
int m = 0, p = i;
while (!vis[p])
{
vis[p] = 1;
t[m++] = p;
p = a[p];
}
for (int j = 0; j < m; j++)
{
ans[t[(k%m+j)%m]] = t[j];
}
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << " ";
}
return 0;
}