text stringlengths 49 983k |
|---|
#include <iostream>
#include <iomanip>
#include<vector>
#include <math.h>
using namespace std;
const double eps = 1e-15;
bool IsEq(double d1, double d2)
{
if (d1 - d2 > -eps && d1 - d2 < eps){
return true;
} else {
return false;
}
}
class Point2D {
public:
double x, y;
Point2D() : x(0), y(0) {}
Point2D(double _x, double _y) : x(_x), y(_y) {}
virtual ~Point2D() {}
};
istream& operator >> (istream& input, Point2D& P)
{
input >> P.x >> P.y;
return input;
}
class Segment {
public:
Point2D P, Q;
Segment() {}
Segment(const Point2D& _P, const Point2D& _Q) : P(_P), Q(_Q) {}
};
istream& operator >> (istream& input, Segment& Seg)
{
input >> Seg.P >> Seg.Q;
return input;
}
class Vector2D : public Point2D {
public:
Vector2D(double _x, double _y) : Point2D(_x, _y) {}
Vector2D(const Point2D& P) : Point2D(P) {}
Vector2D(const Segment& Seg) : Point2D(Seg.Q.x - Seg.P.x, Seg.Q.y - Seg.P.y) {}
Vector2D(const Point2D& Start, const Point2D& End) : Point2D(End.x - Start.x, End.y - Start.y) {}
double InnerProd(const Vector2D Vec) const
{
return x * Vec.x + y * Vec.y;
}
double OuterProd(const Vector2D Vec) const
{
return x * Vec.y - y * Vec.x;
}
double Length() const
{
return sqrt((*this).InnerProd(*this));
}
};
Vector2D operator * (double d, const Vector2D& V)
{
return Vector2D(d * V.x, d * V.y);
}
Vector2D operator + (const Vector2D& V1, const Vector2D& V2)
{
return Vector2D(V1.x + V2.x, V1.y + V2.y);
}
ostream& operator << (ostream& out, const Vector2D& V)
{
out << V.x << " " << V.y;
return out;
}
double Dist(const Point2D& P, const Point2D& Q)
{
Vector2D V(P, Q);
return V.Length();
}
bool IsPara(const Segment& Seg1, const Segment& Seg2)
{
Vector2D V1(Seg1), V2(Seg2);
return IsEq(V1.OuterProd(V2), 0.);
}
bool IsOnSeg(const Point2D& P, const Segment& Seg)
{
Vector2D V(Seg);
return IsEq(V.Length(), Dist(P, Seg.P) + Dist(P, Seg.Q));
}
bool IsIntersect(const Segment& Seg1, const Segment& Seg2)
{
Vector2D P1(Seg1.P), P2(Seg2.P);
Vector2D V1(Seg1), V2(Seg2);
if (!IsPara(Seg1, Seg2)) {
double t = (P2.OuterProd(V2) - P1.OuterProd(V2)) / V1.OuterProd(V2);
double s = (P2.OuterProd(V1) - P1.OuterProd(V1)) / V1.OuterProd(V2);
return t > -eps && t < 1 + eps && s > -eps && s < 1 + eps;
} else {
if (IsOnSeg(Seg1.P, Seg2) || IsOnSeg(Seg1.Q, Seg2) || IsOnSeg(Seg2.P, Seg1) || IsOnSeg(Seg2.Q, Seg1)) return true;
return false;
}
}
Vector2D CrossPoint(const Segment& Seg1, const Segment& Seg2)
{
Vector2D P1(Seg1.P), P2(Seg2.P);
Vector2D V1(Seg1), V2(Seg2);
if (!IsPara(Seg1, Seg2)) {
double t = (P2.OuterProd(V2) - P1.OuterProd(V2)) / V1.OuterProd(V2);
return P1 + t * V1;
} else {
return Point2D();
}
}
double Dist(const Point2D& P, const Segment& Seg)
{
Vector2D V1(Seg);
Vector2D V2(Seg.P, P);
double IP = V1.InnerProd(V2);
if (IP < 0.) {
return Dist(P, Seg.P);
} else if (IP > V1.InnerProd(V1)) {
return Dist(P, Seg.Q);
} else {
return fabs(V1.OuterProd(V2)) / V1.Length();
}
}
double Dist(const Segment& Seg1, const Segment& Seg2)
{
if (IsIntersect(Seg1, Seg2)) return 0.;
double Ret = Dist(Seg1.P, Seg2);
Ret = min(Ret, Dist(Seg1.Q, Seg2));
Ret = min(Ret, Dist(Seg2.P, Seg1));
Ret = min(Ret, Dist(Seg2.Q, Seg1));
return Ret;
}
double AreaPoly(vector<Point2D> vP)
{
int n = static_cast<int>(vP.size());
double Ret = 0.;
Vector2D Prev(vP[0], vP[1]), Now(vP[0], vP[2]);
for (int i = 2; i < n; ++i) {
Now = Vector2D(vP[0], vP[i]);
Ret += Prev.OuterProd(Now);
Prev = Now;
}
Ret /= 2.;
return Ret;
}
bool IsConvex(vector<Point2D> vP)
{
int n = static_cast<int>(vP.size());
vP.push_back(vP[0]);
vP.push_back(vP[1]);
Vector2D Prev(vP[0], vP[1]), Now(vP[0], vP[1]);
for (int i = 1; i <= n; ++i) {
Now = Vector2D(vP[i], vP[i+1]);
if (Prev.OuterProd(Now) < 0.) return false;
Prev = Now;
}
return true;
}
int main(int argc, const char * argv[]) {
int n;
cin >> n;
vector<Point2D> vP(n);
for (int i = 0; i < n; ++i) {
cin >> vP[i];
}
cout << fixed << setprecision(1);
cout << IsConvex(vP) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef complex<double> Point;
typedef pair<Point, Point> Line;
typedef vector<Point> Poly;
const double EPS = 1e-10;
// 内積 |a||b|cosθ
double dot(Point a, Point b){
// return a.real() * b.real() + a.imag() * b.imag();
return (conj(a) * b).real();
}
// 外積、符号付面積の2倍 |a||b|sinθ
double cross(Point a, Point b){
// return a.real() * b.imag() - a.imag() * b.real();
return (conj(a) * b).imag();
}
// 点の位置関係 2点a,bから見た点cの位置関係
int ccw(Point a, Point b, Point c){
// COUNTER_CLOCKWISE
if(cross(b - a, c - a) > EPS) return 1;
// CLOCKWISE
if(cross(b - a, c - a) < -EPS) return -1;
// ONLINE_BACK (c--a--b on line)
if(dot(b - a, c - a) < -EPS) return 2;
// ONLINE_FRONT (a--b--c on line)
if(abs(b - a) + EPS < abs(c - a)) return -2;
// ON_SEGMENT
return 0;
}
bool isConvex(Poly p){
int n = p.size();
for(int i = 0; i < n; i++){
if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;
}
return true;
}
int main(){
int n;
cin >> n;
Poly pl(n);
for(int i = 0; i < n; i++){
double x, y;
cin >> x >> y;
pl[i] = {x, y};
}
cout << isConvex(pl) << endl;
}
|
#include <cmath>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
const double kEps = 1e-10;
const double kInf = 1e15;
int dcmp(double x) {
if (fabs(x) < kEps) return 0;
return x < 0 ? -1 : 1;
}
struct Vector {
Vector() {}
Vector(double x, double y): x(x), y(y) {}
// Vector(const Point & p1, const Point & p2): x(p2.x - p1.x), y(p2.y - p1.y) {}
Vector(const Vector & v): x(v.x), y(v.y) {};
double x, y;
double Norm() const { return hypot(x, y); }
double NormSquared() const { return x * x + y * y; }
Vector Normalize() const {
return *this / this->Norm();
}
Vector operator-() const {
return Vector(-x, -y);
}
Vector operator+(const Vector & rhs) const {
return Vector(x + rhs.x, y + rhs.y);
}
Vector operator-(const Vector & rhs) const {
return *this + (-rhs);
}
Vector operator*(const double rhs) const {
return Vector(rhs * x, rhs * y);
}
Vector operator/(const double rhs) const {
return *this * (1.0 / rhs);
}
double Dot(const Vector & rhs) const {
return x * rhs.x + y * rhs.y;
}
double Cross(const Vector & rhs) const {
return x * rhs.y - y * rhs.x;
}
bool operator==(const Vector & rhs) const {
return dcmp(x - rhs.x) == 0 && dcmp(y - rhs.y) == 0;
}
bool operator<(const Vector & rhs) const {
return dcmp(x - rhs.x) < 0 || (dcmp(x - rhs.x) == 0 && dcmp(y - rhs.y) < 0);
}
};
struct Point {
Point() {}
Point(double x, double y): x(x), y(y) {}
Point(const Point & p): x(p.x), y(p.y) {};
double x, y;
Point operator-() {
return Point(-x, -y);
}
Point operator+(const Vector & rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
Point operator-(const Vector & rhs) const {
return *this + (-rhs);
}
bool operator==(const Point & rhs) const {
return dcmp(x - rhs.x) == 0 && dcmp(y - rhs.y) == 0;
}
bool operator<(const Point & rhs) const {
return dcmp(x - rhs.x) < 0 || (dcmp(x - rhs.x) == 0 && dcmp(y - rhs.y) < 0);
}
};
struct Line {
Line() {}
Line(const Point & p1, const Point & p2): p1(p1), p2(p2) {
a = p2.y - p1.y;
b = p1.x - p2.x;
c = (p2.x - p1.x) * p1.y + (p1.y - p2.y) * p1.x;
if (dcmp(p2.x - p1.x) == 0) {
k = kInf;
} else {
k = (p2.y - p1.y) / (p2.x - p1.x);
}
}
Line(double x1, double y1, double x2, double y2): p1(x1, y1), p2(x2, y2) {
a = y2 - y1;
b = x1 - x2;
c = (p2.x - p1.x) * p1.y + (p1.y - p2.y) * p1.x;
if (dcmp(x2 - x1) == 0) {
k = kInf;
} else {
k = (y2 - y1) / (x2 - x1);
}
}
Line(const Line & l);
Point p1, p2;
double a, b, c;
double k;
};
ostream & operator<<(ostream & os, const Point & p) {
os << p.x << " " << p.y;
return os;
}
ostream & operator<<(ostream & os, const Vector & v) {
os << v.x << " " << v.y;
return os;
}
ostream & operator<<(ostream & os, const Line & l) {
os << l.p1.x << " " << l.p1.y << " " << l.p2.x << " " << l.p2.y;
return os;
}
// -1: left, 1: right, 0: colinear
int Direction(const Point & p0, const Point & p1, const Point & p2) {
Vector v1(p1.x - p0.x, p1.y - p0.y);
Vector v2(p2.x - p0.x, p2.y - p0.y);
return dcmp(v2.Cross(v1));
}
int main(int argc, char const *argv[]) {
int n;
double x0, y0, x1, y1;
int is_convex = 1;
int direction = 0;
cin >> n;
cin >> x0 >> y0;
cin >> x1 >> y1;
Point p00(x0, y0);
Point p0(x0, y0);
Point p1(x1, y1);
for (unsigned i = 0; i < n - 2; ++i) {
double x2, y2;
cin >> x2 >> y2;
Point p2(x2, y2);
auto direction_now = Direction(p0, p1, p2);
p0 = p1;
p1 = p2;
if (Direction(p0, p1, p2) * direction == -1) {
is_convex = 0;
break;
}
if (direction == 0) {
direction = direction_now;
} else {
if (direction_now * direction == -1) {
is_convex = 0;
break;
}
}
}
if (Direction(p0, p1, p00) * direction == -1) {
is_convex = 0;
}
cout << is_convex << "\n";
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define per(i, a, b) for(int i = (a); i >= (b); --i)
#define debug(x) cerr << #x << ' ' << x << endl;
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int MAXN = 2e5 + 7;
const double EPS=1e-8;
inline int sign(double a){return a<-EPS?-1:a>EPS;}
inline int cmp(double a,double b){return sign(a-b);}
//点
struct P{
double x,y;
P(){}
P(double _x,double _y):x(_x),y(_y){}
P operator + (P p){return P(x+p.x,y+p.y);}
P operator - (P p){return P(x-p.x,y-p.y);}
P operator * (double k){return P(x*k,y*k);}
P operator / (double k){return P(x/k,y/k);}
double dot(P p){return x*p.x+y*p.y;}
double det(P p){return x*p.y-y*p.x;}
double distTo(P p){return (*this-p).abs();}
double alpha(){return atan2(y,x);}
void read(){scanf("%lf%lf", &x, &y);}
void write(){printf("%.10lf %.10lf\n", x, y);}
double abs(){return sqrt(abs2());}
double abs2(){return x*x+y*y;}
P rot90(){return P(-y,x);}
P unit(){return *this/abs();}
int quad(){return sign(y)==1||(sign(y)==0&&sign(x)>=0);}
P rot(double an){return P(x*cos(an)-y*sin(an),x*sin(an)+y*cos(an));}
bool operator < (P p)const{int c=cmp(x,p.x);if(c)return c==-1;return cmp(y,p.y)==-1;}
bool operator == (P o)const{return cmp(x,o.x)==0&&cmp(y,o.y)==0;}
}s[105];
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
rep(i, 0, n-1) s[i].read();
bool flag = 1;
rep(i, 0, n-1) if((s[(i+2)%n]-s[(i+1)%n]).det((s[i]-s[(i+1)%n])) < -EPS) flag = 0;
printf("%d\n", flag);
return 0;
}
|
#include <stdio.h>
#include <stack>
#include <math.h>
using namespace std;
struct Point{
double x,y;
};
int main(){
int n;
scanf("%d",&n);
double gaiseki;
bool FLG = true;
Point points[n],left,right;
for(int i=0; i < n; i++){
scanf("%lf %lf",&points[i].x,&points[i].y);
}
for(int i=0; i < n; i++){
if(i == 0){
right.x = points[1].x - points[0].x;
right.y = points[1].y - points[0].y;
left.x = points[n-1].x - points[0].x;
left.y = points[n-1].y - points[0].y;
gaiseki = right.x*left.y - right.y*left.x;
}else if(i == n-1){
right.x = points[0].x - points[n-1].x;
right.y = points[0].y - points[n-1].y;
left.x = points[n-2].x - points[n-1].x;
left.y = points[n-2].y - points[n-1].y;
gaiseki = right.x*left.y - right.y*left.x;
}else{
right.x = points[i+1].x - points[i].x;
right.y = points[i+1].y - points[i].y;
left.x = points[i-1].x - points[i].x;
left.y = points[i-1].y - points[i].y;
gaiseki = right.x*left.y - right.y*left.x;
}
if(gaiseki < 0){
FLG = false;
break;
}
}
if(FLG)printf("1\n");
else{
printf("0\n");
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ld = long double; using ull = unsigned long long; using uint = unsigned int;
using vi = vector<int>; using vb = vector<bool>; using vd = vector<double>; using vl = vector<ll>;
using vvi = vector<vi>; using vvb = vector<vb>; using vvd = vector<vd>; using vvl = vector<vl>;
#define REP(i,n) for(ll i=0; i<(n); ++i)
#define FOR(i,b,n) for(ll i=(b); i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define TEN(x) ((ll)1e##x)
template<typename T> inline string join(const vector<T>& vec, string sep = " ") { stringstream ss; REP(i, vec.size()) ss << vec[i] << ( i+1 == vec.size() ? "" : sep ); return ss.str(); }
///////////////
#define EPS (1e-10)
#define NEXT(x, i) (x[(i + 1) % x.size()])
template<class T> using CR = const T &;
using P = complex<ld>;
using G = vector<P>;
int sgn(ld a, ld b = 0) {
if (a > b + EPS) return 1;
if (a < b - EPS) return -1;
return 0;
}
ld dot(P a, P b) { return real(conj(a)*b); }
ld cross(P a, P b) { return imag(conj(a)*b); }
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (sgn(cross(b, c))) return sgn(cross(b, c)); // clockwise or counter clockwise
if (sgn(dot(b, c)) == -1) return 2; // c--a--b
if (sgn(norm(b), norm(c)) == -1) return -2; // a--b--c
return 0; // a--c--b
}
bool is_convex(CR<G> g) {
REP(i, g.size()) if (ccw(g[(i+g.size()-1) % g.size()], g[i], g[(i + 1) % g.size()]) == -1) return false;
return true;
}
//////////////
int main() {
#ifdef INPUT_FROM_FILE
ifstream cin("sample.in");
ofstream cout("sample.out");
#endif
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
ll n; cin >> n;
G g(n);
REP(i,n){
ld x, y;
cin >> x >> y;
g[i] = { x, y };
}
cout << is_convex(g) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define Fi first
#define Se second
#define pb push_back
#define mp make_pair
#define rep(x, a, b) for(int x = (a); x <= (b); ++ x)
#define per(x, a, b) for(int x = (a); x >= (b); -- x)
#define rop(x, a, b) for(int x = (a); x < (b); ++ x)
#define por(x, a, b) for(int x = (a); x > (b); -- x)
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int unt;
typedef double db;
typedef pair <int, int> pii;
typedef vector <int> vi;
const db eps = 1e-8;
const db _PI = 3.1415926535897932384;
const db _E = 2.7182818284590452354;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fll;
int dcmp(db x) {
if(x > eps) return 1;
if(x < -eps) return -1;
return 0;
}
int dcmp(db x, db y) {
return dcmp(x - y);
}
struct Point {
db x, y;
Point(db _x = 0, db _y = 0) : x(_x), y(_y) {}
void print() { printf("%.10f %.10f", x, y); }
};
struct Line {
Point p1, p2;
Line() {};
Line(Point _p1, Point _p2) : p1(_p1), p2(_p2) {};
};
typedef Point Vector;
typedef Line Segment;
typedef vector<Point> Polygon;
// CCW
const int CCW_COUNTER_CLOCKWISE = 1;
const int CCW_CLOCKWISE = -1;
const int CCW_ONLINE_FRONT = -2;
const int CCW_ONLINE_BACK = 2;
const int CCW_ON_SEGMENT = 0;
Vector operator + (Vector a, Vector b) {
return Vector(a.x + b.x, a.y + b.y);
}
Vector operator - (Vector a, Vector b) {
return Vector(a.x - b.x, a.y - b.y);
}
Vector operator * (Vector a, db b) {
return Vector(a.x * b, a.y * b);
}
Vector operator / (Vector a, db b) {
return Vector(a.x / b, a.y / b);
}
db operator * (Vector a, Vector b) {
return a.x * b.x + a.y * b.y;
}
db operator ^ (Vector a, Vector b) {
return a.x * b.y - a.y * b.x;
}
db dis(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
db dis2(Point a, Point b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
db dis(Vector a) {
return sqrt(a.x * a.x + a.y * a.y);
}
db dis2(Vector a) {
return a.x * a.x + a.y * a.y;
}
Point Project(Line L, Point p) {
Vector Base = L.p2 - L.p1;
db len = Base * (p - L.p1);
return L.p1 + Base * (len / dis2(L.p1, L.p2));
}
Point Reflect(Line L, Point p) {
return p + (Project(L, p) - p) * 2.0;
}
int ccw(Point p0, Point p1, Point p2) {
Vector p01 = p1 - p0, p02 = p2 - p0;
if(dcmp(p01 ^ p02) == 1) return CCW_COUNTER_CLOCKWISE; // 逆时针
if(dcmp(p01 ^ p02) == -1) return CCW_CLOCKWISE; // 顺时针
if(dcmp(p01 * p02) == -1) return CCW_ONLINE_BACK;
if(dcmp(dis2(p01), dis2(p02)) == -1) return CCW_ONLINE_FRONT;
return CCW_ON_SEGMENT;
}
bool isOrthogonal(Vector a, Vector b) {
return dcmp(a * b) == 0;
}
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) {
return isOrthogonal(a2 - a1, b2 - b1);
}
bool isOrthogonal(Line a, Line b) {
return isOrthogonal(a.p2 - a.p1, b.p2 - b.p1);
}
bool isParallel(Vector a, Vector b) {
return dcmp(a ^ b) == 0;
}
bool isParallel(Point a1, Point a2, Point b1, Point b2) {
return isParallel(a2 - a1, b2 - b1);
}
bool isParallel(Line a, Line b) {
return isParallel(a.p2 - a.p1, b.p2 - b.p1);
}
bool IntersectSS(Point a1, Point a2, Point b1, Point b2) {
return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 && ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}
bool IntersectSS(Segment a, Segment b) {
return IntersectSS(a.p1, a.p2, b.p1, b.p2);
}
Point getCrossPointSS(Segment a, Segment b) {
Vector Base = b.p2 - b.p1;
db S1 = fabs(Base ^ (a.p1 - b.p1));
db S2 = fabs(Base ^ (a.p2 - b.p1));
return a.p1 + (a.p2 - a.p1) * (S1 / (S1 + S2));
}
//Point getCrossPointLL(Line a, Line b) {
// db S1 = (b.p2 - b.p1) ^ (a.p2 - a.p1);
// db S2 = (b.p2 - b.p1) ^ (b.p2 - a.p1);
// if(dcmp(S1) <= 0 && dcmp(S2) <= 0) return a.p1;
// return a.p1 + (a.p2 - a.p1) * (S2 / S1); //抄的别人代码,然而并不懂为啥/kel
//}
db disLP(Line a, Point p) {
return fabs(((a.p2 - a.p1) ^ (p - a.p1)) / dis(a.p1, a.p2));
}
db disSP(Segment a, Point p) {
if(dcmp((a.p2 - a.p1) * (p - a.p1)) == -1) return dis(a.p1, p);
if(dcmp((a.p1 - a.p2) * (p - a.p2)) == -1) return dis(a.p2, p);
return disLP(a, p);
}
db disSS(Segment a, Segment b) {
if(IntersectSS(a, b)) return 0;
return min({disSP(b, a.p1), disSP(b, a.p2), disSP(a, b.p1), disSP(a, b.p2)});
}
db Area(Polygon p) {
db S = p.back() ^ p[0];
rop(i, 0, (int) p.size() - 1) S += p[i] ^ p[i + 1];
return fabs(S) / 2;
}
bool isConvex(Polygon p) {
rop(i, 0, (int) p.size() - 2) if(ccw(p[i], p[i + 1], p[i + 2]) == CCW_CLOCKWISE) return 0;
if(ccw(p.back(), p[0], p[1]) == CCW_CLOCKWISE) return 0;
if(ccw(p[(int)p.size() - 2], p.back(), p[0]) == CCW_CLOCKWISE) return 0;
return 1;
}
int main() {
int n; scanf("%d", &n);
Polygon p; p.resize(n);
rop(i, 0, n) scanf("%lf%lf", &p[i].x, &p[i].y);
printf("%d\n", (int) isConvex(p));
return 0;
}
|
#include <iostream>
#include <vector>
#include <complex>
using namespace std;
const double eps = 1e-10;
typedef complex<double> Point;
inline double cross(const Point& a, const Point& b){
return imag(conj(a) * b);
}
inline double dot(const Point& a, const Point& b){
return real(conj(a) * b);
}
int ccw(Point a, Point b, Point c){
b -= a; c -= a;
if(cross(b, c) > eps) return 1;
if(cross(b, c) < -eps)return -1;
if(dot(b, c) < 0) return 2;
if(norm(b) < norm(c)) return -2;
return 0;
}
typedef vector<Point> Polygon;
bool isconvex(const Polygon& g){
int n = (int)g.size();
for(int i=0; i<n; ++i){
if(ccw(g[i], g[(i+1) % n], g[(i+2) % n]) == -1)return false;
}
return true;
}
int main(){
int n;
cin >> n;
Polygon g;
while(n--){
int x, y;
cin >> x >> y;
g.emplace_back(x, y);
}
cout << (isconvex(g)? 1: 0) << '\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
const ld eps = 1e-8, pi = acos(-1.0);
bool eq(ld a, ld b) {
return abs(a - b) < eps;
}
using point = complex<ld>;
ld dot(point a, point b) {
return real(conj(a) * b);
}
ld cross(point a, point b) {
return imag(conj(a) * b);
}
int ccw(point a, point b, point c) {
b -= a, c -= a;
if (cross(b, c) > eps) return 1; // a,b,c counter-clockwise
if (cross(b, c) < -eps) return -1; // a,b,c clockwise
if (dot(b, c) < 0) return 2; // c,a,b on a line
if (norm(b) < norm(c)) return -2; // a,b,c on a line
return 0; // a,c,b on a line
}
struct line {
point a, b;
line() : a(), b() {}
line(const point& a_, const point &b_)
: a(a_), b(b_) {}
};
bool isis_ll(line l, line m) {
return abs(cross(l.b - l.a, m.b - m.a)) > eps;
}
bool isis_ls(line l, line s) {
return (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}
bool isis_lp(line l, point p) {
return abs(cross(l.b - p, l.a - p)) < eps;
}
bool isis_sp(line s, point p) {
return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}
point proj(line l, point p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
point is_ll(line s, line t) {
point sv = s.b - s.a, tv = t.b - t.a;
assert(cross(sv, tv) != 0);
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
bool isis_ss(line s, line t) {
if (isis_ll(s, t)) return isis_ls(s, t) && isis_ls(t, s);
return isis_sp(s, t.a) || isis_sp(s, t.b) || isis_sp(t, s.a);
}
ld dist_lp(line l, point p) {
return abs(p - proj(l, p));
}
ld dist_ll(line l, line m) {
return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}
ld dist_ls(line l, line s) {
return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}
ld dist_sp(line s, point p) {
point r = proj(s, p);
return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}
ld dist_ss(line s, line t) {
if (isis_ss(s, t)) return 0;
return min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });
}
vector<point> convex_hull(vector<point> ps) {
sort(ps.begin(), ps.end(), [](const point& a, const point& b) {
return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
});
int k = 0;
const int n = ps.size();
vector<point> qs(n + 1);
for (int i = 0; i < n; ++i) {
while (k > 1 && ccw(qs[k - 2], qs[k - 1], ps[i]) <= 0) {
k--;
}
qs[k++] = ps[i];
}
for (int i = n - 2, t = k; i >= 0; --i) {
while (k > t && ccw(qs[k - 2], qs[k - 1], ps[i]) <= 0) {
k--;
}
qs[k++] = ps[i];
}
qs.resize(k - 1);
return qs;
}
bool is_convex(const vector<point>& ps) {
const int n = ps.size();
if (n < 3) return false;
for (int i = 0; i < n; i++) {
int c = ccw(ps[i], ps[(i + 1) % n], ps[(i + 2) % n]);
if (c == -1) return false;
}
return true;
}
int main()
{
int n;
cin >> n;
vector<point> ps;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
ps.emplace_back(x, y);
}
cout << is_convex(ps) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
const int MAX_N = 100005;
double add(double a,double b)
{
if(abs(a+b) < EPS * (abs(a) + abs(b))){
return 0;
}
return a + b;
}
struct P
{
double x,y;
P() {}
P(double x,double y) : x(x),y(y) {}
P operator + (P p){
return P(add(x,p.x),add(y,p.y));
}
P operator - (P p){
return P(add(x,-p.x),add(y,-p.y));
}
P operator * (double d){
return P(x*d,y*d);
}
double dot(P p){
return add(x*p.x,y*p.y);
}
double det(P p){
return add(x*p.y,-y*p.x);
}
};
int n;
P ps[MAX_N];
bool cmp_x(const P& p,const P& q)
{
if(p.x != q.x){
return p.x < q.x;
}
return p.y < q.y;
}
vector<P> convex_hull(P* ps,int n)
{
sort(ps,ps+n,cmp_x);
int k = 0;
vector<P> qs(n*2);
rep(i,n){
while(k > 1 && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) < 0){ //????????????=?????\????????¨?????????????????????
k--;
}
qs[k++] = ps[i];
}
for(int i = n-2,t=k;i >= 0;i--){
while(k > t && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) < 0){
k--;
}
qs[k++] = ps[i];
}
qs.resize(k-1);
return qs;
}
double dist(P p,P q)
{
return (p-q).dot(p-q);
}
int main()
{
scanf("%d",&n);
rep(i,n){
int a,b;
scanf("%d%d",&a,&b);
ps[i] = P(a,b);
}
vector<P> qs = convex_hull(ps,n);
if(n <= qs.size()){ //???????????°??????????????´????????????
cout << "1\n";
}else{
cout << "0\n";
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(){}
point(double _x,double _y){
x=_x;y=_y;
}
point operator +(const point &b) const{
return point(x+b.x,y+b.y);
}
point operator -(const point &b) const{
return point(x-b.x,y-b.y);
}
double operator ^(const point &b) const{
return (x*b.x)+(y*b.y);
}
double operator *(const point &b) const{
return x*b.y-y*b.x;
}
point operator *(const double &b) const{
return point(x*b,y*b);
}
};
point a[105];
int main(){
int n;
cin>>n;
for(int i=0;i<n;++i)
cin>>a[i].x>>a[i].y;
double ans=0;
for(int i=0;i<n;++i)
if((a[(i+1)%n]-a[i])*(a[(i+2)%n]-a[i])<0)puts("0"),exit(0);
puts("1");
return 0;
}
|
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define REP(i, lim) for(int i=0;i<lim;++i)
#define REPP(i, lim) for(int i=1;i<=lim;++i)
#define DEC(i, lim) for(int i=lim;i>=1;--i)
#define FOR(i,l,r) for(int i=l;i<r;++i)
#define deBug cout<<"==================================="<<endl;
#define clr(s) memset(s, 0, sizeof(s))
#define lowclr(s) memset(s, -1, sizeof(s))
const int MAXN = 1000055;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const db eps = 1e-9;
inline int sgn(db x) { return x<-eps ? -1 : x>eps; }
inline db sqr(db x) { return x*x; }
#define cross(p1, p2, p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))
#define crossOp(p1, p2, p3) sgn(cross(p1, p2, p3))
struct P {
// information
P() {}
P(db _x, db _y) : x(_x), y(_y) {}
db x, y;
// operation
P operator + (P p) const{ return P(x+p.x, y+p.y); }
P operator - (P p) const{ return P(x-p.x, y-p.y); }
P operator * (db d) const{ return P(x*d, y*d); }
P operator / (db d) const{ return P(x/d, y/d); }
db dot(P p) { return x * p.x + y * p.y; } // 点积
db det(P p) { return x * p.y - y * p.x; } // 叉积
// other
void input() { scanf("%lf%lf", &x, &y); }
void print() { printf("(%lf, %lf)\n", x, y); }
db disTo(P p) { return sqrt(sqr(x-p.x) + sqr(y-p.y)); }
db abs() { return sqrt(x*x + y*y); }
db abs2(){ return x*x + y*y; }
db getw(){ return atan2(y,x); }
int getP() const{ return sgn(y)==1||(sgn(y)==0&&sgn(x)>=0); }
};
int cmpAngle (P a, P b){
if(a.getP()!=b.getP()) return a.getP()<b.getP();
else return sgn(a.det(b)) > 0;
}
bool chkLL(P p1, P p2, P q1, P q2) {
db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
return sgn(a1+a2) != 0;
}
P getLL(P p1, P p2, P q1, P q2) {
db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
return (p1 * a2 + p2 * a1) / (a1 + a2);
}
bool intersect(db l1, db r1, db l2, db r2) {
if(l1>r1) swap(l1, r1); if(l2>r2) swap(l2, r2);
return !( sgn(r1-l2) == -1 || sgn(r2-l1) == -1 );
}
bool chkSS(P p1, P p2, P q1, P q2) {
return intersect(p1.x, p2.x, q1.x, q2.x) && intersect(p1.y, p2.y, q1.y, q2.y) &&
crossOp(p1, p2, q1) * crossOp(p1, p2, q2) <= 0 &&
crossOp(q1, q2, p1) * crossOp(q1, q2, p2) <=0;
}
bool chkSS_strict(P p1, P p2, P q1, P q2) {
return crossOp(p1, p2, q1) * crossOp(p1, p2, q2) < 0 &&
crossOp(q1, q2, p1) * crossOp(q1, q2, p2) < 0;
}
bool inMiddle(db a, db m, db b) { return sgn(a-m)==0 || sgn(b-m)==0 || (a<m != b<m); }
bool inMiddle(P a, P m, P b) { return inMiddle(a.x, m.x, b.x) && inMiddle(a.y, m.y, b.y); }
bool onSeg(P p1, P p2, P q) { return crossOp(p1, p2, q) == 0 && inMiddle(p1, q, p2); }
bool onSeg_strict(P p1, P p2, P q) {
return crossOp(p1, p2, q) == 0 && sgn((q-p1).dot(p1-p2)) * sgn((q-p2).dot(p1-p2)) < 0;
}
P getProj(P a, P b, P p) { return a + (b-a) * ( (b-a).dot(p-a) / sqr(a.disTo(b)));}
P getReflect(P a, P b, P p) { return getProj(a, b, p) * 2 - p; }
db nearest(P p1, P p2, P q) {
P h = getProj(p1, p2, q);
if(inMiddle(p1, h, p2)) return q.disTo(h);
return min(p1.disTo(q), p2.disTo(q));
}
db disSS(P p1, P p2, P q1, P q2) {
if(chkSS(p1, p2, q1, q2)) return 0;
return min(min(nearest(p1,p2,q1), nearest(p1,p2,q2)), min(nearest(q1,q2,p1),nearest(q1,q2,p2)));
}
vector<P> convexHull (vector<P> ps) {
int n = ps.size(); if(n<=1) return ps;
sort(ps.begin(), ps.end(), cmpAngle);
vector<P> qs(n*2); int k = 0;
for(int i=0;i<n;qs[k++]=ps[i++])
while(k>1 && crossOp(qs[k-2], qs[k-1], ps[i])<=0) --k;
for(int i=n-2,t=k;i>=0;qs[k++]=ps[i--])
while(k>t && crossOp(qs[k-2], qs[k-1], ps[i])<=0) --k;
qs.resize(k-1);
return qs;
}
vector<P> convexHull_noStrict (vector<P> ps) {
int n = ps.size(); if(n<=1) return ps;
sort(ps.begin(), ps.end(), cmpAngle);
vector<P> qs(n*2); int k = 0;
for(int i=0;i<n;qs[k++]=ps[i++])
while(k>1 && crossOp(qs[k-2], qs[k-1], ps[i])<0) --k;
for(int i=n-2,t=k;i>=0;qs[k++]=ps[i--])
while(k>t && crossOp(qs[k-2], qs[k-1], ps[i])<0) --k;
qs.resize(k-1);
return qs;
}
int chkConvex(vector<P> ps){
int n = ps.size();
ps.push_back(ps[0]); ps.push_back(ps[1]);
for(int i=0;i<n;++i) if(sgn((ps[i+1]-ps[i]).det(ps[i+2]-ps[i]))==-1) return 0;
return 1;
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// std::ios::sync_with_stdio(false); cin.tie(0);
vector<P> poly;
int n;
scanf("%d", &n);
REP(i, n) {
P tmp; tmp.input();
poly.push_back(tmp);
}
cout<<chkConvex(poly)<<endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <iomanip>
class Point{
public:
int x, y;
Point(int ax = 0, int ay = 0):x(ax),y(ay){};
};
int sub_area(const Point &p1, const Point &p2){
return (p1.x - p2.x) * (p1.y + p2.y);
}
bool judge(const Point &p1, const Point &p2, const Point &p3){
return (p1.x * p3.y + p2.x * p1.y + p3.x * p2.y) <= (p1.x * p2.y + p2.x * p3.y + p3.x * p1.y);
}
bool fold(std::vector<Point> vec){
bool flag = true;
for (auto i = 0; i < vec.size() and flag; ++i){
flag = flag && judge(vec.at((i - 1) % vec.size()), vec.at(i % vec.size()), vec.at((i + 1) % vec.size()));
}
return flag;
}
int main(){
int n;
std::cin >> n;
std::vector<Point> vec;
for (auto i = 0; i < n; ++i){
int x, y;
std::cin >> x >> y;
vec.push_back(Point(x, y));
}
if (fold(vec)){
std::cout << 1 << std::endl;
}else{
std::cout << 0 << std::endl;
}
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define EPS (1E-10)
const int maxn=100000+10;
int n;
struct Point
{
int x,y;
Point() {}
Point(int _x,int _y):x(_x),y(_y) {}
Point operator -(Point p)
{
return Point(x-p.x,y-p.y);
}
bool operator <(Point p)
{
return x==p.x?y<p.y:x<p.x;
}
int cross(Point p)
{
return x*p.y-y*p.x;
}
int dot(Point p)
{
return x*p.x+y*p.y;
}
int norm()
{
return x*x+y*y;
}
};
vector<Point> ps;
int ccw(Point p,Point p1,Point p2)
{
#define COUNT_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_FRONT 2
#define ONLINE_BACK -2
#define ON_SEGMENT 0
Point a=p1-p;
Point b=p2-p;
if(a.cross(b)<-EPS) return CLOCKWISE;
if(fabs(a.cross(b)<EPS)) return CLOCKWISE;
if(a.cross(b)>EPS) return COUNT_CLOCKWISE;
if(a.dot(b)<-EPS) return ONLINE_BACK;
if(a.norm()<b.norm()) return ONLINE_FRONT;
return ON_SEGMENT;
}
bool is_convex(vector<Point> ps)
{
int n=(int)ps.size();
for(int i=0;i<n;i++)
{
if(ccw(ps[(i+1)%n],ps[(i)%n],ps[(i+2)%n])==COUNT_CLOCKWISE) return false;
}
return true;
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&n);
int x,y;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
ps.push_back(Point(x,y));
}
// vector<Point> qs=convex_hull(ps);
if(is_convex(ps)) printf("1\n");
else printf("0\n");
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef complex<double> qua;
vector<qua> v;
const double epx=1e-7;
int ccw(qua a,qua b,qua c)
{
b-=a,c-=a,a=c*conj(b);
if(a.imag()>epx) return 1;
if(a.imag()<-epx) return -1;
if(a.real()<-epx) return 2;
if(abs(b)+epx<abs(c)) return -2;
return 0;
}
bool judge_convex(vector<qua> &ve)
{
int len_ve=ve.size();
int flag1=1,flag2=1;
for(int i=0;i<len_ve;i++)
{
if(ccw(ve[i],ve[(i+1)%len_ve],ve[(i+2)%len_ve])==-1) flag1=0;
if(ccw(ve[i],ve[(i+1)%len_ve],ve[(i+2)%len_ve])==1) flag2=0;
}
return flag1|flag2;
}
int main()
{
int n;
double x,y;
scanf("%d",&n);
while(n--)
{
scanf("%lf%lf",&x,&y);
v.push_back(qua(x,y));
}
printf("%d\n",judge_convex(v));
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ld = long double; using ull = unsigned long long; using uint = unsigned int;
using vi = vector<int>; using vb = vector<bool>; using vd = vector<double>; using vl = vector<ll>;
using vvi = vector<vi>; using vvb = vector<vb>; using vvd = vector<vd>; using vvl = vector<vl>;
#define REP(i,n) for(ll i=0; i<(n); ++i)
#define FOR(i,b,n) for(ll i=(b); i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define TEN(x) ((ll)1e##x)
template<typename T> inline string join(const vector<T>& vec, string sep = " ") { stringstream ss; REP(i, vec.size()) ss << vec[i] << ( i+1 == vec.size() ? "" : sep ); return ss.str(); }
///////////////
#define EPS (1e-10)
#define NEXT(x, i) (x[(i + 1) % x.size()])
template<class T> using CR = const T &;
using P = complex<ld>;
using G = vector<P>;
int sgn(ld a, ld b = 0) {
if (a > b + EPS) return 1;
if (a < b - EPS) return -1;
return 0;
}
ld dot(P a, P b) { return real(conj(a)*b); }
ld cross(P a, P b) { return imag(conj(a)*b); }
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (sgn(cross(b, c))) return sgn(cross(b, c)); // clockwise or counter clockwise
if (sgn(dot(b, c)) == -1) return 2; // c--a--b
if (sgn(norm(b), norm(c)) == -1) return -2; // a--b--c
return 0; // a--c--b
}
bool is_convex(CR<G> g, bool strict = false) {
REP(i, g.size()) {
int c = ccw(g[(i + g.size() - 1) % g.size()], g[i], g[(i + 1) % g.size()]);
if (c == -1 || (strict && c != 1)) return false;
}
return true;
}
//////////////
int main() {
#ifdef INPUT_FROM_FILE
ifstream cin("sample.in");
ofstream cout("sample.out");
#endif
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
ll n; cin >> n;
G g(n);
REP(i,n){
ld x, y;
cin >> x >> y;
g[i] = { x, y };
}
cout << is_convex(g) << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double louble;
template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}
namespace ae86
{
const int bufl = 1<<15;
char buf[bufl],*s=buf,*t=buf;
inline int fetch()
{
if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
return *s++;
}
inline int ty()
{
int a=0,b=1,c=fetch();
while(!isdigit(c))b^=c=='-',c=fetch();
while(isdigit(c))a=a*10+c-48,c=fetch();
return b?a:-a;
}
}
using ae86::ty;
const double eps = 1e-8;
inline int dcmp(double x){if(-eps<x && x<eps)return 0;return x>0?1:-1;}
struct points
{
double x,y;
points(double _x=0,double _y=0){x=_x,y=_y;}
inline friend points operator + (points a,points b){return points(a.x+b.x,a.y+b.y);}
inline friend points operator - (points a,points b){return points(a.x-b.x,a.y-b.y);}
inline friend points operator * (points a,double b){return points(a.x*b,a.y*b);}
inline friend points operator / (points a,double b){return points(a.x/b,a.y/b);}
inline friend int operator == (points a,points b){return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;}
void takein(){x=ty(),y=ty();}
void print(char ends='\n'){printf("%.12lf %.12lf",x,y),putchar(ends);}
};
inline double dox(points a,points b){return a.x*b.x+a.y*b.y;}
inline double cox(points a,points b){return a.x*b.y-a.y*b.x;}
inline double length(points a){return sqrt(dox(a,a));}
inline double distan(points a,points b){return length(a-b);}
inline points pervec(points a){if(dcmp(length(a))<=0)return points(0,0);return a/length(a);}
inline int isponl(points p,points a,points b)
{
return dcmp(cox(p-a,p-b))==0 && distan(a,p)<=distan(a,b) && distan(b,p)<=distan(a,b);
}
inline int iscrashed(points al,points ar,points bl,points br)
{
double amix=min(al.x,ar.x),amxx=max(al.x,ar.x),amiy=min(al.y,ar.y),amxy=max(al.y,ar.y);
double bmix=min(bl.x,br.x),bmxx=max(bl.x,br.x),bmiy=min(bl.y,br.y),bmxy=max(bl.y,br.y);
if(amxx<bmix || bmxx<amix || amxy<bmiy || bmxy<amiy)return 0;
if(isponl(al,bl,br) || isponl(ar,bl,br) || isponl(bl,al,ar) || isponl(br,al,ar))return 1;
if(dcmp(cox(al-bl,br-bl))!=dcmp(cox(ar-bl,br-bl))
&& dcmp(cox(bl-al,ar-al))!=dcmp(cox(br-al,ar-al)))return 1;
return 0;
}
inline points crash(points al,points ar,points bl,points br)
{
points a=ar-al,b=br-bl;
double bas=cox(al-bl,b)/cox(b,a);
return al+a*bas;
}
inline points ortho(points p,points a,points b)
{
points c=a+pervec(b-a);
double dis=dox(p-a,c-a);
points tar=a+(c-a)*dis;
return tar;
}
inline double distanpl(points p,points a,points b)
{
if(isponl(p,a,b))return 0;
points ort=ortho(p,a,b);
if(isponl(ort,a,b))return distan(p,ort);
return min(distan(p,a),distan(p,b));
}
const int _ = 107;
int n;
points p[_];
int main()
{
n=ty();
for(int i=1;i<=n;i++)p[i].takein();
p[0]=p[n],p[n+1]=p[1],p[n+2]=p[2];
for(int i=1;i<=n;i++)
if(dcmp(cox(p[i+1]-p[i],p[i+2]-p[i]))<0){puts("0");return 0;}
puts("1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define db long double
const db eps = 1e-6;
int sgn(db a, db b = 0) {
a -= b;
return (a > eps) - (a < -eps);
}
struct poi {
db x, y;
void r() { cin >> x; cin >> y; }
void w() { cout << x << ' ' << y << '\n'; }
poi operator -(poi p) {
return {x - p.x, y - p.y};
}
poi operator +(poi p) {
return {x + p.x, y + p.y};
}
poi operator *(db d) {
return {x * d, y * d};
}
poi operator /(db d) {
return {x / d, y / d};
}
bool operator ==(poi p) {
return !sgn(x, p.x) && !sgn(y, p.y);
}
db dot(poi p) {
return x * p.x + y * p.y;
}
db cross(poi p) {
return x * p.y - y * p.x;
}
db len() { return sqrt(x * x + y * y); }
db len2() { return x * x + y * y; }
poi proj(poi p, poi q) {
db s = (*this - p).dot(q - p) / (q - p).len2();
return p + (q - p) * s;
}
db dis(poi p, poi q) {
if((*this - p).dot(q - p) < 0) return (*this - p).len();
if((*this - q).dot(p - q) < 0) return (*this - q).len();
return abs((*this - p).cross(q - p) / (q - p).len());
}
};
db xmul(poi a, poi b, poi c) {
return (b - a).cross(c - a);
}
int ccw(poi a, poi b, poi c) {
poi u = b - a, v = c - a;
if(u.cross(v) > eps) return 1;
if(u.cross(v) < -eps) return -1;
if(u.dot(v) < -eps) return -2;
if(u.len2() + eps < v.len2()) return 2;
return 0;
}
bool si(poi a, poi b, poi c, poi d) {
return ccw(a, b, c) * ccw(a, b, d) <= 0 &&
ccw(c, d, a) * ccw(c, d, b) <= 0;
}
poi li(poi a, poi b, poi c, poi d) {
db u = xmul(c, d, a), v = xmul(c, d, b);
return (a * v - b * u) / (v - u);
}
const int N = 111;
poi p[N];
int main() {
ios :: sync_with_stdio(false);
int n; cin >> n;
for(int i = 0; i < n; i ++)
p[i].r();
p[n] = p[0];
bool conv = true;
for(int i = 0; i < n - 1; i ++)
if(ccw(p[i], p[i + 1], p[i + 2]) == -1)
conv = false;
cout << conv << '\n';
return 0;
} |
#include <iostream>
#include <vector>
#include <complex>
using namespace std;
#define X real()
#define Y imag()
#define EPS (1e-10)
typedef complex<double> P;
typedef P Vec;
double dot(Vec v1, Vec v2) {
return (v1*conj(v2)).X;
}
double cross(Vec v1, Vec v2) {
return (v1*conj(v2)).Y;
}
enum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_FRONT, ONLINE_BACK, ON_SEGMENT };
int ccw(P p0, P p1, P p2) {
Vec a = p1 - p0;
Vec b = p2 - p0;
if(cross(a, b) > EPS) return CLOCKWISE;
if(cross(a, b) < -EPS) return COUNTER_CLOCKWISE;
if(dot(a, b) < -EPS) return ONLINE_BACK;
if(norm(a) < norm(b)) return ONLINE_FRONT;
return ON_SEGMENT;
}
typedef vector<P> Polygon;
#define prev(G, i) ((G)[((i)-1+(G).size())%(G).size()])
#define curr(G, i) ((G)[(i)%(G).size()])
#define next(G, i) ((G)[((i)+1+(G).size())%(G).size()])
int main() {
double x, y;
Polygon g;
int n;
cin >> n;
for(int i=0; i<n; i++) {
cin >> x >> y;
g.push_back(P(x, y));
}
for(int i=0; i<n; i++) {
if( ccw(prev(g, i), curr(g, i), next(g, i)) == CLOCKWISE ) {
cout << 0 << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <complex>
#include <iomanip>
using namespace std;
typedef complex<double> P;
typedef vector<P> vec;
const double EPS = 1e-10;
//二つのスカラーが等しいか
#define EQ(a, b) (abs((a) - (b)) < EPS)
//二つのベクトルが等しいか
#define EQV(a, b) (EQ((a), real(), (b).real()) && EQ((a), imag(), (b).imag()))
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
#define prev(P, i) P[(i - 1 + P.size()) % P.size()]
double dot(P a, P b)
{
return real(a) * real(b) + imag(a) * imag(b);
}
double cross(P a, P b)
{
return real(a) * imag(b) - imag(a) * real(b);
}
int is_orthogonal(P a1, P a2, P b1, P b2)
{
return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
int is_parallel(P a1, P a2, P b1, P b2)
{
return EQ(cross(a1 - a2, b1 - b2), 0.0);
}
int ccw(P a, P b, P c)
{
b -= a;
c -= a;
if (cross(b, c) > 0)
return 1;
if (cross(b, c) < 0)
return -1;
if (dot(b, c) < 0) // c--a--b on line
return 2;
if (norm(b) < norm(c)) //a--b--c on line or a==b
return -2;
return 0; // a--c--b on line a==c or b==c
}
int is_intersected_ls(P a1, P a2, P b1, P b2)
{
return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}
double Area(vec &g)
{
double A = 0;
for (int i = 0; i < (int)g.size(); ++i)
{
A += cross(curr(g, i), next(g, i));
}
return A / 2.0;
}
bool is_Convex(const vec &g)
{
for (int i = 0; i < (int)g.size(); ++i)
{
if (ccw(prev(g, i), curr(g, i), next(g, i)) == -1)
return false;
}
return true;
}
int main(void)
{
int N;
cin >> N;
vec v;
for (int i = 0; i < N; ++i)
{
double x, y;
cin >> x >> y;
P c = {x, y};
v.push_back(c);
}
if (is_Convex(v))
cout << "1" << endl;
else
cout << "0" << endl;
}
|
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define X real()
#define Y imag()
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
#define prev(P, i) P[(i+P.size()-1) % P.size()]
const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
bool operator < (const P& a, const P& b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
}
double cross(const P& a, const P& b) {
return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
return real(conj(a)*b);
}
int ccw(P a, P b, P c) {
b -= a; c -= a;
if (cross(b, c) > 0) return +1; // counter clockwise
if (cross(b, c) < 0) return -1; // clockwise
if (dot(b, c) < 0) return +2; // c--a--b on line
if (norm(b) < norm(c)) return -2; // a--b--c on line
return 0;
}
//多角形
typedef vector<P> G;
bool isconvex(const G &P) {
for (int i = 0; i < P.size(); ++i){
//cout <<prev(P, i) <<", " <<curr(P, i) <<", " <<next(P, i) <<endl;
//cout <<ccw(prev(P, i), curr(P, i), next(P, i)) <<endl;
if (ccw(prev(P, i), curr(P, i), next(P, i)) == 1) return false;
}
return true;
}
int N;
int main() {
cin >>N;
G g(N);
REP(i, N){
int x, y; cin >>x >>y;
g[i] = P(x, y);
}
REVERSE(g);
cout <<isconvex(g) <<endl;
return 0;
} |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head
typedef double db;
db eps=1e-9;
struct point {
db x,y;
point() {}
point(db x,db y):x(x),y(y) {}
void read() { scanf("%lf%lf",&x,&y); }
void print() { printf("%.10f %.10f\n",x,y); }
db len() { return sqrt(x*x+y*y); }
db len2() { return x*x+y*y; }
};
point operator + (const point &a,const point &b) { return point(a.x+b.x,a.y+b.y);}
point operator - (const point &a,const point &b) { return point(a.x-b.x,a.y-b.y);}
point operator * (const point &a,const db &b) { return point(a.x*b,a.y*b);}
point operator / (const point &a,const db &b) { return point(a.x/b,a.y/b);}
db dot(const point &a,const point &b) { return a.x*b.x+a.y*b.y; }
db det(const point &a,const point &b) { return a.x*b.y-a.y*b.x; }
struct line {
point a,b;
line() {}
line(point a,point b):a(a),b(b) {}
};
point proj(const line &a,const point &b) {
return a.a+(a.b-a.a)*dot(a.b-a.a,b-a.a)/(a.b-a.a).len2();
}
point reflect(const line &a,const point &b) {
return proj(a,b)*2-b;
}
int sign(db x) {
return fabs(x)<eps?0:(x>0?1:-1);
}
int relation(const line &a,const point &b) {
int x=sign(det(a.b-a.a,b-a.a));
if (x==1) return 0; // COUNTER_CLOCKWISE ?????¶???
else if (x==-1) return 1; // CLOCKWISE ?????¶???
else {
x=sign(dot(b-a.a,a.b-a.a));
if (x==-1) return 2; // ONLINE_BACK
else {
x=sign(dot(b-a.b,a.b-a.a));
if (x==1) return 3; // ONLINE_FRONT
else return 4; // ON_SEGMENT
}
}
}
bool parallel(const line &a,const line &b) { return sign(det(a.a-a.b,b.a-b.b))==0;}
bool ismiddle(const line &a,const point &b) {
return sign(dot(b-a.a,a.b-a.a))>=0&&sign(dot(b-a.b,a.b-a.a))<=0;
}
int crossSS(const line &a,const line &b) {
if (sign(min(b.a.x,b.b.x)-max(a.a.x,a.b.x))==1) return 0;
if (sign(max(b.a.x,b.b.x)-min(a.a.x,a.b.x))==-1) return 0;
if (sign(min(b.a.y,b.b.y)-max(a.a.y,a.b.y))==1) return 0;
if (sign(max(b.a.y,b.b.y)-min(a.a.y,a.b.y))==-1) return 0;
int d1=sign(det(a.b-a.a,b.a-a.a))*sign(det(a.b-a.a,b.b-a.a));
int d2=sign(det(b.b-b.a,a.a-b.a))*sign(det(b.b-b.a,a.b-b.a));
if (d1==-1&&d2==-1) return 2; // ?§??????????
else if (d1<=0&&d2<=0) return 1; // ????§??????????
else return 0; // ?????????
}
point crosspoint(const line &a,const line &b) {
double a1=det(b.b-b.a,a.a-b.a),a2=det(b.b-b.a,a.b-b.a);
return a.a*(a2/(a2-a1))-a.b*(a1/(a2-a1));
}
double distSP(const line &a,const point &b) {
point h=proj(a,b);
if (ismiddle(a,h)) return (b-h).len();
else return min((b-a.a).len(),(b-a.b).len());
}
double distSS(const line &a,const line &b) {
if (crossSS(a,b)>=1) return 0;
else return min(min(distSP(b,a.a),distSP(b,a.b)),min(distSP(a,b.a),distSP(a,b.b)));
}
typedef vector<point> polygon;
double area(polygon &a) { // counter-clockwise
double s=0;
int n=SZ(a);
rep(i,0,n) s+=det(a[i],a[(i+1)%n]);
return 0.5*s;
}
bool isconvex(polygon &a,int ty=0) { // counter-clockwise 0-?????\??? 1-??\??? ????????????
int n=SZ(a);
rep(i,0,n) {
int d=relation(line(a[i],a[(i+1)%n]),a[(i+2)%n]);
if ((ty==0&&(d==1))||(ty==1&&d!=0)) return 0;
}
return 1;
}
int n;
vector<point> T;
int main() {
scanf("%d",&n);
rep(i,0,n) {
point p1;
p1.read();
T.pb(p1);
}
printf("%d\n",isconvex(T));
} |
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define SZ(x) (int)(x).size()
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pll;
const double eps = 1e-10;
const double PI = acos(-1.0);
//const double PI = 3.14159265358979323846264338327950288419716939937510
int dcmp(const double &x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; }
const int MOD = 1e9 + 7;
const int INF = 2e9;
const double INF_d = 1e64;
template<class T>
T qmod(T a, T b){ T ret = 1; while (b){ if (b & 1) ret *= a; b >>= 1; a *= a; } return ret; }
template<class T>
T gcd(T a, T b){ return !b ? a : gcd(b, a % b); }
template<class T>
T ex_gcd(T a, T b, T &x, T &y){
if (!b){ x = 1, y = 0; return a; }
T t, ret;
ret = ex_gcd(b, a % b, x, y);
t = x, x = y, y = t - a / b * y;
return ret;
}
template<class T>
T inv(T t, T p){ return t == 1 ? 1 : (p - p / t) * inv(p % t, p) % p; }
// head
typedef double db;
struct point{
db x, y;
point(){}
point(db a, db b): x(a), y(b){}
point operator + (const point &p){ return point(x + p.x, y + p.y); }
point operator - (const point &p){ return point(x - p.x, y - p.y); }
point operator * (const db &k){ return point(x * k, y * k); }
point operator / (const db &k){ return point(x / k, y / k); }
db operator ^ (const point &p){ return x * p.y - y * p.x; }
db operator * (const point &p){ return x * p.x + y * p.y; }
bool operator == (const point &p){ return !dcmp(x - p.x) && !dcmp(y - p.y); }
bool operator != (const point &p){ return dcmp(x - p.x) || dcmp(y - p.y); }
bool operator < (const point &p){ return x == p.x ? y < p.y : x < p.x; }
void read(){
scanf("%lf%lf", &x, &y);
}
void print(){
printf("%.10f %.10f\n", x, y);
}
point rotate(db &ang){ return point(x * cos(ang) - y * sin(ang), y * cos(ang) + x * sin(ang)); }
point norm() { return point(-y, x); }
db len() { return sqrt(x * x + y * y); }
db len2(){ return x * x + y * y; }
};
point err = point(INF_d, INF_d);
point proj_SP(point A, point B, point P){ // projection point of P in segment AB
point AB = B - A, AP = P - A;
return A + AB * (AP * AB / AB.len2());
}
point refl_SP(point A, point B, point P){ // reflection point of P int segment AB
return proj_SP(A, B, P) * 2 - P;
}
struct line{
point p[2], u;
line(){}
line(point s, point t){
p[0] = s, p[1] = t;
u = p[1] - p[0];
}
point get_point(double t){
return point(p[0] + u * t);
}
};
bool parallel(line L1, line L2){ // check whether two lines L1 and L2 are parallel
return !dcmp(L1.u ^ L2.u);
}
bool orthogonal(line L1, line L2){ // check whether two lines L1 and L2 are orthogonal
return !dcmp(L1.u * L2.u);
}
pair<bool, point> inter_LL(line L1, line L2){ // calculate the intersection of two lines L1 and L2
if (parallel(L1, L2)) return mp(false, err);
double t = ((L2.p[0] - L1.p[0]) ^ L2.u) / (L1.u ^ L2.u);
return mp(true, L1.get_point(t));
}
bool one_dimention_check(db a, db b, db c, db d){ // check whether two one-dimention segments has intersection
if (dcmp(a - b) > 0) swap(a, b);
if (dcmp(c - d) > 0) swap(c, d);
return dcmp(b - c) >= 0 && dcmp(a - d) <= 0;
}
bool check_SS(point A, point B, point C, point D){ // check whether two segments AB and CD has intersection
bool flag = one_dimention_check(A.x, B.x, C.x, D.x) && one_dimention_check(A.y, B.y, C.y, D.y);
double a = dcmp((A - C) ^ (D - C)), b = dcmp((B - C) ^ (D - C)),
c = dcmp((C - A) ^ (B - A)), d = dcmp((D - A) ^ (B - A));
return flag && dcmp(a * b) <= 0 && dcmp(c * d) <= 0;
}
// calculate the intersection point of two segments, first check and then call inter_LL to get the cross point
bool inmid(db a, db b, db c){ // check whether a <= c <= b
return dcmp(dcmp(b - c) * dcmp(a - c)) <= 0;
}
bool inmid(point A, point B, point P){ // check whether point P is in the rectangle of segment AB
return inmid(A.x, B.x, P.x) && inmid(A.y, B.y, P.y);
}
db dis_SP(point A, point B, point P){ // the minimum distance from a point P to segment AB
point p_proj = proj_SP(A, B, P);
if (inmid(A, B, p_proj)) return (P - p_proj).len();
else return min((P - A).len(), (P - B).len());
}
db dis_SS(point A, point B, point C, point D){ // the minimum distance from two segments AB and CD
db res = INF_d;
if (check_SS(A, B, C, D)) res = 0;
res = min(res, dis_SP(A, B, C));
res = min(res, dis_SP(A, B, D));
res = min(res, dis_SP(C, D, A));
res = min(res, dis_SP(C, D, B));
return res;
}
db poly_Area(point *poly, int n){ // calc the area of a simple polygon poly, n is the number of points.
//the points should be in clockwise or counter_clockwise.
db res = 0.0;
for (int i = 1; i < n - 1; i++){
res += (poly[i] - poly[0]) ^ (poly[i + 1] - poly[0]);
}
return fabs(res) / 2;
}
bool is_Convex(point *poly, int n){
poly[n] = poly[0];
if (n < 3) return 1;
for (int i = 1; i < n; i++){
int tmp_dir = dcmp((poly[i] - poly[i - 1]) ^ (poly[(i + 1) % n] - poly[i]));
if (tmp_dir < 0) return 0;
}
return 1;
}
const int N = 123;
int n;
point p[N];
int main(){
scanf("%d", &n);
for (int i = 0; i < n; i++){
p[i].read();
}
printf("%d\n", is_Convex(p, n));
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#define eps 1e-10
#define double long double
using namespace std;
struct point{
double x,y;
void read(){scanf("%Lf%Lf",&x,&y);}
void write(){printf("%.10Lf %.10Lf\n",x,y);}
point(double X=0.0,double Y=0.0):x(X),y(Y){}
point operator +(const point a)const{return point(x+a.x,y+a.y);}
point operator -(const point a)const{return point(x-a.x,y-a.y);}
point operator *(double a)const{return point(x*a,y*a);}
point operator /(double a)const{return point(x/a,y/a);}
bool operator ==(const point a)const{return abs(x-a.x)<=eps && abs(y-a.y)<=eps;}
};
double dis(point a,point b=point(0,0)){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
double dot(point a,point b){return a.x*b.x+a.y*b.y;}
double cross(point a,point b){return a.x*b.y-a.y*b.x;}
double cross3(point o,point a,point b){return cross(a-o,b-o);}
bool jiao(point a,point b,point c,point d)
{
if(min(a.x,b.x)>max(c.x,d.x) || min(a.y,b.y)>max(c.y,d.y)) return false;
if(min(c.x,d.x)>max(a.x,b.x) || min(c.y,d.y)>max(a.y,b.y)) return false;
if(cross3(a,b,c)*cross3(a,b,d)>0) return false;
if(cross3(c,d,a)*cross3(c,d,b)>0) return false;
return true;
}
short int on_line(point o,point a,point b)//oa->ob 0:D 1:U 2:O
{
if(dot(a-o,b-o)<-eps) return 0;
return 1+(dis(a-o)>=dis(b-o));
}
void make_rand(double &x){x=x+(1ll*rand()%10+1)/10.0*eps;}
struct Line{
point a,b;
void read(){a.read();b.read();}
bool operator &&(const Line u){return jiao(a,b,u.a,u.b);}
point operator &(const Line u)
{
if(a==u.a || a==u.b) return a;
if(b==u.a || b==u.b) return b;
double x1=a.x,x2=b.x,x3=u.a.x,x4=u.b.x;
double y1=a.y,y2=b.y,y3=u.a.y,y4=u.b.y;
if(x3==x4) return point(x3,(y2-y1)*x3/(x2-x1)+(y1*x2-x1*y2)/(x2-x1));
if(x1==x2) return point(x1,(y4-y3)*x1/(x4-x3)+(y3*x4-x3*y4)/(x4-x3));
make_rand(x2);
double x=((y3*x4-y4*x3)*(x2-x1)-(y1*x2-y2*x1)*(x4-x3))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));
double y=(y2-y1)*x/(x2-x1)+(y1*x2-x1*y2)/(x2-x1);
return point(x,y);
}
};
point project(Line a,point p)
{
point &p1=a.a,&p2=a.b;
double len=dot(p2-p1,p-p1)/dis(p2-p1);
return p1+(p2-p1)/dis(p2-p1)*len;
}
double dis(Line a,Line b)
{
if(a && b) return 0;
double ans=min(min(dis(a.a,b.a),dis(a.a,b.b)),min(dis(a.b,b.a),dis(a.b,b.b)));
point res=project(a,b.a);
if(on_line(a.a,a.b,res)==2) ans=min(ans,dis(res,b.a));
res=project(a,b.b);
if(on_line(a.a,a.b,res)==2) ans=min(ans,dis(res,b.b));
res=project(b,a.a);
if(on_line(b.a,b.b,res)==2) ans=min(ans,dis(res,a.a));
res=project(b,a.b);
if(on_line(b.a,b.b,res)==2) ans=min(ans,dis(res,a.b));
return ans;
}
struct polygon{
vector<point>p;
void read(int n=-1)
{
if(n<0) scanf("%d",&n);
for(int i=1;i<=n;i++)
{
point a;
a.read();
p.push_back(a);
}
}
double area(void)
{
double ans=0;
for(int i=1;i<p.size();i++)
ans+=cross3(p[0],p[i-1],p[i])/2;
return abs(ans);
}
bool convex(void)
{
short int right=-1;
for(int i=2;i<p.size();i++)
if(right<0) right=cross3(p[i-2],p[i-1],p[i])>=0;
else if(right!=(cross3(p[i-2],p[i-1],p[i])>=0)) return false;
if(right!=(cross3(p[p.size()-2],p[p.size()-1],p[0])>=0)) return false;
return true;
}
}p;
int main()
{
int n;
scanf("%d",&n);
srand(n);
p.read(n);
printf("%d\n",p.convex());
return 0;
}
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <math.h>
#include <stdbool.h>
#include <algorithm>
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
Point operator + (Point p) { return Point(x+p.x, y+p.y); }
Point operator - (Point p) { return Point(x-p.x, y-p.y); }
Point operator * (double a) { return Point(a*x, a*y); }
Point operator / (double a) { return Point(x/a, y/a); }
double abs() {return sqrt(norm()); }
double norm() {return x*x + y*y; }
};
typedef Point Vector;
double dot(Vector a, Vector b) {
return a.x * b.x + a.y * b.y;
}
double cross(Vector a, Vector b) {
return a.x*b.y - a.y*b.x;
}
int main(){
#if 0
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
int n;
cin >> n;
Point i1, i2, p1, p2, p3;
int isConvex = 1;
for(int i=0; i<n; i++){
if(i==0)
{
cin >> i1.x >> i1.y;
cin >> i2.x >> i2.y;
p1 = i1;
p2 = i2;
}
else
{
p1 = p2;
p2 = p3;
}
if(i==n-2){
p3 = i1;
}
else if(i==n-1){
p3 = i2;
}
else{
cin >> p3.x >> p3.y;
}
//cout << p1.x << " " << p1.y << " " << p2.x << " " << p2.y << " " << p3.x << " " << p3.y << "\n";
if( cross(p2-p1,p3-p2) < 0 ){
isConvex = 0;
break;
}
}
cout << isConvex <<"\n";
return 0;
}
|
// #include {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}
typedef ll T;
struct P{
T x,y;
P(){} P(T x,T y):x(x),y(y){}
void read(){scanf("%lld%lld",&x,&y);}
P operator - (const P&b) const {return P(x-b.x,y-b.y);}
P operator + (const P&b) const {return P(x+b.x,y+b.y);}
T operator * (const P&b) const {return x*b.x+y*b.y;}
P operator * (const T&k) const {return P(x*k,y*k);}
T operator ^ (const P&b) const {return x*b.y-y*b.x;}
};
typedef vector<P> polygon;
bool isconvex(polygon A){
bool ok=1;int n=sz(A);
rep(i,0,2) A.pb(A[i]);
rep(i,0,n) ok&=((A[i+1]-A[i])^(A[i+2]-A[i]))>=0;
return ok;
}
int main(){
int n;scanf("%d",&n);
polygon A;
rep(i,0,n){
P p;p.read();
A.pb(p);
}
printf("%d\n",isconvex(A));
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
template <class T> void readPoint(vector< vector<T> >& aVec, int nIdx) {
T x, y;
vector<T> aPoint(2);
std::cin >> x >> y;
aPoint[0] = x;
aPoint[1] = y;
aVec[nIdx] = aPoint;
}
int main()
{
int nSize;
cin >> nSize;
vector< vector<int> > aPoints(nSize);
for (int i = 0; i < nSize; i++) {
readPoint(aPoints, i);
}
int nAns(1), nDir(0), nVecx(0), nVecy(0), nNew_vecx(0), nNew_vecy(0);
for (int i = 0; i <= nSize; i++) {
nNew_vecx = aPoints[(i + 1) % nSize][0] - aPoints[i % nSize][0];
nNew_vecy = aPoints[(i + 1) % nSize][1] - aPoints[i % nSize][1];
int nCross_prod = (nVecx * nNew_vecy - nVecy * nNew_vecx);
if (nCross_prod != 0) {
if (nCross_prod * nDir >= 0) {
nDir = nCross_prod > 0 ? 1 : -1;
}
else {
nAns = 0;
break;
}
}
nVecx = nNew_vecx;
nVecy = nNew_vecy;
}
cout << nAns << "\n";
return 0;
}
|
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n;
struct Point{double x,y;}p[105];
Point operator -(Point a,Point b){return (Point){a.x-b.x,a.y-b.y};}
double cross(Point a,Point b){return a.x*b.y-a.y*b.x;}
void read(Point &a){a.x=read(),a.y=read();}
bool convex()
{
for(int i=0;i<n;i++)
{
int x=(i+1)%n,y=(i+2)%n;
if(cross(p[x]-p[i],p[y]-p[x])<0)return 0;
}
return 1;
}
int main()
{
n=read();
for(int i=0;i<n;i++)read(p[i]);
printf("%d\n",convex());
return 0;
}
|
// CGL_3_B.cpp
//
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
bool EQ(double a, double b){return fabs(a-b) < EPS;}
typedef complex<double> Point;
#define X real()
#define Y imag()
namespace std {
bool operator == (const Point &a, const Point &b) {
return EQ(a.X, b.X) && EQ(a.Y, b.Y);
}
bool operator < (const Point &a, const Point &b) {
return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
}
struct Segment {
Point p1, p2;
Segment(){};
Segment(Point p1, Point p2) : p1(p1), p2(p2) {};
};
typedef Segment Line;
typedef vector<Point> Polygon;
double dot(Point a, Point b){return real(conj(a) * b);}
double cross(Point a, Point b){return imag(conj(a) * b);}
const int COUNTER_CLOCKWISE = +1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = +2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;
int ccw(Point a, Point b, Point c) {
Point x = b - a;
Point y = c - a;
if(cross(x, y) > EPS) return COUNTER_CLOCKWISE;
if(cross(x, y) < -EPS) return CLOCKWISE;
if(dot(x, y) < -EPS) return ONLINE_BACK;
if(norm(x) < norm(y)) return ONLINE_FRONT;
return ON_SEGMENT;
}
#define curr(P,i) P[i]
#define next(P,i) P[(i+1)%P.size()]
#define prev(P,i) P[(i+P.size()-1) % P.size()]
bool isConvex(Polygon P) {
for(int i = 0; i < P.size(); ++i)
if(ccw(prev(P,i), curr(P,i), next(P,i)) == CLOCKWISE) return false;
return true;
}
int main() {
int n,x,y;
Polygon P;
cin>>n;
for(int i=0;i<n;++i){
cin>>x>>y;
P.push_back(Point(x,y));
}
cout<<isConvex(P)<<endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const double EPS = 1e-9;
const double PI = acos(-1);
int sign(double x) {
if (fabs(x) < EPS) {
return 0;
} else if (x > 0) {
return 1;
} else {
return -1;
}
}
int dcmp(double x, double y) {
return sign(x - y);
}
struct Vector;
typedef Vector Point;
struct Vector {
double x;
double y;
explicit Vector(double x = 0, double y = 0);
Vector operator+(const Vector &rhs) const;
Vector operator-() const;
Vector operator-(const Vector &rhs) const;
Vector operator*(double d) const;
Vector operator/(double d) const;
double operator*(const Vector &rhs) const;
double operator^(const Vector &rhs) const;
bool operator==(const Vector &rhs) const;
bool operator!=(const Vector &rhs) const;
bool operator<(const Vector &rhs) const;
bool operator>(const Vector &rhs) const;
bool operator<=(const Vector &rhs) const;
bool operator>=(const Vector &rhs) const;
friend std::ostream &operator<<(std::ostream &os, const Vector &vector);
double dot(const Vector &rhs) const;
double cross(const Vector &rhs) const;
double length() const;
double angle() const;
double angle(const Vector &rhs) const;
Vector rotate(double rad) const;
Vector normal() const;
double getDistanceTo(const Point &rhs) const;
};
Vector::Vector(double x, double y) : x(x), y(y) {}
Vector Vector::operator+(const Vector &rhs) const {
return Vector(x + rhs.x, y + rhs.y);
}
Vector Vector::operator-() const {
return Vector(-x, -y);
}
Vector Vector::operator-(const Vector &rhs) const {
return *this + -rhs;
}
Vector Vector::operator*(double d) const {
return Vector(x * d, y * d);
}
Vector Vector::operator/(double d) const {
return Vector(x / d, y / d);
}
double Vector::operator*(const Vector &rhs) const {
return x * rhs.x + y * rhs.y;
}
double Vector::operator^(const Vector &rhs) const {
return x * rhs.y - rhs.x * y;
}
bool Vector::operator==(const Vector &rhs) const {
return dcmp(x, rhs.x) == 0 && dcmp(y, rhs.y) == 0;
}
bool Vector::operator!=(const Vector &rhs) const {
return !(rhs == *this);
}
bool Vector::operator<(const Vector &rhs) const {
if (dcmp(x, rhs.x) != 0) {
return dcmp(x, rhs.x) < 0;
} else {
return dcmp(y, rhs.y) < 0;
}
}
bool Vector::operator>(const Vector &rhs) const {
return rhs < *this;
}
bool Vector::operator<=(const Vector &rhs) const {
return !(rhs < *this);
}
bool Vector::operator>=(const Vector &rhs) const {
return !(*this < rhs);
}
std::ostream &operator<<(std::ostream &os, const Vector &vector) {
os << "(" << vector.x << ", " << vector.y << ")";
return os;
}
double Vector::dot(const Vector &rhs) const {
return *this * rhs;
}
double Vector::cross(const Vector &rhs) const {
return *this ^ rhs;
}
double Vector::length() const {
return sqrt(*this * *this);
}
double Vector::angle() const {
return atan2(y, x);
}
double Vector::angle(const Vector &rhs) const {
return acos((*this * rhs) / length() / rhs.length());
}
Vector Vector::rotate(double rad) const {
return Vector(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad));
}
Vector Vector::normal() const {
double l = length();
return Vector(-y / l, x / l);
}
double Vector::getDistanceTo(const Point &rhs) const {
return sqrt(pow(x - rhs.x, 2) + pow(y - rhs.y, 2));
}
std::vector<Point> convexHullAndrew(std::vector<Point> &points) {
int n = points.size();
sort(points.begin(), points.end());
std::vector<int> stk(n + 2);
std::vector<bool> used(n + 2, false);
int top = 0;
stk[++top] = 0;
for (int i = 1; i < n; i++) {
while (top >= 2 && sign((points[stk[top]] - points[stk[top - 1]]) ^ (points[i] - points[stk[top]])) < 0) {
used[stk[top--]] = false;
}
used[i] = true;
stk[++top] = i;
}
int tmp = top;
for (int i = n - 2; i >= 0; i--) {
if (!used[i]) {
while (top > tmp && sign((points[stk[top]] - points[stk[top - 1]]) ^ (points[i] - points[stk[top]])) < 0) {
used[stk[top--]] = false;
}
used[i] = true;
stk[++top] = i;
}
}
std::vector<Point> res;
res.reserve(top);
for (int i = 1; i < top; i++) {
res.push_back(points[stk[i]]);
}
return res;
}
int main() {
// freopen("data.in", "r", stdin);
int n;
while (~scanf("%d", &n)) {
vector<Point> points;
points.reserve(n);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
points.emplace_back(x, y);
}
const vector<Point> &vec = convexHullAndrew(points);
if (vec.size() == n) {
puts("1");
} else {
puts("0");
}
}
return 0;
}
|
#include <bits/stdc++.h>
namespace LCY{
#define double long double
const double EPS=1e-10;
inline int dcmp(double x){if(fabs(x)<EPS)return 0;return (x<0)?-1:1;}
struct Point{double x,y;Point(double _x=0,double _y=0){x=_x;y=_y;}};
struct Vector{double x,y;Vector(double _x=0,double _y=0){x=_x;y=_y;}};
Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double d){return Vector(a.x*d,a.y*d);}
Point operator + (Point a,Vector b){return Point(a.x+b.x,a.y+b.y);}
inline double dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
inline double cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
inline double norm(Vector a){return dot(a,a);}
inline double abs(Vector a){return std::sqrt(norm(a));}
struct Line{
Point p1,p2;
Line(Point _p1=Point(0,0),Point _p2=Point(0,0)){p1=_p1;p2=_p2;}
};
inline bool is_parallel(Line l1,Line l2){return dcmp(cross(l2.p2-l2.p1,l1.p2-l1.p1))==0;}
inline bool is_vertical(Line l1,Line l2){return dcmp(dot(l2.p2-l2.p1,l1.p2-l1.p1))==0;}
Point project(Line l,Point p){
Vector base=l.p2-l.p1;
double r=dot(p-l.p1,base)/norm(base);
return l.p1+base*r;
}
Point reflect(Line l,Point p){
Point q=project(l,p);
Vector v=p-q;v.x=-v.x;v.y=-v.y;
return q+v;
}
int ccw(Vector v1,Vector v2){
if(dcmp(cross(v1,v2))>0)return 1;//COUNTER_CLOCKWISE
else if(dcmp(cross(v1,v2))<0)return 2;//CLOCKWISE
else{
if(dcmp(dot(v1,v2))<0)return 3;//ONLINE_BACK
else{
if(dcmp(norm(v1)-norm(v2))<0)return 4;//ONLINE_FRONT
else return 5;//ON_SEGMENT
}
}
}
inline bool is_inter(Line l1,Line l2){
bool flag=1;
int t1=ccw(l1.p2-l1.p1,l2.p1-l1.p1),t2=ccw(l1.p2-l1.p1,l2.p2-l1.p1);
if(t1==5 || t2==5) return true;
else if(t1==t2)return false;
else if(t1>=3 && t2<=2) return false;
else if(t1<=2 && t2>=3) return false;
std::swap(l1,l2);
t1=ccw(l1.p2-l1.p1,l2.p1-l1.p1),t2=ccw(l1.p2-l1.p1,l2.p2-l1.p1);
if(t1==5 || t2==5) return true;
else if(t1==t2)return false;
else if(t1>=3 && t2<=2) return false;
else if(t1<=2 && t2>=3) return false;
return true;
}
inline Point line_intersection(Line l1,Line l2){
if(is_parallel(l1,l2)) return Point(233,233);
Vector u=l1.p1-l2.p1,v=l1.p2-l1.p1,w=l2.p2-l2.p1;
double t=cross(w,u)/cross(v,w);
return l1.p1+v*t;
}
inline Point segment_intersection(Line l1,Line l2){
if(!is_inter(l1,l2)) return Point(233,233);
return line_intersection(l1,l2);
}
inline double line_point_distance(Line l,Point p){
return std::abs(cross(l.p2-l.p1,p-l.p1))/abs(l.p2-l.p1);
}
inline double segment_point_distance(Line l,Point p){
if(dcmp(dot(l.p2-l.p1,p-l.p1))<0) return abs(p-l.p1);
if(dcmp(dot(l.p1-l.p2,p-l.p2))<0) return abs(p-l.p2);
return line_point_distance(l,p);
}
inline double segment_distance(Line l1,Line l2){
#define spd segment_point_distance
if(is_inter(l1,l2))return 0.0;
return std::min(std::min(spd(l1,l2.p1),spd(l1,l2.p2)),std::min(spd(l2,l1.p1),spd(l2,l1.p2)));
}
double area(std::vector<Point> &Q){
int n=Q.size();
double ans=0;
Point o=Point(0.0,0.0);
for(int i=0;i<n;++i)ans+=cross(Q[i]-o,Q[(i+1)%n]-o)/2.0;
return ans;
}
bool is_convex(std::vector<Point> &Q){
int n=Q.size();
Point o=Point(0.0,0.0);
for(int i=0;i<n;++i){
Point lst=Q[(i+n-1)%n],now=Q[i],nxt=Q[(i+1)%n];
if(ccw(now-lst,nxt-lst)==2)return false;
}
return true;
}
}
using namespace LCY;
using namespace std;
int main(){
vector<Point>Q;int n;cin>>n;for(int i=0;i<n;++i){Point p;cin>>p.x>>p.y;Q.push_back(p);}
cout<<is_convex(Q)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define eps 1e-9
#define nmax 200
#define f(c,a,b) for(int c=a; c<=b; c++)
using namespace std;
typedef double db;
struct P{
db x, y;
P(){}
P(db x, db y) : x(x) , y(y) {}
P operator - (P a){ return P(x-a.x, y-a.y); }
db times(P a){ return x*a.y-y*a.x; }
}po[nmax];
typedef P V;
int n;
bool isconvex(){
po[0] = po[n];
f(i,2,n) if( (po[i]-po[i-1]).times(po[i-1]-po[i-2]) > eps ) return false;
if( (po[1]-po[n]).times(po[n]-po[n-1]) > eps ) return false;
return true;
}
int main(){
//freopen("owo.in","r",stdin);
cin >> n;
f(i,1,n) scanf("%lf%lf", &po[i].x, &po[i].y);
printf("%d\n", isconvex());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }
const double eps = 1e-10;
const double inf = 1e+10;
const double pi = acos(-1.0);
int sgn(double a) { return a < -eps ? -1 : a > eps; }
using P = complex<double>;
istream& operator>>(istream& i, P& p) { double x, y; i >> x >> y; p = P(x, y); return i; }
ostream& operator<<(ostream& o, const P& p) { o << real(p) << ' ' << imag(p); return o; }
bool operator<(const P& p, const P& q) { return sgn(real(p) - real(q)) ? sgn(real(p) - real(q)) < 0 : sgn(imag(p) - imag(q)) < 0; }
bool operator>(const P& p, const P& q) { return q < p; }
bool operator<=(const P& p, const P& q) { return !(p > q); }
bool operator>=(const P& p, const P& q) { return !(p < q); }
bool eq(const P& p, const P& q) { return !(p < q) and !(p > q); }
double dot(const P& p, const P& q) { return real(conj(p) * q); }
double cross(const P& p, const P& q) { return imag(conj(p) * q); }
int ccw(P p0, P p1, P p2) {
p1 -= p0, p2 -= p0; // (1)
if (sgn(cross(p1, p2)) > 0) return 1; //
if (sgn(cross(p1, p2)) < 0) return -1; // (-2)---p0---(0)---p1---(2)
if (sgn(dot(p1, p2) < 0)) return -2; //
if (sgn(norm(p1) - norm(p2)) < 0) return 2; // (-1)
return 0;
}
bool convex(V<P>& ps) {
int n = ps.size();
for (int i = 0; i < n; i++) if (ccw(ps[i], ps[(i + 1) % n], ps[(i + 2) % n]) == -1) return false;
return true;
}
int main() {
cin.tie(NULL); ios::sync_with_stdio(false);
int n; cin >> n;
V<P> ps(n); for (int i = 0; i < n; i++) cin >> ps[i];
cout << convex(ps) << '\n';
}
|
/*
判断是否是凸包
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int num;
double x,yy1,yy2,yy3,yy4;
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 110;
int sgn(double x){
if(fabs(x)<eps){
return 0;
}
if(x<0){
return -1;
}else{
return 1;
}
}
struct Point{
double x,y;
Point(){}
void input(){
scanf("%lf%lf",&x,&y);
}
Point(double _x,double _y){
x = _x;y = _y;
}
bool operator < (Point b)const{
return sgn(x-b.x)==0?sgn(y-b.y)<0:x<b.x;
}
Point operator - (const Point &b)const{
return Point(x-b.x,y-b.y);
}
double operator *(const Point &b)const{
return x*b.x+y*b.y;
}
double operator ^(const Point &b)const{
return x*b.y-y*b.x;
}
double distance(Point p){
return hypot(x-p.x,y-p.y);
}
};
struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s = _s;
e = _e;
}
};
struct polygon{
int n;
Point p[maxp];
Line l[maxp];
void input(int _n){
n = _n;
for(int i = 0;i < n;++i ){
p[i].input();
}
}
bool isconvex(){
bool s[3];
memset(s,false,sizeof(s));
for(int i = 0;i < n;++i ){
int j = (i+1)%n;
int k = (j+1)%n;
s[sgn((p[j]-p[i])^(p[k]-p[i]))+1] = true;
if(s[0]&&s[2]){
return false;
}
}
return true;
}
};
int main(){
int N;
scanf("%d",&N);
polygon pp;
pp.input(N);
if(pp.isconvex()){
printf("1\n");
}else{
printf("0\n");
}
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<iomanip>
using namespace std;
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
class Point{
public:
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
Point operator + (Point p){ return Point(x + p.x, y + p.y);}
Point operator - (Point p){ return Point(x - p.x, y - p.y);}
Point operator * (double a){ return Point(a * x, a * y);}
Point operator / (double a){ return Point(x / a, y / a);}
double abs(){ return sqrt(norm());}
double norm(){ return x * x + y * y;}
bool operator < (const Point &p) const{
return x != p.x ? x < p.x : y < p.y;
}
bool operator == (const Point &p) const{
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
typedef Point Vector;
typedef vector<Point> Polygon;
double dot(Vector a, Vector b){
return a.x * b.x + a.y * b.y;
}
double cross(Vector a, Vector b){
return a.x * b.y - a.y * b.x;
}
int ccw(Point p0, Point p1, Point p2){
Vector a = p1 - p0;
Vector b = p2 - p0;
if(cross(a, b) > EPS) return COUNTER_CLOCKWISE;
if(cross(a, b) < -EPS) return CLOCKWISE;
if(dot(a, b) < -EPS) return ONLINE_BACK;
if(a.norm() < b.norm()) return ONLINE_FRONT;
return ON_SEGMENT;
}
int main(){
int n;
cin >> n;
Polygon p(n);
for(int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
for(int i = 0; i < n; i++){
int num = ccw(p[(i + 1) % n], p[i], p[(i + 2) % n]);
if(num == COUNTER_CLOCKWISE){
cout << 0 << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
namespace LibraryOfComputationalGeometry {
typedef long double ld;
const ld eps = 1e-9;
struct point {ld x, y; };
struct line {point a, b; }; //Be sure that a and b are distinct.
void ReadPointInt(point &a) {read(a.x), read(a.y); }
void PrintPoint(point a) {printf("%.10Lf %.10Lf\n", a.x, a.y); }
void ReadLineInt(line &a) {ReadPointInt(a.a), ReadPointInt(a.b); }
void CerrPoint(point a) {cerr << a.x << ' ' << a.y << endl; }
void CerrLine(line a) {cerr << a.a.x << ' ' << a.a.y << ' ' << a.b.x << ' ' << a.b.y << endl; }
point operator + (point a, point b) {return (point) {a.x + b.x, a.y + b.y}; }
point operator - (point a, point b) {return (point) {a.x - b.x, a.y - b.y}; }
point operator * (point a, ld b) {return (point) {a.x * b, a.y * b}; }
ld dot(point a, point b) {return a.x * b.x + a.y * b.y; }
ld operator * (point a, point b) {return a.x * b.y - a.y * b.x; }
ld moo(point a) {return sqrtl(a.x * a.x + a.y * a.y); }
ld dist(point a, point b) {return moo(a - b); }
point unit(point a) {
ld tmp = moo(a);
assert(tmp > eps);
return a * (1.0 / tmp);
}
point Projection(point x, line a) {
ld d = dot(x - a.a, a.b - a.a) / dist(a.a, a.b);
return a.a + unit(a.b - a.a) * d;
}
point Reflection(point x, line a) {
return Projection(x, a) * 2 - x;
}
bool OnLine(point x, line a) {
return fabsl((a.b - a.a) * (x - a.a)) <= eps;
}
bool OnSegment(point x, line a) {
return dist(x, a.a) + dist(x, a.b) - dist(a.a, a.b) <= eps;
}
bool Parallel(line a, line b) {
return fabsl((a.b - a.a) * (b.b - b.a)) <= eps;
}
bool Orthogonal(line a, line b) {
return fabsl(dot(a.b - a.a, b.b - b.a)) <= eps;
}
bool SegmentIntersect(line a, line b) {
if (Parallel(a, b)) return OnSegment(b.a, a) || OnSegment(b.b, a) || OnSegment(a.a, b) || OnSegment(a.b, b);
ld tmp = ((a.b - a.a) * (b.a - a.a)) * ((a.b - a.a) * (b.b - a.a));
ld tnp = ((b.b - b.a) * (a.a - b.a)) * ((b.b - b.a) * (a.b - b.a));
return tmp <= eps && tnp <= eps;
}
point LineIntersection(line a, line b) {
assert(!Parallel(a, b));
ld tmp = (b.a - a.a) * (b.b - a.a);
ld tnp = (b.b - a.b) * (b.a - a.b);
return (a.a * tnp + a.b * tmp) * (1 / (tmp + tnp));
}
ld DistanceToSegment(point x, line a) {
point tmp = Projection(x, a);
if (OnSegment(tmp, a)) return dist(x, tmp);
else return min(dist(x, a.a), dist(x, a.b));
}
}
using namespace LibraryOfComputationalGeometry;
point a[MAXN];
int main() {
int n; read(n);
for (int i = 1; i <= n; i++)
ReadPointInt(a[i]);
a[n + 1] = a[1], a[n + 2] = a[2];
for (int i = 2; i <= n + 1; i++)
if ((a[i] - a[i - 1]) * (a[i + 1] - a[i]) < -eps) {
printf("0\n");
return 0;
}
printf("1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
constexpr double EPS = 1e-14;
struct vec2 {
double x, y;
vec2 operator+(const vec2 rhs) {
return {x + rhs.x, y + rhs.y};
}
vec2 operator-(const vec2 rhs) {
return {x - rhs.x, y - rhs.y};
}
vec2 operator*(const double k) {
return {x * k, y * k};
}
vec2 operator/(const double k) {
return {x / k, y / k};
}
};
void printvec2(vec2 p, int precision) {
cout << setprecision(precision) << fixed << p.x << " " << p.y << endl;
}
double dot(vec2 a, vec2 b) {
return a.x * b.x + a.y * b.y;
};
double norm2(vec2 a) {
return dot(a, a);
}
struct line {
vec2 p, l;
};
line make_line(vec2 a, vec2 b) {
return {a, b - a};
}
double cross(vec2 a, vec2 b) {
return a.x * b.y - a.y * b.x;
}
bool is_parallel(vec2 a, vec2 b) {
return abs(cross(a, b)) < EPS;
}
bool is_parallel(line a, line b) {
return is_parallel(a.l, b.l);
}
double operator/(vec2 a, vec2 b) {
assert(is_parallel(a, b));
return abs(b.x) < EPS ? a.y / b.y : a.x / b.x;
}
vec2 intersection(line l1, line l2) {
assert(!is_parallel(l1, l2));
double a = l1.p.x, b = l1.p.y, c = l1.l.x, d = l1.l.y;
double e = l2.p.x, f = l2.p.y, g = l2.l.x, h = l2.l.y;
double k = (h * (a - e) - g * (b - f)) / (g * d - c * h);
return {
a + k * c,
b + k * d
};
}
vec2 get_normal(vec2 p) {
return {-p.y, p.x};
}
vec2 foot(vec2 p, line l) {
line normal = {p, get_normal(l.l)};
return intersection(normal, l);
}
vec2 reflect(vec2 p, line l) {
vec2 f = foot(p, l);
return {2 * f.x - p.x, 2 * f.y - p.y};
}
double dist2(vec2 p, line l) {
return norm2(foot(p, l) - p);
}
double dist2(vec2 p, vec2 q) {
return norm2(q - p);
}
int sgn(double a) {
if (a < -EPS) return -1;
if (a > EPS) return 1;
return 0;
}
struct segment {
vec2 a, b;
};
segment make_segment(vec2 a, vec2 b) {
return {a, b};
}
double dist2(vec2 p, segment s) {
vec2 l = s.a, r = s.b;
for (int i = 0; i < 100; i++) {
vec2 ml = (l + r) / 3;
vec2 mr = (l + r) * 2 / 3;
if (dist2(p, ml) > dist2(p, mr)) {
l = ml;
} else {
r = mr;
}
}
return dist2(p, l);
}
template<class T>
struct ext {
double val;
T x, y;
};
template<class T>
ext<T> ternary_2d(function<double(T, T)> f, T xl, T xr, T yl, T yr, bool maximum = true) {
if (!maximum) {
function<double(T, T)> g = [&](T a, T b){return -f(a, b);};
ext<T> ret = ternary_2d(g, xl, xr, yl, yr, true);
return {-ret.val, ret.x, ret.y};
}
// fix y
function<ext<T>(T, T, T)> fx = [&](T y, T l, T r) {
for (int i = 0; i < 100; i++) {
T ml = (l * 2 + r) / 3.0;
T mr = (l + r * 2) / 3.0;
if (f(ml, y) < f(mr, y)) {
l = ml;
} else {
r = mr;
}
}
ext<T> ret;
ret.x = l;
ret.y = y;
ret.val = f(l, y);
return ret;
};
for (int i = 0; i < 100; i++) {
T ml = (yl * 2 + yr) / 3.0;
T mr = (yl + yr * 2) / 3.0;
if (f(fx(ml, xl, xr).x, ml) < f(fx(mr, xl, xr).x, mr)) {
yl = ml;
} else {
yr = mr;
}
}
ext<T> ret;
ret.x = fx(yl, xl, xr).x;
ret.y = yl;
ret.val = f(ret.x, ret.y);
return ret;
}
double dist2(segment s, segment t) {
ext<vec2> ans;
function<double(vec2, vec2)> d = [&](vec2 a, vec2 b) {
return norm2(b - a);
};
ans = ternary_2d(d, s.a, s.b, t.a, t.b, false);
return ans.val;
}
vec2 intersection(segment s, segment t) {
ext<vec2> ans;
function<double(vec2, vec2)> d = [&](vec2 a, vec2 b) {
return norm2(b - a);
};
ans = ternary_2d(d, s.a, s.b, t.a, t.b, false);
assert(ans.val < EPS);
return {ans.x.x, ans.x.y};
}
struct triangle {
// counter clockwise
vec2 a, b, c;
};
double area(triangle t) {
return cross(t.b - t.a, t.c - t.a) / 2;
}
// counter clockwise
using polygon = vector<vec2>;
double area(polygon p) {
double ret = 0;
assert(p.size() >= 3);
for (int i = 1; i < p.size() - 1; i++) {
triangle t = {p.front(), p[i], p[i + 1]};
ret += area(t);
}
return ret;
}
int ccw(vec2 a, vec2 b, vec2 c) {
b = b - a;
c = c - a;
int s = sgn(cross(b, c));
if (s == -1) {
// CLOCKWISE
return -1;
} else if (s == 1) {
// COUNTER_CLOCKWISE
return 1;
} else {
double k = c / b;
if (k < -EPS) {
// ONLINE_BACK
return -2;
} else if (k > 1 + EPS) {
// ONLINE_FRONT
return 2;
} else {
// ON_SEGMENT
return 0;
}
}
}
bool is_convex(polygon p, bool strict = true) {
int n = p.size();
assert(p.size() >= 3);
p.push_back(p[0]);
p.push_back(p[1]);
for (int i = 0; i < n; i++) {
if (strict) {
if (ccw(p[i], p[i + 1], p[i + 2]) != 1) {
return false;
}
} else {
if (ccw(p[i], p[i + 1], p[i + 2]) == -1) {
return false;
}
}
}
return true;
}
int main() {
int n;
cin >> n;
polygon p;
for (int i = 0; i < n; i++) {
double x, y;
cin >> x >> y;
p.push_back({x, y});
}
cout << (is_convex(p, false) ? 1 : 0) << endl;
}
|
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using flt = double;
const flt eps = 1e-12, inf = 1e18, PI = acos(-1.0);
template<typename T>
inline T sqr(T x) {return x * x;}
inline flt cmp(flt a, flt b, flt e = eps) {
return fabs(a - b) >= e + fabs(a) * e ? a - b : 0;
}
inline int sgn(flt x, flt e = eps) {return x < -e ? -1 : (x > e);}
inline flt fix(flt x, flt e = eps) {return cmp(x, 0, e);}
struct point {
flt x, y;
point(flt x = 0, flt y = 0): x(x), y(y) {}
bool operator < (const point &rhs) const {
return cmp(x, rhs.x) < 0 || (cmp(x, rhs.x) == 0 && cmp(y, rhs.y) < 0);
}
bool operator == (const point &rhs) const {
return cmp(x, rhs.x) == 0 && cmp(y, rhs.y) == 0;
}
point operator + (const point &rhs) const {
return point(x + rhs.x, y + rhs.y);
}
point operator - (const point &rhs) const {
return point(x - rhs.x, y - rhs.y);
}
point operator * (const flt k) const {
return point(x * k, y * k);
}
point operator / (const flt k) const {
return point(x / k, y / k);
}
point operator ~ () const {// counter clockwise rotate 90 degree
return point(-y, x);
}
flt dot(const point &rhs) const {
return x * rhs.x + y * rhs.y;
}
flt det(const point &rhs) const {
return x * rhs.y - y * rhs.x;
}
flt norm2() const {
return x * x + y * y;
}
flt norm() const {
return hypot(x, y);
}
point rot(flt a) const {// counter clockwise rotate A rad
return point(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));
}
point rot(flt cosa, flt sina) const {// counter clockwise rotate using cos/sin
return point(x * cosa - y * sina, x * sina + y * cosa);
}
point trunc(flt a = 1.0) const {
return (*this) * (a / this->norm());
}
};
int main() {
int n;
scanf("%d", &n);
std::vector<point> p(n);
for (int i = 0; i < n; ++i) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for (int i = 0; i < n; ++i) {
const point &A = p[i], &B = p[(i + 1) % n], &C = p[(i + 2) % n];
if (sgn((B - A).det(C - A)) < 0) {
puts("0");
return 0;
}
}
puts("1");
return 0;
} |
#include<iostream>
using namespace std;
#include<cstdio>
#include<cmath>
struct Point{ double x, y; };
Point set_p(double a, double b)
{
Point p;
p.x = a; p.y = b;
return p;
}
struct Vector{ double vx, vy; };
Vector set_v(const Point &p1, const Point &p2)
{
Vector v;
v.vx = p2.x - p1.x;
v.vy = p2.y - p1.y;
return v;
}
double dot(const Vector &v1, const Vector &v2)
{
return (v1.vx * v2.vx) + (v1.vy * v2.vy);
}
double cross(const Vector &v1, const Vector &v2)
{
return (v1.vx * v2.vy) - (v1.vy * v2.vx);
}
double cross0(const Point &P0, const Point &P1)
{
return (P0.x * P1.y) - (P1.x * P0.y);
}
double onenorm(const Vector &v)
{
return abs(v.vx) + abs(v.vy);
}
double norm(const Vector &v)
{
return sqrt(v.vx * v.vx + v.vy * v.vy);
}
int ccw(const Point &P0, const Point &P1, const Point &P2)
{
Vector V1 = set_v(P0, P1);
Vector V2 = set_v(P0, P2);
double c = cross(V1, V2);
if(c > 0){ return 1; }else if(c < 0){ return -1; }
if(dot(V1, V2) < 0){ return -2; }
if(onenorm(V1) < onenorm(V2)){ return 2; }
return 0;
}
int intersect(const Point &P0, const Point &P1, const Point &P2, const Point &P3)
{
if(ccw(P0, P1, P2) * ccw(P0, P1, P3) <= 0 && ccw(P2, P3, P0) * ccw(P2, P3, P1) <= 0){
return 1;
}else{
return 0;
}
}
double dist_seg(const Point &P0, const Point &P1, const Point &P2) // Segment P0P1 & Point P2.
{
Vector V01 = set_v(P0, P1), V12 = set_v(P1, P2), V02 = set_v(P0, P2);
if(dot(V01, V12) > 0) return norm(V12);
if(dot(V01, V02) < 0) return norm(V02);
double c_sum;
c_sum = cross0(P0, P1) + cross0(P1, P2) + cross0(P2, P0);
if(c_sum < 0) c_sum *= -1.0;
return c_sum / norm(V01);
}
int main()
{
int i, n;
double *X, *Y;
scanf("%d", &n);
X = new double [n + 2];
Y = new double [n + 2];
for(i = 0; i < n; i++){
scanf("%lf %lf", &X[i], &Y[i]);
}
X[n] = X[0], Y[n] = Y[0], X[n + 1] = X[1], Y[n + 1] = Y[1];
Point *P;
P = new Point [n + 2];
for(i = 0; i < n; i++){
P[i] = set_p(X[i], Y[i]);
}
int c;
for(i = 0; i < n; i++){
c = ccw(P[i], P[i + 1], P[i + 2]);
if(c == -1) break;
}
if(i < n){ printf("0\n"); }else{ printf("1\n"); }
delete [] X, delete [] Y, delete [] P;
return 0;
} |
#include <iostream>
#include <complex>
#include <vector>
using namespace std;
typedef complex<double> point;
double cross(const point& a, const point& b) {
return imag(conj(a)*b);
}
double dot(const point& a, const point& b) {
return real(conj(a)*b);
}
int ccw(point a, point b, point c) {
b -= a; c -= a;
if (cross(b, c) > 0) return +1; // counter clockwise
if (cross(b, c) < 0) return -1; // clockwise
if (dot(b, c) < 0) return +2; // c--a--b on line
if (norm(b) < norm(c)) return -2; // a--b--c on line
return 0; // a--c--b on line or a----bc
}
typedef vector<point> polygon;
bool is_convex(const polygon &P) {
size_t n = P.size();
for (int i = 0; i < n; ++i)
if (ccw(P[(i+n-1) % n], P[i % n], P[(i+1) % n]) == -1) return false;
return true;
}
int main() {
int n;
while (cin >> n) {
polygon pol(n);
for (auto& p : pol) {
int x, y;
cin >> x >> y;
p = point(x, y);
}
cout << is_convex(pol) << endl;
}
return 0;
} |
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
struct Point {
double x;
double y;
};
double dot(Point p0, Point p1, Point p2) {
return (p1.x - p0.x) * (p2.x - p0.x) + (p1.y - p0.y) * (p2.y - p0.y);
}
double cross(Point p0, Point p1, Point p2) {
return ( p1.x - p0.x ) * ( p2.y - p0.y ) - ( p2.x - p0.x) * (p1.y - p0.y);
}
bool isConvex(vector<Point> p) {
int n = p.size();
for(int i = 0; i < n; i++) {
if( cross(p[ (i + 1) % n], p[i % n], p[ (i + 2) % n]) > 0) {
return false;
}
}
return true;
}
int main(void) {
int q;
cin >> q;
vector<Point> p;
for(int i = 0; i < q; i++) {
Point pi;
cin >> pi.x >> pi.y;
p.push_back(pi);
}
if(isConvex(p))
cout << "1" << endl;
else
cout << "0" << endl;
}
|
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <stack>
#include <cctype>
#include <cmath>
using namespace std;
/* typedef */
typedef long long ll;
/* constant */
const int INF = 1 << 30;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
void printVec(vector<int> &vec);
/* main */
int main(){
int n; cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
int q; cin >> q;
int l, r, t;
for (int i = 0; i < q; i++) {
cin >> l >> r >> t;
// swap_ranges(begin, end, swap_begin);
// begin ~ end <-> swap_begin ~ swap_begin + (end - begin)
swap_ranges(v.begin() + l, v.begin() + r, v.begin() + t);
}
printVec(v);
return 0;
}
void printVec(vector<int> &vec) {
for (int i = 0; i < vec.size(); i++) {
if (i) cout << ' ' ;
cout << vec[i];
} cout << '\n';
}
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector <long long> a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int q;
cin>>q;
while(q--)
{
int b,e,t;
cin>>b>>e>>t;
swap_ranges(a.begin()+b,a.begin()+e,a.begin()+t);
}
int j;
for(j=0;j<n-1;j++)
{
cout<<a[j]<<" ";
}
cout<<a[j]<<endl;
return 0;
}
|
#include <iostream>
#include <fstream>
#include <algorithm>
//#define LOCAL
using namespace std;
typedef long long ll;
const int mx=1e3+10;
ll num[mx];
int main() {
#ifdef LOCAL
ifstream fin("1.txt");
streambuf*p;
p=cin.rdbuf(fin.rdbuf());
#endif // LOCAL
ll n;
cin>>n;
for(ll i=0;i<n;i++) cin>>num[i];
ll qn;
cin>>qn;
for(ll i=1;i<=qn;i++){
ll b, e, t;
cin>>b>>e>>t;
ll k=e-b;
for(ll d=0;d<k;d++){
swap(num[b+d], num[t+d]);
}
}
for(ll i=0;i<n;i++){
if(0==i) printf("%lld", num[i]);
else printf(" %lld", num[i]);
}
puts("");
#ifdef LOCAL
fin.close();
#endif // LOCAL
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v.at(i);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int b, e, t;
cin >> b >> e >> t;
for (int j = 0; j < e - b; j++) {
swap(v.at(b + j), v.at(t + j));
}
}
for (int i = 0; i < n; i++) {
if (i == n - 1) {
cout << v.at(i) << endl;
} else {
cout << v.at(i) << " ";
}
}
}
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int num;
cin >> num;
vector<int> p;
for (int i = 0;i < num;i++)
{
int n;
cin >> n;
p.push_back(n);
}
int oper;
cin >> oper;
while (oper--)
{
int head, end;
cin >> head >> end;
int k;
cin >> k;
vector<int>::iterator pp;
for (pp = p.begin() + head;pp != p.begin() + end;pp++)
{
swap(*pp, *(pp + k - head));
}
}
vector<int>::iterator temp = p.begin();
while (temp != p.end())
{
cout << *temp;
if (++temp == p.end())cout << endl;
else cout << " ";
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repd(i,a,b) for (int i = (a); i < (b); i++)
typedef long long ll;
int main(void)
{
int n, a, q, b, e, t;
vector<int> A;
cin >> n;
rep(i, n)
{
cin >> a;
A.push_back(a);
}
cin >> q;
rep(i, q)
{
cin >> b >> e >> t;
swap_ranges(A.begin() + b, A.begin() + e, A.begin() + t);
}
rep(i, n)
{
if (i)
{
cout << " ";
}
cout << A[i];
}
cout << endl;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int b, e, t;
cin >> b >>e >> t;
swap_ranges(a.begin() + b, a.begin() + e, a.begin() + t);
}
for (int i = 0; i < n; i++) cout << (i == 0 ? "" : " ") << a[i];
cout << endl;
return 0;
}
|
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <cmath>
#include <vector>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <queue>
typedef long long ll;
const ll LL_MAX (1LL<<60);
#define rep(i,s,e) for(ll i=(s); i<(e); i++)
using namespace std;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >>n;
vector<ll> a(n);
rep(i,0,n) cin >> a[i];
ll q;
cin >> q;
vector<ll> b(q), e(q), t(q);
rep(i,0,n) cin >> b[i] >> e[i] >> t[i];
rep(i,0,q)
{
rep(j,0,e[i]-b[i])
{
swap(a[b[i]+j], a[t[i]+j]);
}
}
rep(i,0,n)
{
if (i != n-1)
cout << a[i] << ' ';
else
cout << a[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int q;
cin >> q;
while (q--) {
int b, e, t;
cin >> b >> e >> t;
swap_ranges(a.begin() + b, a.begin() + e, a.begin() + t);
}
for (int i = 0; i < a.size(); ++i) {
cout << (i ? " " : "") << a[i];
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define r(i,n) for(int i = 0; i < n; i++)
using namespace std;
int main(){
int n;
cin >> n;
int v[n];
r(i,n) cin >>v[i];
int q;
cin >> q;
while(q--){
int b,e,t;
cin >> b >> e >> t;
swap_ranges(v+b,v+e,v+t);
}
r(i,n){
if(i)cout <<" ";
cout << v[i];
}
cout << endl;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
int N;
std::cin >> N;
std::vector<int> v(N);
for (auto& e : v) std::cin >> e;
int Q;
std::cin >> Q;
for (int q = 0; q < Q; ++q) {
int l, r, t;
std::cin >> l >> r >> t;
std::swap_ranges(v.begin() + l, v.begin() + r, v.begin() + t);
}
for (int i = 0; i < v.size(); ++i) {
std::cout << v[i] << " \n"[i == v.size() - 1];
}
}
|
#include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<list>
#include<stack>
#include<map>
#include<set>
#include<string>
#include <sstream>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>
using namespace std;
int main(){
long ii,jj,kk;
vector<int> a;
int n,q,b,e,t;
cin >> n;
a.resize(n);
for(ii=0;ii<n;ii++){
cin >> a[ii];
}
cin >> q;
int pos1,pos2,tmp;
for(ii=0;ii<q;ii++){
cin >> b >> e >> t;
for(jj=0;jj<e - b;jj++){
pos1 = b + jj;
pos2 = t + jj;
tmp = a[pos1];
a[pos1] = a[pos2];
a[pos2] = tmp;
}
}
vector<int>::iterator it = a.begin();
while(it != a.end()){
cout << *it;
it++;
if(it != a.end()){
cout << " ";
}
else{
cout << endl;
}
}
return 0;
}
|
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}
// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';
/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
*/
void rotate2(vector<int> &A, int b, int e, int t){
for(int k=0; k<e-b; k++){
swap(A[b+k], A[t+k]);
}
}
int main()
{
int n; cin >> n;
vector<int> A(n);
rep(i,n){
int Ai; cin >> Ai;
A[i] = Ai;
}
int q; cin >> q;
rep(i,q){
int b, e, t; cin >> b >> e >> t;
rotate2(A, b, e, t);
}
rep(i,n){
cout << A[i];
if(i != n-1) cout << " ";
}
cout << endl;
}
|
#include<iostream>
#include<string>
using namespace std;
#define MAX 1100
#define INF ((int)1e9+9)
int main(){
int A[MAX],temp[MAX], n, q, b, e, m, t;
cin>>n;
for(int i = 0; i < n; i++){
cin>>A[i];
}
cin>>q;
for(int i = 0; i < q; i++){
cin>>b>>e>>t;
for(int j = 0; j < e-b; j++){
swap(A[j+b], A[j+t]);
}
}
for(int i = 0; i < n; i++){
cout<<(i?" ":"")<<A[i];
}
cout<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAX 1005
int A[MAX], n;
int main(void) {
int i;
cin >> n;
for (i = 0; i < n; i++) {
cin >> A[i];
}
int q, b, e, t;
cin >> q;
while (q--) {
cin >> b >> e >> t;
swap_ranges(A + b, A + e, A + t);
}
for (i = 0; i < n; i++) {
if (i) cout << " ";
cout << A[i];
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Fast {
Fast() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} fast;
long long mod = 1000000007;
long long modpow(long long m, long long n) {
if (n == 0) return 1;
if (n % 2 == 0) {
long long t = modpow(m, n / 2);
return (t * t) % mod;
} else {
return (modpow(m, n - 1) * m) % mod;
}
}
void yes() {
cout << "Yes" << endl;
exit(0);
}
void no() {
cout << "No" << endl;
exit(0);
}
#define REP(i, n) for (long long i = 0; i < (n); i++)
struct LCA {
vector<vector<int>> par;
vector<int> d;
int n;
LCA(vector<vector<int>> graph, int root) {
n = graph.size();
par.resize(n, vector<int>(30));
d.resize(n, 0);
function<void(int, int, int)> dfs = [&](int c, int p, int depth) {
d[c] = depth;
par[c][0] = p;
for (auto ch : graph[c]) {
if (ch != p) dfs(ch, c, depth + 1);
}
};
dfs(0, 0, 0);
for (int j = 1; j < 30; j++) {
REP(i, n) { par[i][j] = par[par[i][j - 1]][j - 1]; }
}
}
int getdistance(int u, int v) { return (d[u] + d[v]) - 2 * (d[query(u, v)]); }
int query(int u, int v) {
if (d[u] > d[v]) {
swap(u, v);
}
for (int i = 29; 0 <= i; i--) {
if (d[u] <= d[par[v][i]]) {
v = par[v][i];
}
}
/* LCA */
for (int i = 29; 0 <= i; i--) {
if (par[v][i] != par[u][i]) {
v = par[v][i];
u = par[u][i];
}
}
if (u == v) {
return u;
} else {
return par[v][0];
}
}
};
struct UnionFind {
vector<int> parent, size;
UnionFind(int n) {
parent.resize(n, -1);
size.resize(n, 1);
}
void unite(int x, int y) {
if (same(x, y)) {
return;
}
x = root(x);
y = root(y);
if (size[x] <= size[y]) {
swap(x, y);
}
parent[y] = x;
size[x] += size[y];
}
bool same(int x, int y) { return (root(x) == root(y)); }
int root(int x) {
while (parent[x] != -1) {
x = parent[x];
}
return x;
}
int getsize(int x) { return size[root(x)]; }
};
int n, m;
vector<vector<int>> graph;
signed main() {
int n;
cin >> n;
vector<long long> as(n);
REP(i, n) { cin >> as[i]; }
int q;
cin >> q;
REP(i, q) {
int b, e, t;
cin >> b >> e >> t;
REP(i, e - b) { swap(as[b + i], as[t + i]); }
}
REP(i, n - 1) { cout << as[i] << " "; }
cout << as[n - 1] << endl;
}
|
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n,T,i,j,k;
cin>>n;
vector<long long>v(n+3);
for(i=0;i<n;i++)
{
cin>>v[i];
}
cin>>T;
while(T--)
{
long long b,e,t;
cin>>b>>e>>t;
swap_ranges(v.begin()+b,v.begin()+e,v.begin()+t);
}
// cout<<"*"<<endl;
long long ans=0;
for(i=0;i<n;i++,ans++)
{
if(ans!=0)cout<<" ";
cout<<v[i];
}
cout<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int a[1010];
int main()
{
int n, m;
scanf("%d",&n);
int i;
for(i=0; i<n; i++) scanf("%d",&a[i]);
scanf("%d",&m);
while(m--)
{
int l, mid, r;
scanf("%d%d%d",&l,&mid,&r);
swap_ranges(l+a, mid+a, r+a);
}
for(i=0; i<n; i++)
{
if(i == n-1) printf("%d\n",a[i]);
else printf("%d ",a[i]);
}
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int n1,commandNum,b,e,t;
cin >> n1;
vector<int>a1(n1),a2(n1);
for(int i = 0;i < n1;++i){
cin >> a1[i];
a2[i] = a1[i];
}
cin >> commandNum;
for(int i = 0;i < commandNum;++i){
cin >> b >> e >> t;
// 左側文字列を右側に代入する
for(int k = b;k < b + (e - b);++k){
int moveIndex = (k - b) + t;
a2[moveIndex] = a1[k];
}
// 右側文字列を左側に代入する
for(int k = t;k < t + (e - b);++k){
int moveIndex = b + (k - t);
a2[moveIndex] = a1[k];
}
copy(a2.begin(),a2.end(),a1.begin());
}
for(int i = 0;i < n1;++i){
if(i) cout << " ";
cout << a2[i];
}
cout << endl;
}
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n, q, b, e, t;
cin >> n;
vector<int> A(n);
vector<int>::iterator a = A.begin();
for(int i = 0;i < n;i++) cin >> A[i];
cin >> q;
for(int i = 0;i < q;i++){
cin >> b >> e >> t;
swap_ranges(a + b, a + e, a + t);
}
for(int i = 0;i < n;i++){
cout << (i?" ":"");
cout << A[i];
}
cout << endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n, q, b, e, t;
cin >> n;
std::vector<int> A;
int a[n];
for(int i=0; i<n; i++){
cin >> a[i];
A.push_back(a[i]);
}
cin >> q;
for(int j=0; j<q; j++){
cin >> b >> e >> t;
std::swap_ranges(A.begin()+b, A.begin()+e, A.begin()+t);
}
for(int k=0; k<n; k++){
cout << A[k];
if(k!=n-1) cout << " ";
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAX 1005
int A[MAX], n;
void Myswap(int b, int e, int t) {
int i;
for (i = 0; i < e - b; i++) {
swap(A[i + b], A[i + t]);
}
}
int main(void) {
int i;
cin >> n;
for (i = 0; i < n; i++) {
cin >> A[i];
}
int q, b, e, t;
cin >> q;
while (q--) {
cin >> b >> e >> t;
Myswap(b, e, t);
}
for (i = 0; i < n; i++) {
if (i) cout << " ";
cout << A[i];
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;cin>>n;
vector<int> a(n);
for(int i=0;i<n;++i)cin>>a[i];
int q;cin>>q;
while(q--){
int b,e,t;cin>>b>>e>>t;
swap_ranges(next(a.begin(),b), next(a.begin(),e), next(a.begin(),t));
}
bool isFirst=true;
for(auto &&ai: a){
if(!isFirst) cout<<" ";
cout<<ai;
isFirst &= false;
}
cout<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i) cin >> a[i];
int q;
cin >> q;
while(q--) {
int b, e, t;
cin >> b >> e >> t;
swap_ranges(a.begin()+b, a.begin()+e, a.begin()+t);
}
for(int i = 0; i < n; ++i) cout << a[i] << " \n"[i==n-1];
return 0;
}
|
#include <cstdio>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_in(int c){int n=0;bool m=false;if(c=='-')m=true,c=gcu();
do{n=10*n+(c-'0'),c=gcu();}while(c>='0');return m?-n:n;} //&&c<='9'
_in() {return in(gcu());}
_sc(int &n){int c=gcu();return c==EOF?false:(n=in(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
//_sc(string &s){int c;s="";
// for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
template <typename H,typename... T> _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
template <typename T>
_vo(T n){static char buf[20];char *p=buf;
if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
while (p!=buf)pcu(*--p);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
//_vo(string &s){for (char c: s) pcu(c);}
template <typename H,typename... T> _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename T> _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
_vl(){out('\n');}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}
int main() {
vector<int> v(in());
for (int &x: v)
x = in();
auto s = v.begin();
for (int q = in(); q; q--) {
int b = in(), e = in();
swap_ranges(s + b, s + e, s + in());
}
out(v);
}
|
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n, q;
cin >> n;
vector<int> a(n);
for (int& i : a) cin >> i;
cin >> q;
for (int i = 0; i < q; ++i) {
int b, e, t;
cin >> b >> e >> t;
for (int i = 0; i < e - b; ++i) swap(a[b + i], a[t + i]);
}
cout << a[0];
for (int i = 1; i < n; ++i) cout << ' ' << a[i];
cout << endl;
}
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
using ll = long long;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;
const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;
template<class T, class S>
bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S>
bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
int main(){
int n; cin >> n;
VI a(n);
REP(i, n) cin >> a[i];
int q; cin >> q;
REP(i, q){
int b, e, t; cin >> b >> e >> t;
swap_ranges(a.begin()+b, a.begin()+e, a.begin()+t);
}
REP(i, n){
if(i) cout << " ";
cout << a[i];
}
cout << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
int main(int argc, char *argv[]) {
int n, q;
std::cin >> n;
long long a[1001];
for (int i = 0; i < n; ++i)
std::cin >> a[i];
std::cin >> q;
for (int i = 0; i < q; ++i) {
int b, e, t;
std::cin >> b >> e >> t;
std::swap_ranges(a + b, a + e, a + t);
}
for (int i = 0; i < n; ++i) {
if(i>0)
std::cout << " ";
std::cout << a[i];
}
std::cout << std::endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define reps(i, n, m) for (int i = (int) (n); i < (int) (m); i++)
#define rep(i, n) reps(i, 0, (n))
int main() {
int n; cin >> n;
vector<int> v(n);
rep(i, n) cin >> v[i];
int q; cin >> q;
while (q--) {
int l, m, r;
cin >> l >> m >> r;
auto b = v.begin();
swap_ranges(b + l, b + m, b + r);
}
rep(i, n) {
if (i) cout << " ";
cout << v[i];
}
cout << endl;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int N;
cin >> N;
vector<int> vec;
for(int i = 0; i < N; ++i)
{
int Num; cin >> Num;
vec.push_back(Num);
}
cin >> N;
for(int i = 0; i < N; ++i)
{
int b, e, t;
cin >> b >> e >> t;
for(int k = 0; k < e-b; ++k)
{
int tmp = vec[k+b];
vec[k+b] = vec[k+t];
vec[k+t] = tmp;
}
}
for(int i = 0; i < vec.size(); ++i)
cout << (i == 0 ? "" : " ") << vec[i];
cout << endl;
return 0;
}
|
# include <iostream>
# include <algorithm>
# include <iterator>
using namespace std;
int main() {
int n; scanf("%d", &n);
int a[n]; for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
int num_query; scanf("%d", &num_query);
int begin, end, t;
for (int i = 0; i < num_query; ++i) {
scanf("%d%d%d", &begin, &end, &t);
swap_ranges(a + begin, a + end, a + t);
}
printf("%d", a[0]);
for (int i = 1; i < n; ++i) printf(" %d", a[i]);
printf("\n");
return 0;
}
|
#include <iostream>
using namespace std;
void swap(int b, int e, int t, long* A);
int main(void){
int n;
cin>>n;
long A[n];
for(int i=0;i<n;i++){
cin>>A[i];
}
int q;
cin>>q;
int b,e,t;
for(int i=0;i<q;i++){
cin>>b>>e>>t;
swap(b,e,t,A);
}
for(int i=0;i<n-1;i++){
cout<<A[i]<<" ";
}
cout<<A[n-1]<<endl;
return 0;
}
void swap(int b,int e,int t,long* A){
long tmp;
for(int k=0;k<e-b;k++){
tmp=A[t+k];
A[t+k]=A[b+k];
A[b+k]=tmp;
}
}
|
#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;
const int MOD_TYPE = 1;
const ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const double PI = acos(-1.0);
#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(50) << setiosflags(ios::fixed);
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
int q;
cin >> q;
rep(qi, q)
{
int b, e, t;
cin >> b >> e >> t;
swap_ranges(a.begin() + b, a.begin() + e, a.begin() + t);
}
rep(i, a.size()) cout << a[i] << (i == a.size() - 1 ? "\n" : " ");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++){
cin >> a[i];
}
int q;
cin >> q;
for (int i = 0; i < q; i++){
int b, e, t;
cin >> b >> e >> t;
for (int j = b; j < e; j++){
swap(a[j], a[t + j - b]);
}
}
for (int i = 0; i < n; i++){
cout << a[i];
if (i < n - 1){
cout << ' ';
}
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int v[n];
int i;
for (i = 0; i < n; i++)
{
cin >> v[i];
}
int q;
cin >> q;
while (q--)
{
int a, b, c;
cin >> a >> b >> c;
swap_ranges(v + a, v + b, v + c);
}
for (i = 0; i < n; i++)
{
if (i)
cout << ' ';
cout << v[i];
}
cout << endl;
}
|
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int a[1010];
int main()
{
int n;
cin >> n;
vector<long long> v;
for(int i=0; i<n; ++i) {
int a; cin >> a;
v.push_back(a);
}
int q; cin >> q;
for(int i=0; i<q; ++i){
int b, e, t; cin >> b >> e >> t;
for(int i=0; i<e-b; ++i){
swap(v[b+i], v[t+i]);
}
}
for(int i=0; i<n; i++){
if(i) cout << ' ';
cout << v[i];
}
cout << endl;
}
// GCC reference:
// https://gcc.gnu.org/
// C++ language references:
// https://msdn.microsoft.com/library/3bstk3k5.aspx
// http://www.cplusplus.com/
// https://isocpp.org/
// http://www.open-std.org/jtc1/sc22/wg21/
// Boost libraries references:
// http://www.boost.org/doc/
|
#include <vector>
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::swap;
using std::vector;
using std::for_each;
int main() {
int n;
cin >> n;
vector<int> v(n);
for_each(v.begin(),v.end(),[](int &_e){cin >> _e;});
int q;
cin >> q;
while (q--) {
int b;
int e;
int t;
cin >> b >> e >> t;
for (int k = 0;k != e-b;++k) {
swap(v[b+k],v[t+k]);
}
}
int i = 1;
for_each(v.begin(),v.end(),[&i,n](int _e){cout << _e << (i++ == n ? "\n" : " ");});
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
typedef long long ll;
const ll inf = 1e9;
int main(){
int n; cin >> n;
vector<ll> a(n);
rep(i,n)cin >> a[i];
int q; cin >> q;
rep(i,q){
int b, m, e;
cin >> b >> m >> e;
swap_ranges(a.begin()+b, a.begin()+m, a.begin()+e);
}
rep(i,n)cout << a[i] << ((i < n-1)?" ":"\n");
return 0;
}
|
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, q;
cin >> n;
int arr[10001];
for (int i = 0; i < n; i++)
cin >> arr[i];
cin >> q;
while (q--) {
int b, e, t;
cin >> b >> e >> t;
for (int j = 0; j < (e - b); j++)
swap(arr[b + j], arr[t + j]);
}
for (int i = 0; i < n; i++) {
if (i == n - 1)
cout << arr[i] << endl;
else cout << arr[i] << " ";
}
}
|
#include <iostream>
using namespace std;
int a[1001];
int main()
{
bool f;
int n,q;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>q;
for(int i=0;i<q;i++)
{
int b,e,t,j;
cin>>b;cin>>e;cin>>t;
for(j=0;j<n;j++)
{
if(j>=b&&j<e)
swap(a[j+t-b],a[j]);
}
}
f=false;
for(int j=0;j<n;j++)
{
if(f==true)
cout<<" "<<a[j];
else
{
f=true;
cout<<a[j];
}
}
cout<<"\n";
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<ld> vld;
typedef vector<string> vstr;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define TS to_string
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
ll INF = 1e9;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;} //最小公倍数
void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}
//-----------------------------------------
//-----------------------------------------
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
ll n;
cin >> n;
vll a(n);
REP(i,n) cin >> a[i];
ll q;
cin >> q;
REP(i,q){
ll b, e, t;
cin >> b >> e >> t;
REP(j,e-b){
swap(a[b+j],a[t+j]);
}
}
REP(i,n){
if(i) cout << " ";
cout << a[i];
}
cout << endl;
return 0;
}
|
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int q;
cin >> q;
while (q--)
{
int b, e, t;
cin >> b >> e >> t;
for (int i = 0; i < e - b; i++) {
swap(a[b + i], a[t + i]);
}
}
for (int i = 0; i < a.size(); i++) {
if (i > 0) {
cout << " ";
}
cout << a[i];
}
cout << endl;
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <random>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define irep(it, stl) for(auto it = stl.begin(); it != stl.end(); it++)
#define drep(i,n) for(int i = (n) - 1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p,q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
#define endl '\n';
#define fi first
#define se second
#define NONVOID [[nodiscard]]
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;} return 0;}
template <class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;} return 0;}
inline string getline(){string s; getline(cin,s); return s;}
inline void yn(const bool b){b?fin("yes"):fin("no");}
inline void Yn(const bool b){b?fin("Yes"):fin("No");}
inline void YN(const bool b){b?fin("YES"):fin("NO");}
struct io{io(){ios::sync_with_stdio(false);cin.tie(0);}};
const int INF = INT_MAX;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
int main() {
int n,m;
cin>>n;
vi v(n);
rep(i,n)cin>>v[i];
int q;
cin>>q;
rep(i,q){
int b,e,t;
cin>>b>>e>>t;
for(int i=0;i<e-b;i++){
swap(v[b+i],v[t+i]);
}
}
rep(j,n){
if(!j)cout<<v[0];
else cout<<" "<<v[j];
}cout<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n;
cin >> n;
vector<int>res(n);
for (int i = 0; i < n; i++)cin >> res[i];
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
int b, e, t;
cin >> b >> e >> t;
swap_ranges(res.begin() + b, res.begin() + e, res.begin() + t);
}
for (int i = 0; i < n; i++)
if (i == 0)cout << res[i];
else cout << " " << res[i];
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define name (i++?" ":"")
int main(){
int n;
cin>>n;
vector <int> a(n);
REP(i,n) cin>>a[i];
int q;
cin>>q;
\
REP(i,q){
int b,e,t;
cin>>b>>e>>t;
swap_ranges(a.begin()+b,a.begin()+e,a.begin()+t);
}
int i=0;
for(int e:a) cout<<name<<e;
cout<<endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define long int64_t
template<class T>
inline istream& operator >>( istream& s, vector<T>& v ) {
for( size_t i = 0; i < v.size(); ++i ) { s >> v[i]; }
return s;
}
template<class T>
inline ostream& operator <<( ostream& s, vector<T>& v ) {
for( size_t i = 0; i < v.size(); ++i ) {
s << v[i];
if( i < v.size()-1 ) { s << ' '; }
}
return s;
}
int main() {
ios_base::sync_with_stdio( false );
int n;
cin >> n;
vector<int> a( n );
cin >> a;
int q;
cin >> q;
while( q-- > 0 ) {
int b, e, t;
cin >> b >> e >> t;
auto it = a.begin();
swap_ranges( it+b, it+e, it+t );
}
cout << a << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define il inline
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for(int i = x; i <= y; i ++)
#define rrep(i, x, y) for(int i = x; i >= y; i --)
#define rep0(i, n) for(int i = 0; i < (n); i ++)
#define per0(i, n) for(int i = (n) - 1; i >= 0; i --)
#define ept 1e-9
#define INF 0x3f3f3f3f
#define sz(x) (x).size()
#define All(x) (x).begin(), (x).end()
#define lowbit(x) ((x) & -(x))
using namespace std;
inline int read()
{
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
inline ll read1()
{
ll x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
const ll mod = 1e9 + 7;
const int N = 4e6 + 10;
const db pi = acos(-1);
//int a[N];
int main()
{
int n = read();
vi a(n);
rep0(i, n) a[i] = read();
int q = read();
while(q --)
{
int x = read();
int y = read();
int z = read();
swap_ranges(a.begin() + x, a.begin() + y, a.begin() + z);
}
rep0(i, n)
{
if(i) cout << " ";
cout << a[i];
}
puts("");
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n;
std::cin >> n;
std::vector<int> A(n);
for (int i = 0; i < n; i++) {
std::cin >> A[i];
}
int q;
std::cin >> q;
for (int i = 0; i < q; i++) {
int b, e, t;
std::cin >> b >> e >> t;
std::swap_ranges(A.begin() + b, A.begin() + e, A.begin() + t);
}
for (int i = 0; i < A.size(); i++) {
std::cout << A[i];
if (i < A.size() - 1) {
std::cout << " ";
}
}
std::cout << std::endl;
}
|
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int A[n];
for(int i=0; i<n; i++)
cin>>A[i];
int q;
cin>>q;
while(q--){
int b,e,t;
cin>>b>>e>>t;
for(int i=0; i<e-b; i++){
swap(A[b+i], A[t+i]);
}
}
for(int i=0; i<n-1; i++)
cout<<A[i]<<" ";
cout<<A[n-1]<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int q;
cin >> q;
int b, e, t;
while(q--) {
cin >> b >> e >> t;
swap_ranges(a + b, a + e, a + t);
}
for (int i = 0; i < n; i++) cout << a[i] << " \n"[i == n - 1];
return 0;
}
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
}
int q;
cin >> q;
for(int i = 0; i < q; ++i){
int b, e, t;
cin >> b >> e >> t;
swap_ranges(a.begin() + b, a.begin() + e, a.begin() + t);
}
for(int i = 0; i < a.size(); ++i){
if(i){
cout << " ";
}
cout << a[i];
}
cout << endl;
return 0;
}
|
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s, f) cout << fixed << setprecision(f) << s << endl;
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
int b, e, t;
cin >> b >> e >> t;
swap_ranges(a.begin() + b, a.begin() + e, a.begin() + t);
}
bool sp = false;
for (int i : a)
{
print((sp ? " " : "") << i);
sp = true;
}
println("");
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void){
int n,a,q,b,e,t;
std::deque<int> deq;
cin >> n;
for (int i=0;i<n;i++) {
cin >> a;
deq.emplace_back(a);
}
cin >> q;
for (int j=0;j<q;j++) {
cin >> b >> e >> t;
for (int k=0;k<(e-b);k++) {
swap(deq[b+k],deq[t+k]);
}
}
for (int l=0;l<deq.size();l++) {
if ( l==deq.size()-1 ) {
cout << deq[l] << endl;
} else {
cout << deq[l] << " ";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = int(A); I < int(B); ++I)
int main(){
ll n;
cin >> n;
vector<ll> a(n);
FOR(i,0,n) cin>>a[i];
ll q;
cin >> q;
FOR(i,0,q){
ll b,e,t;
cin >> b >> e >> t;
FOR(k,0,e-b){
swap(a[b+k],a[t+k]);
}
}
cout << a[0];
FOR(i,1,n){
cout << " " << a[i];
}
cout << endl;
}
|
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> v;
for(int i = 0; i < n; i++)
{
int x;
cin >> x;
v.push_back(x);
}
int q;
cin >> q;
for(int i = 0; i < q; i++)
{
int b, e, t;
cin >> b >> e >> t;
for(int j = 0; j < e - b; j++)
{
swap(v[b + j], v[t + j]);
}
}
for(int i = 0; i < n; i++)
{
if(i > 0) cout << " " << v[i];
else cout << v[i];
}
cout << endl;
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define long long long
#define LF '\n'
using namespace std;
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF = 0x3f3f3f3f;
signed main()
{
cin.tie(nullptr), ios::sync_with_stdio(false);
int n ,q;
int a[1005];
cin >> n;
for (int i= 0; i < n; ++i) cin >> a[i];
cin >> q;
while(q--) {
int s, e, t;
cin >> s >> e >> t;
swap_ranges(a+s, a+e, a+t);
}
for (int i= 0; i < n; ++i) {
cout << a[i] << (i+1 < n ? ' ' : LF);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a(1111);
void swap(int x,int y){
int tmp = a[x];
a[x] = a[y];
a[y] = tmp;
}
int main() {
int n; cin >> n;
for(int i=0; i<n; i++) cin >> a[i];
int q; cin >> q;
for(int i=0; i<q; i++){
int b,e,t;
cin >> b >> e >> t;
for(int k=0; k<(e-b); k++){
//cout << j <<" "<< j+t << endl;
swap(b+k,t+k);
}
}
for(int i=0; i<n; i++){
if(i!=0) cout <<" ";
cout << a[i];
}
cout << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
const ll mod = 1e9 + 7;
const ll inf = 1e18;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
// input
ll N;
cin >> N;
vector<ll> A(N);
FOR(i, 0, N){
cin >> A.at(i);
}
ll Q;
cin >> Q;
while(Q--){
ll b, e, t;
cin >> b >> e >> t;
auto it = A.begin();
swap_ranges(it+b, it+e, it+t);
}
FOR(i, 0, N){
if(i){
cout << ' ';
}
cout << A[i];
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n,m,b,e,q;
cin>>n;
long long a[n];
for(long long i=0;i<n;i++){
cin>>a[i];
}
cin>>q;
for(long long i=0;i<q;i++){
cin>>b>>m>>e;
swap_ranges(a+b,a+m,a+e);
}
for(long long i=0;i<n-1;i++){
cout<<a[i]<<" ";
}
cout<<a[n-1]<<endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.