10.6
Cabbage_Squirrel · · 个人记录
T1
每次合并重量最小的两堆果子,使用小根堆存储果子,然后模拟合并的过程即可。
#include <cstdio>
#include <queue>
using std :: priority_queue;
using std :: greater;
using std :: vector;
int n, ans = 0;
priority_queue < int, vector <int>, greater <int> > q;
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
{
int x; scanf("%d", &x);
q.push(x);
}
while(q.size() > 1)
{
int x = q.top(); q.pop();
int y = q.top(); q.pop();
ans += x + y;
q.push(x + y);
}
printf("%d\n", ans);
return 0;
}
T2
这无疑是简单的,按题目的要求编写 cmp 即可。
#include <bits/stdc++.h>
using namespace std;
struct xx{
int yw;
int ss;
int yy;
int zf;
int bh;
}a[305];
int n;
bool comp(xx x,xx y)
{
if(x.zf!=y.zf)
return x.zf>y.zf;
else
if(x.yw!=y.yw)
return x.yw>y.yw;
else
return x.bh<y.bh;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].yw>>a[i].ss>>a[i].yy;
a[i].bh=i;
a[i].zf=a[i].yw+a[i].ss+a[i].yy;
}
sort(a+1,a+n+1,comp);
for(int i=1;i<=5;i++)
cout<<a[i].bh<<" "<<a[i].zf<<endl;
return 0;
}
T3
同上。
#include <bits/stdc++.h>
using namespace std;
int n;
struct node
{
string s;
int id, y, m, d;
} a[110];
bool cmp(node a, node b)
{
if (a.y == b.y)
{
if (a.m == b.m)
{
if (a.d == b.d)
return a.id > b.id;
return a.d < b.d;
}
return a.m < b.m;
}
return a.y < b.y;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].s >> a[i].y >> a[i].m >> a[i].d;
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++)
cout << a[i].s << "\n";
return 0;
}
T4
同上,不过在这里的字符串不能直接像数字一样排序,它是按字典序排的,字典序就是一位一位比较,哪个大哪个靠前,所以在位数相同的时候可以直接排序,否则就是位数多的在前面就行了。
#include <bits/stdc++.h>
using namespace std;
int n;
struct node
{
string s;
int id;
} a[110];
bool cmp(node x, node y)
{
if (x.s.size() == y.s.size())
return x.s > y.s;
return x.s.size() > y.s.size();
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].s;
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
cout << a[1].id << "\n" << a[1].s;
return 0;
}
T5
STL 大法好!
先排序,然后使用 set 一个一个存一下,最后输出 set 的长度和内容就行。
for(int i = 1;i <= n;i ++) {
cin >> a[i];
}
sort(a + 1,a + n + 1);
set<int>st;
for(int i = 1;i <= n;i ++) {
st.insert(a[i]);
}
cout << st.size() << '\n';
for(auto i:st) {
cout << i << ' ';
}
T6
STL 大法好!
因为有
然后模拟即可
map<map<int,int> >a;
int main() {
int n,q;
cin >> n >> q;
while(q --) {
int op;
cin >> op;
if(op == 1) {
int i,j,k;
cin >> i >> j >> k;
a[i][j] = k;
} else {
int i,j;
cin >> i >> j;
cout << a[i][j] << '\n';
}
}
return 0
}
T7
solution
桶加队列。开一个桶数组
code
#include<bits/stdc++.h>
using namespace std;
int n, ans, num[100005];
struct node{ int x, t; } h;
queue<node> q;
int main(){
cin >> n;
for(int i=1, t, k; i<=n; i++){
cin >> t >> k;
for(int j=1, x; j<=k; j++)
cin >> x,
q.push({x, t}),
ans+=(++num[x]==1);
for (h=q.front(); q.size()&&h.t+86400<=t; q.pop(), h=q.front())
ans-=(--num[h.x]==0);
cout << ans << '\n';
}
return 0;
}
T8
由于采用后缀表达方式,所以在读到运算符号时可以无条件运算,先将整个表达式一起读入,接下来:
- 如果碰到数字,将其放入临时数组当中,方便以后求数字
- 如果碰到'.',那么将临时数组中的字符串转化成数字(具体操作看程序注释),将其压入数字栈中
- 如果碰到运算符号,那么将栈顶的两个元素取出做相应的运算(注意:如果碰到-'或'/'应该用栈顶第二个元素减或除以栈顶元素!!!)
- 最后输出数字栈中剩余的最后一个元素即可