【浮*光】【Codeforces Round #485 (Div. 2)】解题报告

A. Infinity Gauntlet

#include <bits/stdc++.h>
using namespace std;

/* 你偷看了Thanos身穿Infinity手镯。在Gauntlet中有六个异色宝石。
使用在Gauntlet中看到的宝石颜色,来确定缺少宝石的名称。 */

// 即求6个字符串中没有出现的字符串

string str1[6]={"purple","green","blue","orange","red","yellow"};
string str2[6]={"Power","Time","Space","Soul","Reality","Mind"};
set<string> s; //集合储存名字字符串

int main(){
    int n; cin>>n; cout<<6-n<<endl;
    while(n--){
        string aa; cin>>aa;
        s.insert(aa); //已经存在的名字
    }
    for(int i=0;i<6;i++){
        if(!s.count(str1[i])) //集合中没有
            cout<<str2[i]<<endl; //输出
    }
    return 0;
}

B. High School: Become Human

#include <bits/stdc++.h>
using namespace std;

//求x^y与y^x的大小关系
//分析》第一种做法对于x,y<=4进行特判;其余的一定是x,y中较小的在下面比较大。
//      另一种做法是取对数,但是对于x=y的情况要特判

const double ERROR=1e-10; //第二种做法

int main(){
    double x,y; cin>>x>>y; //取对数,所以用double
    double tmp1=y*log(x),tmp2=x*log(y);
    if(x==y||fabs(x-y)<ERROR) printf("=\n");
    else if(x<y) printf("<\n");    
    else printf(">\n");
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

//求x^y与y^x的大小关系 
//分析》(2,3)(2,4)以及有数是1的时候特判,其他小的做底、大的做幂结果大。

int main(){  //第一种做法
    int x,y; cin>>x>>y;
    if(x==y){ cout<<'='<<endl; return 0; }
    else if(x==1||y==1){
        if(x==1){ cout<<'<'<<endl; return 0; }
        else{ cout<<">"<<endl; return 0; }
    }
    else if((x==2&&y==3)||(x==3&&y==2)){
        if(x==2&&y==3){ cout<<"<"<<endl; return 0; }
        else{ cout<<">"<<endl; return 0; }
    }
    else if((x==2&&y==4)||(x==4&&y==2)){
        cout<<"="<<endl; return 0;
    }
    else{
        if(x<y) cout<<">"<<endl;
        else cout<<"<"<<endl;
    }
    return 0;
}
 

C. Three displays

#include <bits/stdc++.h>
using namespace std;

/* 在A数组中找出满足 1》i<j<k
2》a[i]<a[j]<a[k] 3》b[i]+b[j]+b[k]最小 的i,j,k,并输出最小的和 */

//f[i][j]表示 [以第i个display为最后一块display的]、
//[且这块display为第j块display] 情况下的最小费用。

int f[3009][5];
int main(){
    int n; cin>>n; int ans=(1<<31)-1;
    memset(f,0x3f,sizeof(f));
    int s[3005],c[3005];
    for(int i=1;i<=n;i++) cin>>s[i];
    for(int i=1;i<=n;i++) cin>>c[i];
    for(int i=1;i<=n;i++){
        f[i][1]=c[i];
        for(int j=1;j<i;j++)
            if(s[j]<s[i]){
                f[i][2]=min(f[i][2],f[j][1]+c[i]);
                f[i][3]=min(f[i][3],f[j][2]+c[i]);
            }
        ans=min(ans,f[i][3]);
    }
    if(ans>=0x3f3f3f3f||ans==((1<<31)-1))
        cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
} 


D. Fair

题意是一共有k种商品,n个商户,m条道路,如果某个商户想要达到fair状态就必须拥有至少s种商品,每个商户可以从任意其他商户那里得到该商户的商品种类,(不存在减少),花费为两者之间的最短路的道路个数,求每个商家达到fair的最小花费
注意到k只有100,我们可以计算出,每种颜色到所有商家的最短距离,这样每种商家到所有颜色的最短距离我们也就知道了,所以做k次bfs之后对每个商家取前s种花费小的颜色即可。
D题代码

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1e5+5;
int dis[maxn][105];
int a[maxn];
int ans[maxn];
int x,y,n,k,m,s;
vector<int> E[maxn];
void bfs(int x)
{
    queue<int> q;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==x)
        {
            q.push(i);
            dis[i][x]=0;
        }
    }
    while(!q.empty())
    {
        int tmp=q.front();
        q.pop();
        for(int i=0;i<E[tmp].size();i++)
        {
            int pp=E[tmp][i];
            if(dis[pp][x]==-1)
            {
                dis[pp][x]=dis[tmp][x]+1;
                q.push(pp);
            }
        }
    }
    return ;
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    while(m--)
    {
        scanf("%d%d",&x,&y);
        E[x].push_back(y);
        E[y].push_back(x);
    }
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++) dis[j][i]=-1;
        bfs(i);
    }
    for(int i=1;i<=n;i++)
    {
        sort(dis[i]+1,dis[i]+1+k);
        for(int j=1;j<=s;j++) ans[i]+=dis[i][j];
    }
    for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\n':' ');
    return 0;
}

E题.Petr and Permutations
E.Petr and Permutations
题意为给AB分别一个1-n的顺序排列,设定x操作为任意交换序列中的两个元素,A对他进行3*n次x操作,B对他进行7*n+1次x操作,给你最终序列,问你此序列是由谁操作而来的。
我们考虑原序列是顺序的,线性代数中学过,交换序列中任意两个元素,会使序列逆序对的奇偶性发生改变一次,所以我们只需要知道总操作次数的就行,然后判断最终逆序对个数的奇偶性,就结束了。
E题代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define maxn 1000005
using namespace std;
int w[maxn],sum[maxn],n;
struct T
{
    int x,num;
} a[maxn],T[maxn];
void merge_sort(int l,int r)
{
    if(r-l==1)return;
    int m=l+r>>1,tm=l+r>>1,tl=l,i=l;
    merge_sort(l,m),merge_sort(m,r);
    while(tl<m||tm<r)
    {
        if(tm>=r||(tl<m&&a[tl].x<=a[tm].x))
            T[i++]=a[tl++],T[i-1].num+=tm-m;
        else
            T[i++]=a[tm++];
    }
    for(int i=l; i<r; i++)a[i]=T[i];
}
int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i].x),a[i].num=0;
    merge_sort(0,n);
    long long ans=0;
    for(int i=0; i<n; i++)ans+=a[i].num;
    if(n%2==ans%2) printf("Petr");
    else printf("Um_nik");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/flora715/article/details/80707394