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,你构造的可重集必须满足将所有元素合而为一以后,所消耗 ...
随机推荐
- Cocos2d-x中定时器的使用
CCTimer:轻量级的计时器 CCTimer (void) ccTime getInterval (void) void setInterval (ccTime fInterval) bool ...
- block implicitly retains self to indicate this is 警告消除
Build Settings 输入CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF 设置为No
- kvm初体验之二:安装
Host: CentOS release 6.4 (Final) 1. 开启处理器的虚拟化功能 进入BIOS,使能虚拟化功能: 进入linux, grep -E "vmx|svm" ...
- firfox 和 chrome 移动端Web开发页面调试
Firefox浏览: 1."Alt+t" 选择工具栏中的“工具”>Web开发者工具>查看器>点击红框所指的地方 或者 F12 [当然这个歌前提是你没有安装fire ...
- C#SocketAsyncEventArgs实现高效能多并发TCPSocket通信 (服务器实现)
http://freshflower.iteye.com/blog/2285272 想着当初到处找不到相关资料来实现.net的Socket通信的痛苦与心酸, 于是将自己写的代码公布给大家, 让大家少走 ...
- ls命令还能这么玩
排序文件大小: 我们希望以文件大小排序,我们可以使用-S 参数来这么做 如果希望文件大小从小到大排序: 如果只希望列出目录条目: 增加 /(斜线) 标记目录:要这么做,使用-p选项: 通过修改时间列出 ...
- 「LOJ#6121」「网络流 24 题」孤岛营救问题(BFS
题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到了迷宫的地形图.迷宫的外形是一个长方形 ...
- poj3177重修道路——边双连通分量缩点
题目:http://poj.org/problem?id=3177 找桥,缩点,总之都是板子: 对于每个叶子,互相连一条边即可:若最后剩下一个,则去和根节点连边: 所以叶子节点数+1再/2即答案. 代 ...
- linux 命令2
who who am i ssh scott@192.168.1.105 ps aux | grep pts/8 pwd // where are you? Page 205 mkdir -p dir ...
- java 通过System.getProperties()获取系统参数
转自:https://www.cnblogs.com/ksuifeng/archive/2010/09/25/1834416.html 1.java的System.getProperty()方法可以获 ...