第一题:
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int a[N];
int main(){
int n;scanf("%d",&n);
for(int i=0;i<n;i++){
int x;scanf("%d",&x);
a[x]++;
if(i==0) cout<<a[x];
else cout<<" "<<a[x];
}
return 0;
}
第二题:
#include<bits/stdc++.h>
using namespace std;
const int N=550;
int a[N][N];
int main(){
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
int x=1,y=1;
bool flag=false;
while(x!=n||y!=n){
if(x<=n&&y<=n)
cout<<a[x][y]<<" ";
if(flag) x++,y--;
else x--,y++;
if(x==0){
flag=true;
x=1;
}
if(y==0){
flag=false;
y=1;
}
}
cout<<a[x][y]<<endl;
return 0;
}
第三题:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5010;
struct P{
int type;
double p;
int s;
bool is_del;
bool operator<(const P &t) const{
return p<t.p;
}
}a[N],b[N],tot[N];
int cnt;
int t1,t2;
ll as[N],bs[N];
int main(){
//freopen("1.txt","r",stdin);
string ss;
while(cin>>ss){
double p;int s;
if(ss=="buy"){
scanf("%lf%d",&p,&s);
tot[++cnt]={1,p,s};
}
else if(ss=="sell"){
scanf("%lf%d",&p,&s);
tot[++cnt]={2,p,s};
}
else{
int x;scanf("%d",&x);
tot[x].is_del=true;
tot[++cnt].is_del=true;
}
}
set<double> se;
for(int i=1;i<=cnt;i++){
if(tot[i].is_del) continue;
if(tot[i].type==1) a[++t1]=tot[i];
else b[++t2]=tot[i];
se.insert(tot[i].p);
}
sort(a+1,a+t1+1);
sort(b+1,b+t2+1);
for(int i=1;i<=t1;i++) as[i]=as[i-1]+(ll)a[i].s;
for(int i=1;i<=t2;i++) bs[i]=bs[i-1]+(ll)b[i].s;
set<double>::iterator it;
int p1=0,p2=0;
ll ress=0;
double resp=0;
for(it=se.begin();it!=se.end();it++){
double ans=*it;
while(p1+1<=t1&&a[p1+1].p<ans) p1++;
while(p2+1<=t2&&b[p2+1].p<=ans) p2++;
ll cnts=min(bs[p2],as[t1]-as[p1]);
if(cnts>=ress)
ress=cnts,resp=ans;
}
printf("%.2lf %lld\n",resp,ress);
return 0;
}
第四题:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct edge{
int x,y,w;
edge(int x=0,int y=0,int w=0):x(x),y(y),w(w){}
bool operator<(const edge &t)const {
return w<t.w;
}
}e[N];
int n,m;
int fa[N];
int getfather(int x){
if(x==fa[x]) return x;
else return fa[x]=getfather(fa[x]);
}
int kruscal(){
int ans=0;
sort(e+1,e+1+m);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++){
int fa1=getfather(e[i].x);
int fa2=getfather(e[i].y);
if(fa1!=fa2){
fa[fa1]=fa2;
ans+=e[i].w;
}
}
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
printf("%d\n",kruscal());
return 0;
}
第五题:
待补充