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. ocr识别遇到的小问题-图片的EXIF 元数据

    背景   之前在公司通过paddleocr写了个接口,传入图片的base64编码返回识别出的文字信息.但是图片为横向时,文字行会乱序,所以准备新加一个功能通过paddleclas推理图片文字的倾斜角度 ...

  2. 【doctrine/orm】findBy用法

    用法: //$condition array('表字段对应的entity的属性'=>'值') //$orderBy array('表字段'=>'ASC/DESC') //$count in ...

  3. Python—Pytorch学习-RNN(一)

    前言 有好几个月没搞神经网络代码了,期间也就是回顾了两边之前的文字. 不料,对nn,cnn的理解反而更深入了-_-!. 修改 <零基础学习人工智能-Python-Pytorch学习(四)> ...

  4. CTF实验吧:登陆一下? 不一样的SQL注入

    http://ctf5.shiyanbar.com/web/wonderkun/web/index.html 发现 过滤了很多SQL敏感字符,并且 转码绕过也并不行 发现'和=没有进行过滤 考虑万能密 ...

  5. PhpStorm - 本地动态调试-下载配置xdebug扩展

    PhpStorm - 本地动态调试-下载配置xdebug扩展 00x01 查看phpinfo <?php phpinfo(); 00x02下载扩展前查看Architecture 如果Archit ...

  6. linux 指定运行级别

    目录 基本介绍 指定运行级别 基本介绍 0:关机 1:单用户 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启 常用的运行级别是3和5,也可以 ...

  7. 全国海域潮汐表查询微信小程序详情教程及代码

    最近在做一个全国海域潮汐表查询,可以为赶海钓鱼爱好者提供涨潮退潮时间表及潮高信息.下面教大家怎么做一个这样的小程序.主要功能,根据IP定位地理位置,自动查询出省份或城市的港口,进入后预测7天内港口潮汐 ...

  8. Flask 中用 dbutils 实现数据库连接池

    之前用 dbutils 来实现数据库连接池, 这里将其封装为一个自定义类并在 flask 中实际应用一下, 在实际场景中肯定是多页面接口, 这也就涉及到 python 的 import 问题, 就个人 ...

  9. React并发机制揭秘

    @charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...

  10. CentOS 7.6 安装 Mysql 5.7

    一.查看CentOS版本 Mysql的版本必须要和CentOS的版本对应!查看CentOS版本的指令如下: cat /etc/redhat-release 二.下载yum源包 wget http:// ...