关于部分 分的介绍

很多同学都在问我部分分怎么看怎么写

今天就拿一道例题来说吧

C. 三元组

对于30%的数据,1 ≤ N ≤ 100
对于60%的数据,1 ≤ N ≤ 1000

对于100%的数据,1 ≤ N ≤ 100000

数据是这样的

先写30分的代码就是暴力枚举

60分的代码就是以2为枚举边界,依次往左右两边搜

100分的代码就是用60分的代码找出区间的性质,然后推出前缀和的做法

上代码!

//三元组
//暴力枚举
//可以拿到30pts
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N];
int n,ans;
int main()
{
    cin>>n;
    for(int i = 1;i <= n;i++)
    {
        for(int j = i+1;i <= n;i++)
        {
            for(int k = j+1;k <= n;k++)
            {
                if(a[i] == 1 && a[j] == 2 && a[k] == 3)ans++;
            }
        }
    }
    cout<<ans<<endl;
    

    return 0;
}





//O(n ^ 2)
// 可以拿到60pts
#include<iostream>
using namespace std;
#define ll long long
const int N = 1010;
int a[N],s[N];
int n;
ll ans; 
int main()
{
    cin>>n;

    for(int i = 1;i <= n;i++)
    {
        cin>>a[i];
    }
    for(int i = 2;i <= n;i++)
    {
       ll cnt1 = 0,cnt3 = 0;
        if(a[i] == 2)
        {
            for(int j = 1;j < i;j++)
            if(a[j] == 1)cnt1++;

            for(int j = i+1;j <= n;j++)
            if(a[j] == 3)cnt3++;

            ans += cnt1 * cnt3; 
        }
    }
    cout<<ans<<endl;
    return 0;
}






//满分做法
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100010;
ll cnt1[N],cnt3[N],ans;
int n,a[N];
signed main()
{
    cin>>n;
    for(int i = 1;i <= n;i++)
    {
        cin>>a[i];
        cnt1[i] = cnt1[i-1];
        cnt3[i] = cnt3[i-1];
        if(a[i] == 1) cnt1[i]++;
        else if(a[i] == 3)cnt3[i]++;
    }

    for(int i = 2;i <= n;i++)
    {
        if(a[i] == 2)
        {
            //cnt[i-1]
            //cnt3[n]-cnt3[i+1]
            ans += (cnt1[i-1]) * (cnt3[n] - cnt3[i]);
        }
    }
    cout<<ans<<endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值