【2022.10.14】CF827div4

	题的难度并不高,但是自己的思维仍是一团麻,没有理清题目内在的要求,一味暴力加剪枝,没有想出相应的算法来解决问题。
	同时因为题干是英文的关系,理解题意上还花了不少时间,需提高英语水平。
	
	[题目](https://codeforces.com/contest/1742)

A.Sum

简单的求和问题,可以直接利用if语句进行是否成立的判断,也可排序后再进行。

B.Increasing

判断是否能够满足严格单调递增,个人解法是先快排,再判断是否前后相等。
官方题解使用了set容器,保证读入时便自动升序排列
set容器学习链接

官方题解:

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;
    int x[n];
    set<int> a;
    for(int i = 0; i < n; i++)
    {
        cin >> x[i];
    }
    for(int i = 0; i < n; i++)
    {
        if(a.find(x[i]) != a.end())
        {
            cout << "NO" << endl;
            return;
        }
        a.insert(x[i]);
    }
    cout << "YES" << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
}

C.Stripes

一道很简单的判断问题,考试时由于忽略了竖着涂也可能使横着一排全一样导致未能AC。

D.Coprime

初见直接暴力,最糟糕的情况复杂度是n²;
注意到n范围很大,但是ai范围仅有1000,于是想到预处理这1000个数每个数对应的互质的数,并且在判断每个数时利用stl判断该组数据里是否存在其互质数。

vector和数组使用基本相同,但内存连续且长度可变,可以比较并判断是否空。

官方题解:

#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

vector<int> pairs[1001];
void solve() {
    int n; cin >> n;
    vector<int> id[1001];
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        id[x].push_back(i);
    }
    int ans = -1;
    for(int i = 1; i <= 1000; ++i) {
        for(int j: pairs[i]) {
            if(!id[i].empty() && !id[j].empty()) {
                ans = max(ans, id[i].back() + id[j].back());
            }
        }
    }
    cout << ans << "\n";
}   
 
int32_t main() {
    for(int i = 1; i <= 1000; ++i) {
        for(int j = 1; j <= 1000; ++j) {
            if(__gcd(i, j) == 1) {
                pairs[i].push_back(j);
            }
        }
    }
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
}

E.Scuza

由于英文,题干理解花了点时间,后根据样例理解题意。

对询问的q个值,对相应的每个值,找到序列中第一个比它大的数的前一个位置;

我的做法:二分查找,找到该数的前继;

#include<bits/stdc++.h>
#define re register
#define db double
#define ll long long
using namespace std;
inline ll read()
{
	ll x=0,f=1;
	char ch;
	while(ch>'9'||ch<'0')
	{
		if(ch=='-')	f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}
ll t,n,q,k,a;
ll sum[200005],maxx[200005];
int main()
{
	t=read();
	while(t--)
	{
		n=read();
		q=read();
		memset(sum,0,sizeof(sum));
		memset(maxx,0,sizeof(maxx));
		for(re ll i=1;i<=n;i++)
		{
			a=read();
			maxx[i]=max(maxx[i-1],a);
			sum[i]=sum[i-1]+a;
		}	
		for(re ll i=1;i<=q;i++)
		{
			k=read();
			int l=1,r=n;
			while(l<=r)
			{
				int mid=(l+r)/2;
				if(maxx[mid]<=k)	l=mid+1;
				else r=mid-1;
			}
			cout<<sum[r];
			if(i<=q-1)	cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}

官方题解:
同样利用了vector中的二分查找。

#include <bits/stdc++.h>

using namespace std;

void solve()
{
    int n, q;
    cin >> n >> q;
    vector<long long> pref;
    pref.push_back(0);
    vector<int> prefmax;
    for(int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        pref.push_back(pref.back()+x);
        if(i == 0)
        {
            prefmax.push_back(x);
        }
        else
        {
            prefmax.push_back(max(prefmax.back(), x));
        }
    }
    for(int i = 0; i < q; i++)
    {
        int k;
        cin >> k;
        int ind = upper_bound(prefmax.begin(), prefmax.end(), k)-prefmax.begin();
        cout << pref[ind] << " ";
    }
    cout << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
}

F.Smaller

关于字符串的处理,用字典序判断,从始至终题意便理解错误,题目要求尽可能比出大小,而不是严格排序。

因为初始均为’a’,进行输入的判定即可。

官方题解:

#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

void solve() {
    int q; cin >> q;
    bool otherA = false, otherB = false;
    ll cntA = 0, cntB = 0;
    while(q--) {
        ll d, k; string x; cin >> d >> k >> x;
        for(auto c: x) {
            if(d == 1) {
                if(c != 'a') otherA = 1;
                else cntA += k;
            } else {
                if(c != 'a') otherB = 1;
                else cntB += k;
            } 
        }
        if(otherB) {
            cout << "YES\n";
        } else if(!otherA && cntA < cntB) {
            cout << "YES\n";
        } else {
            cout << "NO\n";
        }
    }
}   
 
int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值