周日J1R-东塘-404

· · 个人记录

T676960 月色的排队

新知识

用结构体数组一定要用自定义函数:
struct node{
  int s;
  double h;
}a[2010];
bool cmp(node a,node b){
  return a.h<b.h;
}

思路

先定一个结构体node:struct node{ int s; double h;}a[2010];再来个自定义函数cmp;然后定两个整数变量n,t,在输入t,再来个while循环:while(t--){}里面先写输入n,在输入a[i].s和a[i].h,排个序最后来两个for循环判断。

代码

    #include<bits/stdc++.h>
using namespace std;
struct node{
    int s;
    double h;
}a[2010];
bool cmp(node a,node b){
    return a.h<b.h;
}
int n,t;
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i].s;
        }
        for(int i=1;i<=n;i++){
            cin>>a[i].h;
        }
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++){
            if(a[i].s==0){
                cout<<a[i].h<<' ';  
            }
        }
        cout<<endl;
        for(int i=1;i<=n;i++){
            if(a[i].s==1){
                cout<<a[i].h<<' ';  
            }
        }
        cout<<endl;
    }

    return 0;
}

B3680 [语言月赛202211] BAN-PICK

思路

先定一个结构体struct node{ string s; long long d; }a[1000010],b[1000010];再来一个自定义函数,然后定两个变量n,m;在输入n,m再定两个变量l1=0,l2=0再来一个for循环,然后拍两个序,最后输出。

代码

#include<bits/stdc++.h>
using namespace std;
struct node{
    string s;
    long long d;
}a[1000010],b[1000010];
bool cmp(node a,node b){
    return a.d>b.d;
}
int n,m;
int main(){
    cin>>n>>m;
    int l1=0,l2=0;
    for(int i=1;i<=n+m;i++){
        string s;
        char c;
        long long d;
        cin>>s>>c>>d;
        if(c=='H'){
            l1++;
            a[l1].s=s;
            a[l1].d=d;  
        }else{
            l2++;
            b[l2].s=s;
            b[l2].d=d;
        }
    }
    sort(a+1,a+m+1,cmp);
    sort(b+1,b+n+1,cmp);
    cout<<a[3].s<<endl;
    cout<<b[6].s<<endl;
    cout<<b[7].s<<endl;  
    cout<<b[8].s<<endl;
    cout<<b[9].s<<endl;  
    return 0;
}

B3679 [语言月赛202211] Zone Selection

思路

先来个结构体和一个自定义函数,再定三个变量n,k,t,再来两个输入for循环,和一个嵌套循环,再来个while循环,最后输出。

代码

    #include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
    int id;
    int cnt;
    int d;
}a[1010];
bool cmp(node a,node b){
    if(a.d!=b.d){
        return a.d>b.d;
    }
    return a.id<b.id;
}
int n,k,t;
int main(){
    cin>>n>>k>>t;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        a[i].id=i;
        a[i].cnt=0;
    }
    for(int i=1;i<=k;i++){
        int x,y;
        cin>>x>>y;
        for(int j=1;j<=n;j++){
            if(a[j].x==x && a[j].y==y){
                a[j].cnt=1;
            }
        } 
    }
    int ans=0;
    while(t--){
        int x,y;
        cin>>x>>y;
        for(int i=1;i<=n;i++){
            a[i].d=(a[i].x-x)*(a[i].x-x)+(a[i].y-y)*(a[i].y-y);
        }
        sort(a+1,a+n+1,cmp);
        if(a[1].cnt==1){
            ans++;
        } 
    }
    cout<<ans;
    return 0;
}