Educational Codeforces Round 180 (Rated for Div. 2) C. Coloring Game
C – Coloring Game
思路:不难看出,当 Alice 选完三个数 a b c(其中 a ≤ b ≤ c)后,Bob 能选的只有两种情况:
- 选择 c,这样只用比较 a+b 和 c 的大小关系,其中 a+b 一定要大于 c;
- 选择数组最大值 a[n],这样只用比较 a+b+c 和 a[n] 的大小关系,且前者要更大。
所以,排序后枚举外层 a_id 和内层 b_id:
- 对于情况1:因为 a+b 的和一开始较小,后续逐渐变大,所以最大能选择的 c_id 一开始会较小,然后会随着 b_id 的变大而变大,该情况的“最大满足当前条件的 c_id”记为 k;
- 对于情况2:因为 a+b 的和一开始较小,需要较大的 c 才能使得 a+b+c > a[n],因此满足条件的最小 c_id 一开始会较大,后续随着 a+b 的增大而变小,该情况的“最小满足当前条件的 c_id”记为 l。
那么,对于每对 (a_id, b_id),可选的 c_id 数量即为 [l, k] 区间长度。利用 k、l 单调移动的特点,可用双指针在 O(n²) 内完成。
view code
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0; rep(i,1,n+1) res=(res+k)%i; return (res+s)%n;}
inline ll read(){ ll f=1,x=0; char ch=getchar(); while(ch>'9'||ch<'0'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } return x*f; }
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
ll a[maxn];
void sol() {
ll n = read();
rep(i,1,n) a[i] = read();
sort(a+1,a+1+n);
ll ans = 0;
rep(i,1,n) {
ll k = i+2, l = n;
rep(j,i+1,n-1) {
while(k <= n && a[i] + a[j] > a[k]) k++;
while(l > j && a[i] + a[j] + a[l] > a[n]) l--;
ll k1 = min(n, k-1), l1 = max(l+1, j+1);
if(k1 >= l1) ans += (k1 - l1 + 1);
}
}
cout << ans << endl;
}
int main() {
int kase;
cin >> kase;
while(kase--) sol();
return 0;
}
Educational Codeforces Round 180 (Rated for Div. 2) C. Coloring Game的更多相关文章
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
随机推荐
- CPU 和GPUskinning对比
CPU: 比如广泛的设备兼容性,比如上面说的精确逻辑处理,比如可以根据距离对Skinning进行LOD(如近距离角色每秒30帧Skinning,远距离角色每秒15帧Skinning),比如多Pass渲 ...
- 一款 .NET 开源、免费、轻量级且非侵入性的防火墙软件
前言 在当今数字化时代,系统服务器网络安全已成为我们日常生活和工作中不可忽视的重要议题.随着网络威胁的日益复杂和多样化,选择一款高效.可靠且易于使用的防火墙软件显得尤为重要.今天大姚给大家分享一款 . ...
- 智能语音备忘录:SpeechRecognition与gTTS的奇妙融合
引言:智能语音备忘录的时代已经到来 在这个信息爆炸的时代,我们每天需要处理大量的事务和信息.传统的文字记录方式虽然可靠,但在效率上往往难以满足快节奏生活的需求.想象一下,如果你能在驾车.散步或是灵感突 ...
- 蒟蒻 AstralNahida 的码风
前言 这里是蒟蒻 OIer AstralNahida 在 OI 中的码风的详细介绍. 个人认为码风相当清晰,供给各位参考. 约定 对于一些表示必要性的关键词,从 must 到 mustn't 排序如下 ...
- 通过Linux包管理器提升权限
免责声明:本文所涉及的技术仅供学习和参考,严禁使用本文内容从事违法行为和未授权行为,如因个人原因造成不良后果,均由使用者本人负责,作者及本博客不承担任何责任. 前言 在Linux系统中,apt和yum ...
- 一个开源、经典的 WPF 控件、组件和实用工具集合,值得参考学习!
前言 今天大姚给大家推荐一个开源.经典的 WPF 控件.组件和实用工具集合,对于想要自己编写 WPF UI 界面的同学可以参考借鉴学习:Extended.Wpf.Toolkit. 项目介绍 Exten ...
- java代码中启动exe程序最简单的方法
使用awt的Desktop类的open方法: public static void startExe(String exePath){ try { if(StringUtils.isNotBlank( ...
- HUAWEI USG6505E 如何使用光电互斥口
1.display ip interface brief 2.display int g0/0/4 查看端口 是否为光电互斥口,并确定端口当前状态 Copper 电口 Fiber 光口 3.inte ...
- ceph存储介绍
一.ceph简介 ceph是一个开源的.统一的分布式存储系统,设计初衷是提供较好的性能.可靠性和可扩展性.其中"统一"是说ceph可以一套存储系统同时提供块存储设备.文件系统存储和 ...
- 深入浅出了解生成模型-1:GAN模型原理以及代码实战
更加好排版:https://www.big-yellow-j.top/posts/2025/05/08/GAN.html 日常使用比较多的生成模型比如GPT/Qwen等这些大多都是"文生文& ...