acwing 第59场周赛

A .4491

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std ;
const int N = 1e3+10;
int q[N],temp[N];
int main()
{
    int n ;
    cin >> n ;
    int s = 0 , x = 0;
    for(int i = 0 ; i < n ; i++)
    {
        cin >> q[i] ;
        s +=q[i];
        temp[i+1] = temp[i] + q[i];
    }
    for(int i = 0 ; i <= n ; i++)
    {
        x = min(x , temp[i]);
    }
    cout << s - x ;
}

B.4492

这道题我从2一直枚举到n/2,但是tle了

所以要找规律,当n为偶数的时候,次数就是n/2,n为奇数的时候,找出最小质因子d,n-d就为偶数

还有一点就是,遍历的时候可以是 直接定义 i 为long long类型 , 也可以是 i <= n / i, 因为这样写自动转换成long long 类型了

 特判:质因子只有1和本身,res=1

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std ;
typedef long long LL ;
LL n ;
int main()
{
    cin >> n ;
    LL res = 0 ;
    if(n%2)
    {
        for(LL i = 2 ; i * i <= n; i++)
        {
            if(n % i==0)
            {
                n = n - i ;
                res++;
                break;
            }
        } 
        if(n%2) res++ , n = 0 ;
        
    }
    res += n/2 ;
    cout << res ;
    return 0 ;
}

C.4493

所有点的度数必然为2

维护并查集,维护每一个点的度数

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std ;
const int N = 2e5+10;
int d[N] , p[N] ;
bool st[N];
int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]) ;
    return p[x];
}
int main()
{
    int n , m ;
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i++)
    {
        p[i] = i ;
    }
    while(m--)
    {
        int a ,  b;
        cin >> a >> b ;
        p[find(a)] = find(b) ;
        d[a] ++ , d[b]++;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        if(d[i] != 2 ) st[find(i)] = true ;
    }
    int res = 0 ;
    for(int i = 1 ; i <= n ; i++)
    {
        if(p[i] == i && !st[i]) res ++ ;
    }
    cout << res ;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三粒小金子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值