2021牛客暑期多校训练营4

F题 -Just a joke``

Alice and Bob are playing a game.
At the beginning, there is an undirected graph G with n nodes.
Alice and Bob take turns to operate, Alice will play first. The player who can’t operate will lose the game.
Each turn, the player should do one of the following operations.

  1. Select an edge of G and delete it from G.
  2. Select a connected component of G which doesn’t have any loop, then delete it from G.
    Alice and Bob are smart enough, you need to find who will win this game.
    A connected component of an undirected graph is a set of nodes such that each pair of nodes is connected by a path, and other nodes in the graph are not connected to the nodes in this set.
    For example, for graph with 33 nodes and edge set {(1,2),(2,3),(1,3)}.{1,2,3} is a connected component but {1,2},{1,3}{1,2},{1,3} are not.

输入描述:
The first line has two integers n,m.

Then there are mm lines, each line has two integers (u,v)(u,v) describe an edge in G.

1≤ n≤ 1001≤n≤100

0≤m≤min(200,n(n−1)/2)

It’s guaranteed that graph G doesn’t have self loop and multiple edge.
输出描述:
Output the name of the player who will win the game.

输入
3 1
1 2

输出
Bob

#include<bits/stdc++.h>
using namespace std;
template<class T>inline void read(T &res){
        char c;T flag=1;
        while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
        while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}

int main(){
  int n,m;
  read(n);
  read(m);
  for(int i = 1 ; i <= m; i++){
    int x,y;
    read(x);
    read(y);
  }
  if((n+m)%2) puts("Alice");
  else puts("Bob");
	return 0;
}

C题-LCS

#include<bits/stdc++.h>
using namespace std;
template<class T>inline void read(T &res){
        char c;T flag=1;
        while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
        while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}

int main(){
  int a,b,c,n;
  read(a);read(b);read(c);read(n);
  int m=min(a,b);
  int mm=min(m,c);
  
    a-=mm;b-=mm;c-=mm;
  
  if(a+b+c+mm>n){
    puts("NO");
    return 0;
  }
  
  string s1(mm,'a'); 
  string s2(mm,'a');
  string s3(mm,'a');

  while(a--){
    s1.push_back('b');
    s2.push_back('b');
  }
  
   while(b--){
    s2.push_back('c');
    s3.push_back('c');
  }
  
   while(c--){
    s1.push_back('d');
    s3.push_back('d');
  }
  
  while(s1.size()<n) s1.push_back('x');
  while(s2.size()<n) s2.push_back('y');
  while(s3.size()<n) s3.push_back('z');
  
  cout<<s1<<endl;
  cout<<s2<<endl;
  cout<<s3<<endl; 
	return 0;
}

I题-Inverse Pair

题目描述
For a sequence t 1…n, we define the weight of it is the number of pairs (i,j)(i,j) satisfy it j
Now give you a permutation a 1…n, you need to choose a sequence b 1…n satisfies b i ∈{0,1} to minimize the weight of sequence c_{1…n} which satisfies c i=a i+b i

输入描述:
The first line has one integer n.

The second line has n integers a 1…n
It’s guaranteed that a i is a permutation of {1,2…n}
1≤n≤2×10 5

输出描述:
Output the minimum weight of c 1…n you can get.

输入
5
4 3 2 5 1

输出
5

思路:利用归并排序或树状数组来求逆序对的个个数,满足i<j&&a[i]==a[j]+1时逆序对-1

#include<bits/stdc++.h>
using namespace std;
template<class T>inline void read(T &res){
        char c;T flag=1;
        while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
        while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
const int N = 2e5+5;
 
typedef long long ll;
ll a[N],d[N],t[N];
ll n;
ll lowbit(ll x){
  return x&(-x);
}
bool cmp(int x,int y){
  if(a[x]==a[y]) return x>y;
  return a[x]>a[y];
}
 
void add(ll x){
  while(x<=n){
    t[x]++;
    x+=lowbit(x);
  }
}
 
ll sum(int x){
  ll res=0;
  while(x>=1){
    res+=t[x];
    x-=lowbit(x);
  }
  return res;
}
int main(){
  ll ans=0;
  read(n);
  for(int i = 1 ; i <= n ; i++){
    cin>>a[i];
    ans+=sum(a[i]);
    add(a[i]);
    d[a[i]]=i;
  }
  int t = 1;
  ans = n*(n-1)/2 -ans;
  while(t<n){
    if(d[t]>d[t+1]) {
      ans--;
      t+=2;
    }
    else t+=1;
  }
//  sort(d+1,d+1+n,cmp);
//  for(int i = 1 ; i <= n ; i++){
//   
//  }
  cout<<ans<<endl;
    return 0;
}

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值