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 ...
随机推荐
- 静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析
静态批处理[1] 定义 标明为 Static 的静态物件,如果在使用相同材质球的条件下,在Build(项目打包)的时候Unity会自动地提取这些共享材质的静态模型的Vertex buffer和Inde ...
- app自动化的元素操作api
1.click() 触发当前元素的点击事件 elelogin.click(); 2.sendKeys(String str) 往触发的当前元素输入数据 eleinputpwd.sendKeys(&qu ...
- 阿里云OSS前端直传
注意: oss直传request与global对象冲突 <script src="http://gosspublic.alicdn.com/aliyun-oss-sdk-6.9.0.m ...
- symfony4怎么切换到开发环境的问题
1.根目录下有.env文件,约17行有这句: APP_ENV=dev 默认开发环境 prod为生产环境 2..env.local.php文件(如果有)会覆盖.env的配置
- 解决get请求特殊字符问题
@Bean public ServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory fa = new Tom ...
- 基于Java Swing开发好看的皮肤
先介绍几款开源及商业的皮肤. Weblaf:非常赞的套件,界面现代.简约.依赖包较少. 有开源也有商业协议,个人最喜欢的皮肤.https://github.com/mgarin/weblaf PgsL ...
- GIM: 调用AI自动生成git提交消息的工具
GIM - Git Intelligence Message,是根据文件变更内容,自动请求用户配置的AI服务,生成提交消息的工具. 代码托管地址 https://github.com/davelet/ ...
- 应用间通信(一):详解Linux进程IPC
进程之间是独立的.隔离的,使得应用程序之间绝对不可以互相"侵犯"各自的领地. 但,应用程序之间有时是需要互相通信,相互写作,才能完成相关的功能,这就不得不由操作系统介入,实现一种通 ...
- C++ 智能指针的删除器
为什么要设置删除器 C++11 加入STL的 shared_ptr 和 unique_ptr,已经是我们编码的常客了.用的多自然就会了解到它们的删除器,比如很多C语言库(GDAL, GLFW, lib ...
- 【uni-app】在windows10系统中HBuliderX用iPhone苹果手机进行调试运行详细说明
测试准备: 1)iphone13 ios18.4.1 和一根可以读取数据的苹果线 2)HBuliderX打开uni-app项目文件(项目图标是正方形内一个U) 3)windows10系统 测试目标 ...