QFNU-ACM 2020.04.12个人赛

A题:

代码:

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
inline int read(){
    int x=0,y=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            y=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*y;
}
struct book{
    int index,x;
    
}g[MAXN];
bool cmp(struct book z, struct book y){
    return z.x < y.x;
}
int main(){
    int n,a[MAXN],flag=0;
    n=read();
    if(n==1){
        printf("-1\n");
        return 0;
    }
    if(n==2)
        flag=1;
    for(int i = 1;i<=n;i++){
        g[i].x=read();
        g[i].index=i;
    }
    sort(g+1,g+1+n,cmp);
    if(flag){
        if(g[1].x==g[2].x){
            printf("-1\n");
            return 0;
        }
    }
    printf("%d\n",n-1);
    for(int i = 2;i<=n;i++){
        if(i!=2)
            printf(" ");
        
        printf("%d",g[i].index);
    }
    return 0;
}

D题:

题意:

给出n个数,要求找出含有多少对不同的(x , y) 其中 x 在 y 的左边(给出的数组的相对位置)。

思路:

可以用一个数组 b 来存储 x 后面不同数的个数,再用一个map来记录 x 是否已经被使用,中途需要开ll,加和可能爆int

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
using namespace std;
const long long N = 1e9 + 7;
typedef long long ll;
map<ll,ll> ma;
set<ll> s;
ll a[100010],b[100010];
int main()
{
    int n;
    ll sum = 0;
    cin >> n;
    for(int i = 0;i < n ;i++ )
    {
        cin >> a[i];
    }
    for(int i = n-1;i >= 0;i--)
    {
        b[i] = s.size();   //记录a[i]后面又多少个不相等的数
        s.insert(a[i]);

    }
    for(int i = 0;i < n;i++)
    {
        if(ma[a[i]] == 0)   //证明这个数在之前没被使用
            sum += b[i];
        ma[a[i]] = 1;  //记录a[i] 已经使用
    }
    cout << sum << endl;
    return 0;
}

B题:

代码:

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
inline int read(){
    int x=0,y=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            y=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*y;
}
struct book{
    int index,x;
    
}g[MAXN];
bool cmp(struct book z, struct book y){
    return z.x < y.x;
}
int main(){
    int  n,m,a[MAXN],p=0,res=0,in=0,count[MAXN];
    n=read();m=read();
    for(int i = 1; i <= n;i++){
        a[i]=read();
    }
    for(int i = 1;i <= n;i++){
        if(a[i]%2){
            p++;
        }
        else{
            p--;
        }
        if(p==0&&i!=n){
            
            count[in++] += abs(a[i+1]-a[i]);
        }    
    }
    int i=0;
    sort(count,count+in);
    while(i<in){
        m -= abs(count[i]);
        if(m<0){
            break;
        }
        i++;
        res++;
    }
    cout<<res<<endl;
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/pioneerjiesen/p/12730241.html