0
点赞
收藏
分享

微信扫一扫

班级活动(哈希表统计)

孟祥忠诗歌 2024-11-18 阅读 13

A. Penchick and Modern Monument

翻译:

思路:

实现:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
// using pll = pair<ll,ll>;
 
void solve(){
  int n;
  int maxx = 0;  
  vector<int> nums(100,0);
  cin>>n;
  for (int i=0,num;i<n;i++){
    cin>>num;
    nums[num]++;
    maxx = max(maxx,nums[num]);
  }  
  cout<<n-maxx<<endl;
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // 中间填保留几位小数,不填默认
    // cout.precision();
    ll t;cin>>t;
    while (t--) solve();
    
}

B. Penchick and Satay Sticks

 翻译:

思路:

实现:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
// using pll = pair<ll,ll>;
 
void solve(){
  int n;    
  cin>>n;
  vector<int> a(n);
  for (int i=0;i<n;i++) cin>>a[i];
  if (n==2||n==1){
    cout<<"YES"<<endl;
    return;
  }else{
    int maxx = a[0];
    for (int i=1;i<n;i++){
      if (maxx>a[i]+1){
        cout<<"NO"<<endl;
        return;
      }
      maxx = max(a[i],maxx);
    }
    cout<<"YES"<<endl;
  }
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // 中间填保留几位小数,不填默认
    // cout.precision();
    ll t;cin>>t;
    while (t--) solve();
}

C. Penchick and BBQ Buns

 翻译:

思路:

实现:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
// using pll = pair<ll,ll>;
 
void solve(){
  int n;  
  cin>>n;
  vector<int> a(n+1,0);
  if (n%2==1){
    if (n>=27){
      a[1] = 1;a[10] = 1;a[26] = 1;a[11] = 2;a[27] = 2;
      int cnt = 3,f = 0;
      for (int i=1;i<=n;i++){
        if (a[i]==0){
          a[i] = cnt;
          f++;
        }
        if (f==2){
          f = 0;
          cnt++;
        }
      }
    }else{
      cout<<-1<<endl;
      return;
    }
  }else{
    int cnt = 1;
    for (int i=1;i<=n;i+=2){
      a[i] = cnt;
      a[i+1] = cnt;
      cnt++;
    }
  }
  for (int i=1;i<n;i++){
      cout<<a[i]<<" ";
    }
  cout<<a[n]<<endl;
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // 中间填保留几位小数,不填默认
    // cout.precision();
    ll t;cin>>t;
    while (t--) solve();
    
}

D. Penchick and Desert Rabbit

翻译:

思路:

代码:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
// using pll = pair<ll,ll>;
const int N = 5e5+10;
vector<int> f(N),sz(N);
int find(int k){
  return f[k]==k ? f[k] : f[k] = find(f[k]);
}
void add(int x,int y){
  x = find(x);
  y = find(y);
  if (x==y) return;
  if (sz[x]<sz[y]) swap(x,y);
  f[y] = x;
  sz[x] += sz[y];
}

int n;
vector<int> nums(N);

void solve(){
  int n;cin>>n;
  for (int i=1;i<=n;i++){
    cin>>nums[i];
    sz[i] = 1;
    f[i] = i;
  }
  priority_queue<pair<int,int>> piece1;
  for (int i=1;i<=n;i++){
    int temp = nums[i];
    while (!piece1.empty()){
      int x = piece1.top().first, y = piece1.top().second;
      if (x>nums[i]){
        temp = max(x,temp);
        add(y,i);
        piece1.pop();
      }else{
        break;
      }
    }
    piece1.push(make_pair(temp,find(i)));
  }
  map<int,int> mp;
  while (!piece1.empty()){
    int x = piece1.top().first, y = piece1.top().second;
    piece1.pop();
    mp[find(y)] = x;
  }
  for (int i=1;i<n;i++){
    cout<<mp[find(i)]<<" ";
  }
  cout<<mp[find(n)]<<endl;
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // 中间填保留几位小数,不填默认
    // cout.precision();
    ll t;cin>>t;
    while (t--) solve();
    
}
举报

相关推荐

0 条评论