首页 > 其他分享 >AtCoder Beginner Contest 159

AtCoder Beginner Contest 159

时间:2023-01-26 21:35:17浏览次数:61  
标签:AtCoder cnt frac Beginner 159 复杂度 个数 times dp

A - The Number of Even Pairs

相加为偶数只有偶加偶和奇加奇两种情况,其实就是在\(n\)个数中取两个(\(C\binom{2}{n}\)),在\(m\)个数中取两个(\(C\binom{2}{m}\))。

B - String Palindrome

直接取出每一部分的字符串,判断到中心距离相等的位置的字符是否相等即可。

时间复杂度\(O(|s|)\)。

C - Maximum Volume

和一定差小积大。

其实就是一个均值不等式。

设棱长为\(a,b,c\),则 \(a \times b \times c \leq \frac{(a+b+c)^3}{27}=\frac{x^3}{27}\),当\(a=b=c\)时成立。

D - Banned K

我们可以把 \(n\) 个数中有多少相同的数对,因为每个数都很小所以这点我们可以用堆来做。

设 \(cnt[i]\) 代表 \(i\) 这个数出现的次数所以答案等于 \(\sum_{i=1}^{n}\frac{cnt[i] \times (cnt[i]-1)}{2}\)。

现在我们来看删掉一个值为 \(x\) 的数。

\(cnt[x]\) 的值减小了一,它的贡献变成了 \(\frac{(cnt[x]-1) \times (cnt[x]-2)}{2}\),相比之前减少了 \(cnt[x]-1\)。

设不删时的答案是 \(tot\),则删掉一个权值为 \(x\) 的数的答案为 \(tot-(cnt[x]-1)\)。

单词询问复杂度O(1)。

E - Dividing Chocolate

我们发现n非常的小,于是我们可以枚举行之间且的情况,再判断列之间要切的情况,取最小值即可。

时间复杂度\(O(2^n \times n \times m)\)

F - Knapsack for All Segments

考虑一组和为s的排列x1<x2<...<xk,所有[1,x1]内的L,[xk, n]之内的R都包含这组排列,所以它的贡献就是\(x_1 \times (n-x_k+1)\)。

设dp[i][j]表示枚举到第i个数,和为j的排列的第一个数的和。

显然dp[0][j]都是0。转移方程类似01背包,dp[i][j]=dp[i-1][j]+(j==a[i]?i:0)。

考虑如何统计答案,如果a[i]=s,ans+=i(n-i+1);如果a[i]>s,ans+=dp[s-a[i]](n-i+1)。如果a[i]<s则不用更新。

当然类似于背包问题,这题也可以用滚动背包优化空间复杂度。

时间复杂度O(ns);空间复杂度O(n+s)

标签:AtCoder,cnt,frac,Beginner,159,复杂度,个数,times,dp
From: https://www.cnblogs.com/zcr-blog/p/17068239.html

相关文章

  • AtCoder Beginner Contest 126
    AtCoderBeginnerContest126https://atcoder.jp/contests/abc126A-ChangingaCharacter#include<bits/stdc++.h>usingnamespacestd;intmain(){int......
  • AtCoder Beginner Contest 162
    A-Lucky7大水题,模拟即可。#include<iostream>#include<cstdio>usingnamespacestd;intmain(){strings;cin>>s;if(s[0]=='7'||s[1]==......
  • AtCoder Beginner Contest 172
    A-Calc(abc172a)题目大意给定一个\(a\),输出\(a+a^2+a^3\)解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=long......
  • 【最短路】Atcoder Beginner Contest 286----E
    题目:E-Souvenir(atcoder.jp)题解:首先这道题可以很容易看出来是求最短路。最开始自己是用bfs写的,出现了WA,TLE,RE等错误。因为对于每种情况会有Q次询问,如果每次询问都......
  • 【多重背包】Atcoder Beginner Contest 286----D
    题目:D-MoneyinHand(atcoder.jp)分析:经典的多重背包。用dp[i]表示i能否正好凑出。先复习一下多重背包。多重背包就是有N组物品,每组最多有k个,每组可以选多个。分组背......
  • AtCoder Beginner Contest 286 解题报告
    AtCoderBeginnerContest286解题报告\(\text{ByDaiRuiChen007}\)ContestLinkA.RangeSwap直接模拟交换过程即可时间复杂度\(\Theta(n)\)#include<bits/stdc++......
  • AtCoder Beginner Contest 286
    A-RangeSwap(abc286a)题目大意给定长度为\(n\)的数组\(a\)和\(p,q,r,s\),交换\(a[p..q]\)和\(a[r..s]\)并输出交换后的数组\(a\)。解题思路模拟即可。神奇......
  • D - Change Usernames -- ATCODER
    D-ChangeUsernameshttps://atcoder.jp/contests/abc285/tasks/abc285_d 思路DFS深度遍历图。需要注意的是,整个大图中可能有很多小的连通子图,每个子图需要确定起......
  • AtCoder Beginner Contest 043
    A-ChildrenandCandies(ABCEdit)n=int(input())print(n*(n+1)//2)B-UnhappyHacking(ABCEdit)用栈模拟一下?但是栈的遍历比较麻烦这里用vector实现#......
  • AtCoder Beginner Contest 047
    A-FightingoverCandies签到#include<bits/stdc++.h>usingnamespacestd;intread(){...}constintN=1e6+5;intmain(){inta=read(),b=read(......