C – Coloring Game

思路:不难看出,当 Alice 选完三个数 a b c(其中 a ≤ b ≤ c)后,Bob 能选的只有两种情况:

  1. 选择 c,这样只用比较 a+b 和 c 的大小关系,其中 a+b 一定要大于 c;
  2. 选择数组最大值 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的更多相关文章

  1. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  2. 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 ...

  3. 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 ...

  4. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  6. 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 ...

  7. 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 ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  9. 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 < ...

  10. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

随机推荐

  1. 🎀chrome-网页gif截图插件

    简介 本文介绍网页中gif截图工具使用,便于日常对网页中动态效果或元素进行截图 软件介绍 Capture to a Gif 是用来录制屏幕并将其保存为 GIF 格式文件的chrome插件工具.它允许用 ...

  2. Linux四剑客grep、find、sed、awk使用

    ‌介绍 Linux四剑客‌是指在Linux系统中非常常用的四个命令工具,它们分别是grep.find.sed和awk.这四个工具在Linux系统中具有非常强大的功能,可以方便快捷地对文本进行搜索.处理 ...

  3. php 设置友好时间

    public static function formatTime($time){ if (is_int($time)) { $time = intval($time); } elseif ($tim ...

  4. 1、 为什么软件开发周期总是预估的2~3倍? 2、什么是分而治之? 3、了解 WBS

    1.为什么软件开发周期总是预估的2~3倍? 首先,软件开发中经常会有需求变更的情况,客户或者利益相关者可能会提出新的需求或者改变现有的需求,这就得调整计划,增加了开发时间.其次,开发人员的技术和经验也 ...

  5. 在Ubuntu Server上安装Checkmk监控系统

    一.安装前准备 更新系统并安装依赖: sudo apt update && sudo apt upgrade -y sudo apt install -y wget apt-trans ...

  6. C#之使用任务并行库

    .NET Framework4.0引入了一个新的关于异步操作的API,它叫做任务并行库(Task Parallel Library,简称TPL).TPL的核心是任务,一个任务代表一个异步操作,该操作可 ...

  7. B1041 考试座位号

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

  8. 博创Luby使用指南

    Luby使用指南 1.开机 通电,当显示在boot界面的时候,长按正方形(深灰色)那个键,即可进入选择程序界面,此时再按一次正方形那个键,即可进入USB连接模式,此时用线将Luby和电脑连接起来. 当 ...

  9. Spring扩展接口-BeanFactoryAware

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  10. 杂七杂八系列----C#代码如何影响CPU缓存速度?

    CPU与RAM的隔阂 CPU与RAM是两个独立的硬件,并非集成在一起.所以他们两个之间一定会存在一个连接的桥梁,这个桥梁的名字叫做内存总线. 内存总线由三部分组成: 地址总线(Address Bus) ...