Codeforces Round 988 Div. 3 题解

发布于 2024-11-18  273 次阅读


题目链接:https://codeforces.com/contest/2037

A Twice

从数组a中找到相同的两数组数,总数为得分

其中若有数出现多次,不可重复记录,每队下标不得重复

使用f[i]数组记录数i出现次数

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
	int t, n;
	int sum, maxx;
	int a[30], f[100000];
	cin >> t;
	while (t --) {
		cin >> n;
		sum = 0;
		maxx = 0;
		memset(a, 0, sizeof(a));
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; i ++) {
			cin >> a[i];
			maxx = max(a[i], maxx);
			f[a[i]] ++;
		}
		for (int i = 1; i <= maxx; i ++) {
			sum += f[i] / 2;
		}
		cout << sum << "\n";
	}
	return 0;
}

B Intercepted Inputs

从数组a中找到相同的两个值,满足两数之积为k-2

即满足从 k个数中选择两数n、m组成网格能放下剩余k-2个数

使用time[i]数据记录对应数字i出现次数,来检验对应的整数n/a[i]是否能组成合适网格

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	int t, k;
	int a[200010], time[200010];
	cin >> t;
	while (t --) {
		memset(a, 0, sizeof(a));
		memset(time, 0, sizeof(time));
		cin >> k;
		int n = k - 2;
		for (int i = 1; i <= k; i ++) {
			cin >> a[i];
			time[a[i]] ++;
		}
		for (int i = 1; i <= k; i ++) {
			if (a[i] * a[i] == n && time[a[i]] >= 2) {
				cout << a[i] << " " << a[i] << "\n";
				break;
			}
			else if (a[i] * a[i] != n && n % a[i] == 0 && time[n / a[i]]) {
				cout << a[i] << " " << n / a[i] << "\n";
				break;
			}
		}
	}
	return 0;
}

C Superultra's Favorite Permutation

满足相邻两数为合数,可将奇数偶数分组

奇数之和为合数,偶数之和为合数,交界处固定5和4也满足合数

奇数组放左边,偶数组放右边,交界选5和4

#include <iostream>
using namespace std;

int main() 
{
	int t, n;
	cin >> t;
	while (t --) {
		cin >> n;
		if (n <= 4) {
			cout << "-1\n";
		}
		else {
			for (int i = 1; i <= n; i += 2) {
				if (i != 5) {
					cout << i << " ";
				}
			}
			cout << "5 4 ";
			for (int i = 2; i <= n; i += 2) {
				if (i != 4) {
					cout << i << " ";
				}
			}
			cout << "\n";
		}
	}
	return 0;
}

D Sharky Surfing

#include<bits/stdc++.h>
#define L <<"\n"
#define P <<" "
#define CL cout L;
#define CI cin>>
#define CO cout<<
#define CII(a,s,n) F(cii,s,n) CI a[cii];
#define F(a,b,c) for (int a=b;a<=c;a++)
#define FD(a,b,c) for (int a=b;a>=c;a--) 
#define DL if (debug) {cout L;}
#define DBV(x) if (debug) {cout<<#x<<"="<<x L;}
#define DBA(a,s,n) if (debug) {F(dbi,s,n) CO a[dbi] P; CL}
#define LL long long
#define SF(x) scanf("%d",&x);
#define PF(x) printf("%d\n",x);
#define clear(a) memset(a,0,sizeof(a));
#define elif else if
#define MAX 
#define debug 1 
using namespace std;
int t;
int n,m,l;
int a[200005][2];
int b[200005][2];
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    CI t;
    while (t--)
    {
        int kn=0,km=0;
        CI n>>m>>l;
        F(i,1,n)
        {
            int x,y;
            CI x>>y;
            kn++;
            a[kn][0]=x;
            a[kn][1]=y-x+2;
        }
        F(i,1,m)
        {
            km++;
            CI b[km][0]>>b[km][1];
        }
        // F(i,1,kn)
        // {
        //     CO a[i][0] P<<a[i][1] L;
        // }
        int v=1,p=1,ans=0;
        priority_queue<int> q;
        // CO ">>>>>";
        // CO q.empty() L;
        F(i,1,kn)
        {
            int x=a[i][0];
            while (p<=km && b[p][0]<x) 
            {
                // DBV(b[p][1])
                q.push(b[p][1]);
                p++;
            }

            while (v<a[i][1]&&!q.empty())
            {
                ans++;
                v+=q.top();
                q.pop();
                // DBV(v)
            }

            if (v<a[i][1]) 
            {
                CO -1 L;
                goto END;
            }
        }
        CO ans L;
        END:;
    }
    return 0;
}

E Kachina's Favorite Binary String

#include<bits/stdc++.h>
using namespace std;
int n,t;
int a[200005],k[200005];
int query(int l,int r)
{
    cout<<"? "<<l<<" "<<r<<endl;
    int x;
    cin>>x;
    return x;
}
int main()
{
	cin>>t;
    while (t--)
    {
        cin>>n;
        for (int i=0;i<=n;i++) 
        	a[i]=0;
        for (int i=2;i<=n;i++)
        	k[i]=query(1,i);
        if (k[n]==0) 
        {
            cout<<"! IMPOSSIBLE"<<endl;
            continue;
        }
        for (int i=2;i<=n;i++)
        	if (k[i]>k[i-1]) a[i]=1;
        for (int i=2;i<=n;i++)
        	if (a[i]!=0)
            {
                for (int j=1;j<=i-1-k[i];j++)
                	a[j]=1;
            }
        cout<<"! ";
        for (int i=1;i<=n;i++)
        	cout<<a[i];
        cout<<endl;
    }
}