牛客练习赛50 A B C

文章目录

  • A
  • B
  • C

A

签到

#include
using namespace std;
#define ll long long
char vis[200];
int main()
{
    ios::sync_with_stdio(false);
    string s;
    int n;
    cin>>n>>s;
    vis[s[0]] = 1;
    for (int i = 1;i < n;i ++)
    {
    	if (vis[s[i]] && s[i-1] != s[i])
    	{
    		cout<<"NO"<<'\n';
    		return 0;
    	}
    	vis[s[i]] = 1;
    }
    cout<<"YES"<<'\n';
    return 0;
}

B

并查集维护hash表中每一位的最近的空位,特别的,f[n] = 0

#include
using namespace std;
#define ll long long
const int maxn = 1e6 + 10;
int f[maxn],ans[maxn];
int find(int x)
{
	if (x != f[x]) f[x] = find(f[x]);
	return f[x];
}
void join(int x,int y)
{
	if (x != f[x])
	{
		join(f[x],y);
		f[x] = y;
	}
}
int main()
{
    ios::sync_with_stdio(false);
    // freopen("out.txt","r",stdout);
    int n,x;
    cin>>n;
    for (int i = 0;i < n;i ++) f[i] = i;
    f[n] = 0;
	for (int i = 0;i < n;i ++)
	{
		cin>>x;
		int y = find(x%n);
		ans[y] = x;
		join(x%n,find(y+1));
		f[y] = f[y+1];
	}
	cout<<ans[0];
	for (int i = 1;i < n;i ++)
		cout<<' '<<ans[i];
	cout<<'\n';
    return 0;
}

C

考虑优先s从大到小排序,把v扔到set里面,对于第 i 个士兵,起到限制人数的一定是第 i 个士兵的 s,一旦超过限制,我们就扔掉战力值小的

#include
using namespace std;
#define ll long long
const int maxn = 1e5 +10;
struct node
{
	ll v,s;
}a[maxn];
bool cmp(node x,node y)
{
    return x.s > y.s;
}
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for (int i = 1;i <= n;i ++)
    	cin>>a[i].v>>a[i].s;
    sort(a+1,a+n+1,cmp);
    ll ans = 0,cnt = 0;
    multiset<ll> st;
    for (int i = 1;i <= n;i ++)
    {
        st.insert(a[i].v);
        cnt += a[i].v;
        while (st.size() > a[i].s)
        {
            cnt -= *st.begin();
            st.erase(st.begin());
        }
        ans = max(ans,cnt);
    }
    cout<<ans<<'\n';
    return 0;
}

你可能感兴趣的:(牛客网)