cf1163B2. Cat Party (Hard Edition)(简单总结一下map数据结构的简单方法)

题目链接:https://codeforces.ml/contest/1163/problem/B2

题意:n和一个长度为n的a 数组(1<=n<=1e5,1<=ai<=1e5)。

问最大的a数组前缀长度(要求满足去除一个数之后剩下的每个数的出现次数相同)。(nmd我读题都不认真emmm,读的不太懂就去划水了,尼玛今天才注意自己打cf8个月了,最高才1596,目前1389)。

要好好准备一下打cf了,不要总做简单题,要面向xcpc编程。好好训练自己应对比赛的能力。

题解:用一个map维护就好了(不是特别熟练它的各种用法,今天调试了一下,1.mp.erase(x)代表删除值为x的map,2.如果之前赋值mp[1]=1,现在mp[1]=0,如果不用mp.erase(1)那么它还会留在里面(赋过值不删除就会一直在里面)。3.map<ll,ll>::iterator p;p=mp.begin();p->fi,p->se;这些基本操作)。

在每次判断的时候,应该思路清晰!!!(思路清晰,思路清晰,真的超级重要!!!)

代码:

#include <bits/stdc++.h>

#define ll long long
#define pi acos(-1)
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
template <class T> void read(T &x) {
    T res = 0, f = 1;
    char c = getchar();
    while (!isdigit(c)) {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (isdigit(c)) {
        res = (res << 3) + (res << 1) + c - '0';
        c = getchar();
    }
    x = res * f;
}
void print(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
const ll maxn = 1e5 + 10;
const ll mod = 1e9 + 7;

ll n, a[maxn], b[maxn];
map<ll,ll> mp; 
map<ll,ll>::iterator p1,p2;
// ll gcd(ll a,ll b){return (b==0)?a:gcd(b,a%b);}
// ll qpow(ll a,ll p,ll mod){ll
// ans=1;a=a%mod;while(p){if(p&1)ans=(ans*a)%mod;p>>=1;a=(a*a)%mod;}return ans;}
int main() {
    ll _s = 1;
    // read(_s);
    for (ll _ = 1; _ <= _s; _++) {
        read(n);
        for(ll i=1;i<=n;i++) read(a[i]);
        ll ans=0;
        bool f;
        for(ll i=1;i<=n;i++){
            b[a[i]]++;
            f=true;
            if(b[a[i]]>1){
                mp[b[a[i]]-1]--;
                if(mp[b[a[i]]-1]==0) mp.erase(b[a[i]]-1);
            }
            mp[b[a[i]]]++;
            if(mp.size()==1){
                p1=mp.begin();
                if(p1->fi!=1&&p1->se!=1) f=false;
            }
            else if(mp.size()==2){
                p1=mp.begin();
                p2=--mp.end();
                if((p1->fi==1&&p1->se==1)||(p2->fi==p1->fi+1&&p2->se==1)) f=true;
                else f=false;
            }
            else f=false;
            if(f) ans=i;
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
input:::

output:::

*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值