Codeforces Round #431 (Div. 2) B
Connect the countless points with lines, till we reach the faraway yonder.
There are n points on a coordinate plane, the i-th of which being (i, yi).
Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.
The second line contains n space-separated integers y1, y2, ..., yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise.
You can print each letter in any case (upper or lower).
5
7 5 8 6 9
Yes
5
-1 -2 0 0 -5
No
5
5 4 3 2 1
No
5
1000000000 0 0 0 0
Yes
In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.
In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.
In the third example, it's impossible to satisfy both requirements at the same time.
解法:把点分成两条平行线的形式,能不能分成呢
解法:
1 暴力啦
2 我们把1和i的斜率算一算,然后符合条件的都标记
3 剩下的再算一算斜率,然后发现有没有不等于这个斜率的,不等于返回第二步 i+1
4 因为我们1这个点已经当做确定点了,我们需要特殊考虑一下 比如 1 4 5 6这种情况
5 还有其他细节自己判断一下
#include<bits/stdc++.h>
using namespace std;
double x[];
set<double>Se;
double ans;
int main(){
int n;
int flag=;
cin>>n;
cin>>x[];
cin>>x[];
ans=x[]-x[];
for(int i=;i<=n;i++){
cin>>x[i];
if(x[i]-x[i-]!=ans){
flag=;
}
}
if(x[]-x[]!=ans){
int flag3=;
double cnt=x[]-x[];
for(int i=;i<=n;i++){
if(x[i]-x[i-]!=cnt){
flag3=;
}
}
if(flag3==){
cout<<"Yes"<<endl;
return ;
}
}
for(int i=;i<=n;i++){
Se.insert(x[i]);
}
if(Se.size()==){
cout<<"No"<<endl;
return ;
}
if(Se.size()==){
cout<<"Yes"<<endl;
return ;
}
if(flag==){
cout<<"No"<<endl;
return ;
}else{
map<int,int>Mp;
double lv;
Mp[]=;
for(int i=;i<=n;i++){
Mp.clear();
Mp[i]=;
lv=(x[i]-x[])/(i-)*1.0;
// cout<<lv<<" "<<i<<endl;
for(int j=;j<=n;j++){
if(Mp[j]) continue;
//cout<<(x[j]-x[1])/(j-1)<<" "<<j<<endl;
if((x[j]-x[])/(j-)==lv){
Mp[j]=;
}
}
int x1;
for(int j=;j<=n;j++){
if(Mp[j]==){
x1=j;
Mp[j]=;
break;
}
}
// cout<<x1<<endl;
int flag1=;
for(int j=;j<=n;j++){
if(Mp[j]==){
// cout<<(x[j]-x[x1])/(j-x1)<<"B"<<x1<<" "<<j<<endl;
if((x[j]-x[x1])/(j-x1)!=lv){
flag1=;
}
}
}
if(flag1==){
cout<<"Yes"<<endl;
return ;
}
}
}
cout<<"No"<<endl;
return ;
}
官方题解:我们只要讨论1 2 3点就行。不管怎么分,平行线都会经过这3点,复杂度变为O(n)
#include <bits/stdc++.h>
#define eps 1e-7
using namespace std;
int read()
{
int x=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,a[];
bool vis[];
bool check(double k,int b)
{
memset(vis,false,sizeof(vis));
int cnt=;
for (int i=;i<=n;i++)
{
if (a[i]-b==1LL*k*(i-))
{
vis[i]=true;
++cnt;
}
}
if (cnt==n) return false;
if (cnt==n-) return true;
int pos1=;
for (int i=;i<=n;i++)
if (!vis[i]&&pos1==) pos1=i;
for (int i=pos1+;i<=n;i++)
if (!vis[i])
{
if (fabs((double)(a[i]-a[pos1])/(i-pos1)-k)>eps) return false;
}
return true;
}
int main()
{
n=read();
for (int i=;i<=n;i++)
a[i]=read();
bool ans=false;
ans|=check(1.0*(a[]-a[]),a[]);
ans|=check(0.5*(a[]-a[]),a[]);
ans|=check(1.0*(a[]-a[]),a[]*-a[]);
if (ans) printf("Yes\n"); else printf("No\n");
return ;
}
Codeforces Round #431 (Div. 2) B的更多相关文章
- Codeforces Round #431 (Div. 1)
A. From Y to Y time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #431 (Div. 2) C. From Y to Y
题目: C. From Y to Y time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #431 (Div. 2)
A. Odds and Ends Where do odds begin, and where do they end? Where does hope emerge, and will they e ...
- Codeforces Round #431 (Div. 2) C
From beginning till end, this message has been waiting to be conveyed. For a given unordered multise ...
- 【Codeforces Round #431 (Div. 1) D.Shake It!】
·最小割和组合数放在了一起,产生了这道题目. 英文题,述大意: 一张初始化为仅有一个起点0,一个终点1和一条边的图.输入n,m表示n次操作(1<=n,m<=50),每次操作是任选一 ...
- 【Codeforces Round 431 (Div. 2) A B C D E五个题】
先给出比赛地址啦,感觉这场比赛思维考察非常灵活而美妙. A. Odds and Ends ·述大意: 输入n(n<=100)表示长度为n的序列,接下来输入这个序列.询问是否可以将序列划 ...
- Codeforces Round #431 (Div. 2) B. Tell Your World
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 【推导】【分类讨论】Codeforces Round #431 (Div. 1) B. Rooter's Song
给你一个这样的图,那些点是舞者,他们每个人会在原地待ti时间之后,以每秒1m的速度向前移动,到边界以后停止.只不过有时候会碰撞,碰撞之后的转向是这样哒: 让你输出每个人的停止位置坐标. ①将x轴上初始 ...
- 【推导】【贪心】Codeforces Round #431 (Div. 1) A. From Y to Y
题意:让你构造一个只包含小写字母的可重集,每次可以取两个元素,将它们合并,合并的代价是这两个元素各自的从‘a’到‘z’出现的次数之积的和. 给你K,你构造的可重集必须满足将所有元素合而为一以后,所消耗 ...
随机推荐
- Zookeeper用来干什么?
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- UVALive - 7831 :ACM Tax (主席树求树路径上中位数:LCA+主席树)
题意:给定一棵带权树,Q次询问,每次询问路径上的中位数. 思路:中位数分边数奇偶考虑,当当边数为num=奇时,结果就算路径第num/2+1大,用主席树做即可... (做了几道比较难的主席树,都wa了. ...
- 「P3385」【模板】负环(spfa
题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...
- CF1076E:Vasya and a Tree
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://codeforces.com/problemset/prob ...
- Mysql 创建存储过程 更新表
DELIMITER // use protocoldb// drop procedure if exists sp_protocol_Update// create procedure sp_prot ...
- [codeforces161D]Distance in Tree(点分治/树形dp)
题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...
- 学习总结:斯特林数( Stirling number )
基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{ ...
- [Makefile] 递归编译的Makefile的实现
转自:http://www.linuxidc.com/Linux/2017-01/139702.htm 最近写了一个递归Makefile,目的是既可以实现子模块的单独编译,也可以不做任何修改就和整个程 ...
- POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...
- [小工具] C#多线程|匿名委托传参数|测试网站压力--升级版
上次文章链接:http://www.sufeinet.com/thread-11-1-1.html写这些并不是不会用测试工具,也并不是无视测试工具,而是做为一个程序员希望用自己写的东西来完成一些功能, ...