class Solution {
public:
int getUglyNumber(int n) {
vector<int> q(1, 1);
int i = 0, j = 0, k = 0;
while( -- n) // 循环 n - 1 次
{
int t = min(q[i] * 2, min(q[j] * 3, q[k] * 5));
q.push_back(t);
if(t == q[i] * 2) i ++ ;
if(t == q[j] * 3) j ++ ;
if(t == q[k] * 5) k ++ ;
}
return q.back();
}
};
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110;
int n, T;
int a[N], d[N], l[N], spend[N];
int get(int k)
{
return max(0, a[k] - d[k] * spend[k]);
}
int work(int n, int T)
{
int res = 0;
memset(spend, 0, sizeof spend);
for(int i = 0; i < T; i ++ )
{
int t = 1;
for(int j = 1; j <= n; j ++ )
if(get(t) < get(j))
t = j;
res += get(t);
spend[t] ++ ;
}
return res;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++ ) scanf("%d", &d[i]);
for(int i = 2; i <= n; i ++ )
{
scanf("%d", &l[i]);
l[i] += l[i - 1];
}
scanf("%d", &T);
int res = 0;
for(int i = 1; i <= n; i ++ )
res = max(res, work(i, T - l[i]));
printf("%d\n", res);
return 0;
}
作者:NFYD
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 210;
int n, m;
struct Person
{
string name;
int age, w;
bool operator< (const Person& t) const
{
if(w != t.w) return w > t.w;
if(age != t.age) return age < t.age;
return name < t.name;
}
};
vector<Person> ages[N];
int idx[N];
int main()
{
scanf("%d%d", &n, &m);
char name[10];
for(int i = 0; i < n; i ++ )
{
int age, w;
scanf("%s%d%d", name, &age, &w);
ages[age].push_back({name, age, w});
}
for(auto& c : ages) sort(c.begin(), c.end());
for(int T = 1; T <= m; T ++ )
{
printf("Case #%d:\n", T);
int cnt, a, b;
scanf("%d%d%d", &cnt, &a, &b);
memset(idx, 0, sizeof idx);
bool exists = false;
while(cnt -- )
{
int t = -1;
for(int i = a; i <= b; i ++ )
{
if(idx[i] < ages[i].size())
{
if(t == -1 || ages[i][idx[i]] < ages[t][idx[t]])
t = i;
}
}
if(t == -1) break;
auto& p = ages[t][idx[t]];
printf("%s %d %d\n", p.name.c_str(), p.age, p.w);
idx[t] ++ ;
exists = true;
}
if(!exists) puts("None");
}
return 0;
}
作者:NFYD
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <set>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> PII;
const int N = 55;
int m, n;
set<PII> g[N];
unordered_map<int, int> f[N];
int sum[N], cnt[N];
set<PII>::reverse_iterator it[N];
vector<int> ans[N];
int main()
{
scanf("%d%d", &m, &n);
for(int i = 1; i <= n; i ++ )
{
int id, score;
scanf("%d%d", &id, &score);
for(int j = 0; j < m; j ++ )
{
f[j][id] = score;
g[j].insert({score, -id});
}
}
int Q;
scanf("%d", &Q);
while(Q -- )
{
int t;
scanf("%d", &t);
if(t == 1)
{
int type, id, score;
scanf("%d%d%d", &type, &id, &score);
f[type][id] = score;
g[type].insert({score, -id});
}
else if(t == 2)
{
int type, id;
scanf("%d%d", &type, &id);
g[type].erase({f[type][id], -id});
f[type].erase(id);
}
else
{
int tot;
scanf("%d", &tot);
for(int i = 0; i < m; i ++ )
{
scanf("%d", &sum[i]);
it[i] = g[i].rbegin();
cnt[i] = 0;
ans[i].clear();
}
while(tot -- )
{
int k = -1;
for(int i = 0; i < m; i ++ )
{
if(it[i] != g[i].rend() && cnt[i] < sum[i])
{
if(k == -1 || it[i]->fi > it[k]->fi)
{
k = i;
}
}
}
if(k == -1) break;
ans[k].push_back(-it[k]->se);
cnt[k] ++ ;
it[k] ++ ;
}
for(int i = 0; i < m; i ++ )
{
if(ans[i].empty()) puts("-1");
else
{
for(auto x : ans[i])
{
printf("%d ", x);
}
puts("");
}
}
}
}
return 0;
}
作者:NFYD
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int l, m, n;
int a[N], b[N], c[N];
int main()
{
scanf("%d%d%d", &l, &m, &n);
for(int i = 0; i < l; i ++ ) scanf("%d", &a[i]);
for(int i = 0; i < m; i ++ ) scanf("%d", &b[i]);
for(int i = 0; i < n; i ++ ) scanf("%d", &c[i]);
LL res = 1e18;
for(int i = 0, j = 0, k = 0; i < l && j < m && k < n; )
{
int x = a[i], y = b[j], z = c[k];
res = min(res, (LL)max(max(x, y), z) - (LL)min(min(x, y), z));
if(x <= y && x <= z) i ++ ;
else if(y <= x && y <= z) j ++ ;
else k ++ ;
}
printf("%lld\n", res * 2);
return 0;
}
作者:NFYD
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1010;
int n, m, k1, k2;
int a[N], b[N], d[N];
int main()
{
scanf("%d%d%d", &n, &k1, &k2);
m = k1 + k2;
for(int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for(int i = 0; i < n; i ++ ) scanf("%d", &b[i]);
for(int i = 0; i < n; i ++ ) d[i] = abs(a[i] - b[i]);
LL res = 0;
for(int i = 0; i < m; i ++ )
{
int t = 0;
for(int j = 0; j < n; j ++ )
if(d[t] < d[j])
t = j;
if(d[t] == 0)
{
res = (m - i) % 2;
break;
}
d[t] -- ;
}
for(int i = 0; i < n; i ++ ) res += (LL)d[i] * d[i];
printf("%lld", res);
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程