首页 > 其他分享 >AcWing 第 87 场周赛 ABC

AcWing 第 87 场周赛 ABC

时间:2023-01-22 17:11:06浏览次数:58  
标签:周赛 typedef ABC const 怪兽 int LL long 87

https://www.acwing.com/activity/content/competition/problem_list/2814/

4797. 移动棋子

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1e6+10,M=4002;
const double PI=3.1415926535;
#define endl '\n'
int a[M][M];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL x=-1,y=-1;
        for(int i=1;i<=5;i++)
        {
            for(int j=1;j<=5;j++)
            {
                cin>>a[i][j];
                if(a[i][j]==1) x=i,y=j;
            }
        }
        cout<<abs(x-3)+abs(y-3);
    }
    return 0;
}

4798. 打怪兽

题目大意:

n个怪兽,每个怪兽的防御值为 ai 。我的初始攻击力为 m 。 

当攻击力大于 0 时,可以发动攻击: 任意选择 1∼2 个怪兽,并消耗 x 点法力值,对每个所选怪兽发动一次伤害为 x 的攻击。

对于受到攻击的怪兽,如果其防御值小于或等于 x,则会被消灭。否则,它将免疫此次攻击,不受任何影响。

请你确定最大的整数 k,满足:通过合理安排攻击,可以将第 1∼k 个怪兽全部消灭。
输入样例1:
5 7
2 3 5 4 1
输出样例1:
3

二分+贪心
因为这个题目的数据范围只在1000之内,所以二分+贪心是没有问题的(nlog^2n),不会爆时间

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1e6+10,M=4002;
const double PI=3.1415926535;
#define endl '\n'
LL n,m,a[N],b[N];
bool check(LL x)
{
    for(int i=1;i<=x;i++)
        b[i]=a[i];
    sort(b+1,b+1+x);
    reverse(b+1,b+1+x);
    LL res=0;
    for(int i=1;i<=x;i++)
    {
        res+=b[i];
        i++;
    }
    if(res<=m) return true;
    else return false;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        LL l=1,r=n,ans=0;
        while(l<=r)
        {
            LL mid=(l+r)/2;
            if(check(mid)==false) r=mid-1;
            else
            {
                ans=mid;
                l=mid+1;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

4799. 最远距离

题目大意:

如果一个无向连通图满足去掉其中的任意一条边都会使得该图变得不连通,则称该图为有效无向连通图。

 n个点 m 条边的有效无向连通图,点的编号为 1∼n,所有边的长度均为 1。 

求给定图中距离最远的两个点之间的距离。
输入样例1:
4 3
1 2
1 3
1 4
输出样例1:
2

树的直径板子题

详解见代码内部

/*
由题目已知条件可得知:
如果一个无向连通图满足去掉其中的任意一条边都会使得该图变得不连通,
则称该图为有效无向连通图
即为这所有的点都是由边连起来了成为了一棵树
我们要求最长的路径即求树的最长直径
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1e6+10,M=4002;
const double PI=3.1415926535;
#define endl '\n'
LL n,m,ans=0;
LL e[N],ne[N],h[N],idx;
void add(LL a,LL b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
LL dfs(LL u,LL father)
{
    LL dist=0;//表示从当前点往下走的最大长度
    LL d1=0,d2=0;
    for(int i=h[u];i!=-1;i=ne[i])
    {
        LL j=e[i];
        if(j==father) continue;//不能往回搜啦
        LL d=dfs(j,u)+1;
        dist=max(dist,d);

        if(d>=d1) d2=d1,d1=d;
        else if(d>d2) d2=d;
    }
    ans=max(ans,d1+d2);
    return dist;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        memset(h,-1,sizeof h);
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            LL a,b;
            cin>>a>>b;
            //建边
            add(a,b);
            add(b,a);
        }
        dfs(1,-1);//当前节点,前一个节点
        cout<<ans<<endl;
    }
    return 0;
}

标签:周赛,typedef,ABC,const,怪兽,int,LL,long,87
From: https://www.cnblogs.com/Vivian-0918/p/17064522.html

相关文章

  • abc222 F - Expensive Expense
    题意:给定一棵树,边权为路费,点权为观光费。从\(u\)去\(v\)旅游的费用定义为路费加上\(v\)点的观光费求从每个点出发到其它点旅游的最大费用\(n\le2e5\)思路:一眼......
  • leetcode笔记——328周赛
    1.二维前缀和,二维差分304.二维区域和检索-矩阵不可变-力扣(LeetCode)二维前缀和怎么处理,注意加减的下标 2.2537.统计好子数组的数目-力扣(LeetCode)首先看到子数......
  • AT_abc286d 题解
    板子首先我们看到值域并不大。因此可以维护值域,跑完全背包。具体而言维护某一个值(小于\(10000\))是否能被凑出来,然后枚举物品种类以及物品数量即可。一般而言,完全背包......
  • AT_abc286e 题解
    首先观察到\(n\leq300\)加上全源“最短路”便可以自然而然的想到floyd。注意到floyd算法的可行性只依赖统计的东西具有优先级。这里我们定义优先级为最短路最短且......
  • ABC286 A-E题解
    题目虽然是大年三十,但是玩手机没写题有意思。从50分钟才开始看题。A题意:将数组中\([p,q]\)与\([r,s]\)的元素交换并输出。sbt。B大意:将字符中的na换成nya。......
  • AcWing 第87场周赛题解
    T1移动棋子算出为\(1\)的点离\((3,3)\)的距离即可。#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;intmain(){int......
  • abc231 E - Minimal payments
    题意:给定\(n\)种不同面值的纸币,面值为\(a_1,\cdots,a_n\)。问通过给钱和找零支付价格为\(x\)的商品至少共要多少张纸币\(1=a_1<a_2<\cdots<a_n\le1e18\)\(a_i|......
  • 20th Jan 1872.查找用户活跃分钟数
    20thJan1872.查找用户活跃分钟数给你用户在LeetCode的操作日志,和一个整数k。日志用一个二维整数数组logs表示,其中每个logs[i]=[IDi,timei]表示ID为IDi的......
  • GoodBye Renyin ABC题解
    GoodByeRenyinABC题解A答案为\(\text{YES}\)的充要条件是\(\max(a_i)\timesr\le(\suma_i-\max(a_i))\timesR\)。必要性显然。充分性是可以先把最大的放在\((......
  • C - abc285_brutmhyhiizp
    C-abc285_brutmhyhiizphttps://atcoder.jp/contests/abc285/tasks/abc285_c思路对于长度为L+1的字符序列,A[1],...,A[L],A[L+1]首先统计长度为1到L字符序列总......