Bishops Alliance—— 最大上升子序列
原题链接:http://codeforces.com/gym/101147/problem/F
题意:n*n的棋盘,给m个主教的坐标及其私有距离p,以及常数C,求位于同一对角线上满足条件:dist(i, j) >= p[i]^2 + p[j]^2 + C
的主教集合的元素个数最大值。
解题思路:
上述条件可以等价为:
d(j) - d(i) +1 >= p[i]^2 + p[j]^2 + C // d(i) 为第i个主教相对于该对角线顶点的距离
d(j) - p[j]^2 - C + 1>= d(i) + p[i]^2
设 f(i) = d(i) + p[i] ^2, g(i) = d(i) - p[i]^2 - C + 1
下面考虑一条对角线,设 c[x] 为长度为x 的最后一个主教编号,例如c[len] = i 代表长度为len的防线最后一个主教编号为i。
(特别的,c[0] = 0, f(0) = -INF )
首先将该对角线上的主教按 d(i) 排序, len 为当前最大长度+1,依次查询每一个主教并同时更新最大长度, 伪代码如下:
对当前查询的主教u
j = lower_bound(c, c+len,u,cmp) - c
if j =len && g(u) >= f(c[j-1])
c[len++] = u
if j != len && g(u) >= f(c[j-1])
c[j] = u
注意: 数据范围为 LL
代码如下:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = +;
typedef long long LL;
#define INF 999999999999999999LL
vector<int> D1[*maxn];
vector<int> D2[*maxn]; int c[maxn];
int row[maxn], col[maxn], p[maxn];
int n, m, C;
//计算对角线编号
int dig_id1(int x, int y) {return x-y+n;}
int dig_id2(int x, int y) {return x+y;} int d1(int i) {return min(row[i], col[i]);}
int d2(int i) {return min(n-row[i]+, col[i]);} LL f1(int i) {return !i ? -INF : d1(i) + LL(p[i])*p[i];}
LL f2(int i) {return !i ? -INF : d2(i) + LL(p[i])*p[i];} LL g1(int i) {return d1(i) - LL(p[i])*p[i] - C + ;}
LL g2(int i) {return d2(i) - LL(p[i])*p[i] - C + ;} bool cmpd1(int i, int j) {return d1(i) < d1(j);}
bool cmpd2(int i, int j) {return d2(i) < d2(j);}
bool cmp1(const int& a,const int& b) {return f1(a) < f1(b);}
bool cmp2(const int& a,const int& b) {return f2(a) < f2(b);}
LL (*f[])(int) ={
f1,
f2
};
LL (*g[])(int) = {
g1,
g2
};
bool (*cmp[])(const int& ,const int& ) = {
cmp1,
cmp2
}; int cal(vector<int> &D,int flag) {
if(!D.size()) return ;
if(flag == ) sort(D.begin(), D.end(), cmpd1);
else sort(D.begin(), D.end(), cmpd2);
for(int i = ; i <= D.size(); i++) c[i] = ;
int len = ;
int j;
for(int i = ; i < D.size(); i++){
int u = D[i];
j = lower_bound(c, c+len, u, cmp[flag]) - c;
if(j == len && g[flag](u) >= f[flag](c[j-])) {
c[len++] = u;
}
if(j != len && g[flag](u) >= f[flag](c[j-])) {
c[j] = u;
}
}
return len - ;
}
#define fin stdin
int main() {
// FILE * fin;
// fin = fopen("bishops.in", "r");
int T;
fscanf(fin, "%d", &T);
while(T--) {
fscanf(fin, "%d%d%d", &n, &m, &C);
for(int i = ; i <= *n; i++) D1[i].clear();
for(int i = ; i <= *n; i++) D2[i].clear();
for(int i = ; i <= m; i++) {
fscanf(fin, "%d%d%d", &row[i], &col[i], &p[i]);
int id1 = dig_id1(row[i], col[i]);
int id2 = dig_id2(row[i], col[i]);
D1[id1].push_back(i);
D2[id2].push_back(i);
}
int ans = ;
for(int i = ; i <= *n; i++) {
ans = max(ans, cal(D1[i], ));
ans = max(ans, cal(D2[i], ));
}
printf("%d\n", ans);
}
return ;
}
Bishops Alliance—— 最大上升子序列的更多相关文章
- GYM - 101147 F.Bishops Alliance
题意: 一个n*n的棋盘,有m个主教.每个主教都有自己的权值p.给出一个值C,在棋盘中找到一个最大点集.这个点集中的点在同一条对角线上且对于点集中任意两点(i,j),i和j之间的主教数(包括i,j)不 ...
- 【Mutual Training for Wannafly Union #1 】
A.Phillip and Trains CodeForces 586D 题意:过隧道,每次人可以先向前一格,然后向上或向下或不动,然后车都向左2格.问能否到达隧道终点. 题解:dp,一开始s所在列如 ...
- 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16) 题解
题目链接:http://codeforces.com/gym/101147 2017/8/27日训练赛,题目情况9/11,Rank 4/79. A. The game of Osho 题意:定义一个子 ...
- 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)
A.The game of Osho(sg函数+二项展开) 题意: 一共有G个子游戏,一个子游戏有Bi, Ni两个数字.两名玩家开始玩游戏,每名玩家从N中减去B的任意幂次的数,直到不能操作判定为输.问 ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
随机推荐
- 外贸电子商务网站之Prestashop 设置网站基本信息
顶部banner图,首页各尺寸banner图.社交分享.物流支持.支付方式提醒等等设置 示例版本:1.6.1.11
- qt加载背景图片的一种方式
//加载背景图片 void LCTGrid::loadBgPicture() { QImage image; QPalette palette; image.load(m_sPicturePath); ...
- Core Data Migration 之拆分Entity
参考文章:http://blog.slalom.com/2010/01/08/splitting-an-entity-in-a-coredata-migration/ 最近自己做的背单词软件,在之前的 ...
- MAC使用技巧之苹果电脑抓图截屏方法
用苹果电脑自带的截图功能的快捷键:command+shift+3 三个键按下则抓取/截取全屏 command+shift+4 然后用鼠标框选则抓取该区域的截图 command+shift+4 然后按空 ...
- httpclient向浏览器发送get和post请求
get请求代码实现 public static void main(String[] args) { CloseableHttpClient httpClient = null; //请求对象 Cl ...
- C++中delete this
Is it legal (and moral) for a member function to say delete this? As long as you’re careful, it’s ok ...
- (译)Objective-C的动态特性
这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型 ...
- Kubernetes1.4即将发布
(一)发布历史 Kubernetes 1.0 - 2015年7月发布 Kubernetes 1.1 - 2015年11月发布 Kubernetes 1.2 - 2016年3月发布 Kubernetes ...
- 干货 | 解读MySQL 8.0新特性:Skip Scan Range
MySQL从8.0.13版本开始支持一种新的range scan方式,称为Loose Skip Scan.该特性由Facebook贡献.我们知道在之前的版本中,如果要使用到索引进行扫描,条件必须满足索 ...
- GIT 用RSA加密方式来记住密码
ssh-kegen -t rsa -C "你的帐号";//生成rsa的公钥和密钥 当然这个要在GNU环境下来执行,要是用Windows的CMD是不可以的(不输入DIR时),感觉是因 ...