text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ALL(c) c.begin(), c.end()
#define SZ(c) ((int)c.size())
#define REP(i, n) for (int i = 0, loop_num_##i = n; i < loop_num_##i; i++)
template <class T, class U>
bool chmax(T &a, const U &b) {
if (a < b) { a = b; return true; }
return false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
if (a > b) { a = b; return true; }
return false;
}
using ld = long double;
ld dp[21][200000];
signed main() {
cout << setprecision(15) << fixed << endl;
REP(i, 21) REP(j, 200000) dp[i][j] = 1e9;
int N;
cin >> N;
vector<int> a(N);
REP(i, N) cin >> a[i];
dp[0][1] = 0;
REP(i, N) {
REP(j, 200000) {
if (j == 0) continue;
for (int k = 1; j * k < 200000; k++) {
ld tmp = max(dp[i][j], (ld)abs(a[i] - j * k) / a[i]);
dp[i + 1][j * k] = min(dp[i + 1][j * k], tmp);
}
}
}
ld ans = 1e9;
REP(i, 200000) ans = min(ans, dp[N][i]);
cout << ans << endl;
}
|
#include<iostream>
#include<cstdio>
using namespace std;
double ans;
int ori[22],a[22],n;
void dfs(int i,double maxr)
{
if (i==n){ans=maxr;return;}
int add,sub,now=a[i];
if (a[i]>a[i-1])
{
add=a[i-1]-now%a[i-1];
sub=a[i]%a[i-1];
while (true)
{
a[i]=now-sub;
if (double(sub)/(double)ori[i]>ans||a[i]<a[i-1])break;
dfs(i+1,max(maxr,double(sub)/(double)ori[i]));
sub+=a[i-1];
}
}
else add=a[i-1]-a[i];
while (true)
{
if ((double)add/(double)ori[i]>ans)break;
a[i]=now+add;
dfs(i+1,max(maxr,(double)add/(double)ori[i]));
add+=a[i-1];
}
a[i]=now;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=0;i<n;i++)
{
scanf("%d",&ori[i]);
a[i]=ori[i];
}
ans=1e18;
dfs(1,0);
int add=1;
double radio;
while (true)
{
radio=(double)add/(double)ori[0];
if (radio>ans)break;
a[0]=ori[0]+add;
dfs(1,radio);
a[0]=ori[0]-add;
dfs(1,radio);
add++;
}
printf("%.10lf\n",ans);
}
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#pragma comment (linker, "/STACK:526000000")
#include "bits/stdc++.h"
using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()
void init() {
iostream::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
unsigned long xor128() {
static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
unsigned long t = (x ^ (x << 11));
x = y; y = z; z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
//li chao tree by ei1333. It's public available.
// https://ei1333.github.io/luzhiled/snippets/structure/li-chao-tree.html
/*
template< typename T >
struct LiChaoTree {
struct Line {
T a, b;
Line(T a, T b) : a(a), b(b) {}
inline T get(T x) const { return a * x + b; }
inline bool over(const Line& b, const T& x) const {
return get(x) < b.get(x);
}
};
vector< T > xs;
vector< Line > seg;
int sz;
LiChaoTree(const vector< T >& x, T INF) : xs(x) {
sz = 1;
while (sz < xs.size()) sz <<= 1;
while (xs.size() < sz) xs.push_back(xs.back() + 1);
seg.assign(2 * sz - 1, Line(0, INF));
}
void update(Line& x, int k, int l, int r) {
int mid = (l + r) >> 1;
auto latte = x.over(seg[k], xs[l]), malta = x.over(seg[k], xs[mid]);
if (malta) swap(seg[k], x);
if (l + 1 >= r) return;
else if (latte != malta) update(x, 2 * k + 1, l, mid);
else update(x, 2 * k + 2, mid, r);
}
void update(T a, T b) { // ax+b
Line l(a, b);
update(l, 0, 0, sz);
}
T query(int k) { // xs[k]
const T x = xs[k];
k += sz - 1;
T ret = seg[k].get(x);
while (k > 0) {
k = (k - 1) >> 1;
ret = min(ret, seg[k].get(x));
}
return ret;
}
};
#define int long long
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> landmarks;
vector<vector<int>> costs;
REP(i, n) {
vector<int> tmp;
REP(q, m) {
int b;
cin >> b;
b = xor128() % 10000000;
tmp.push_back(b);
}
landmarks.push_back(tmp);
}
REP(i, n) {
vector<int> tmp;
{
REP(q, m) {
int b;
cin >> b;
b = xor128() % 100000000;
tmp.push_back(b);
}
}
costs.push_back(tmp);
}
vector<LiChaoTree<int>> yoko; // size should be m
REP(i, n) {
vector<int> hoge(m);
REP(q, m) {
hoge[q] = q;
}
LiChaoTree<int> now(hoge, 1e18);//size should be n
yoko.push_back(now);
}
vector<vector<int>> dp = landmarks;
for (int q = 0; q < m; ++q) {
vector<int> hoge(n);
REP(i, n) {
hoge[i] = i;
}
LiChaoTree<int> now(hoge, 1e18);//size should be n
for (int i = 0; i < n; ++i) {
int now_min = costs[0][0] * (i + q);
if (q < 0) {
int geko = yoko[i].query(q);
now_min = min(now_min, geko);
}
if (i != 0) {
int geko = now.query(i);
now_min = min(now_min, geko);
}
dp[i][q] = now_min;
yoko[i].update(costs[i][q], landmarks[i][q] + dp[i][q] - q * costs[i][q]);
now.update(costs[i][q], landmarks[i][q] + dp[i][q] - i * costs[i][q]);
}
}
cout << dp[n - 1][m - 1] << endl;
}
*/
#define int ll
long double dp[21][200001];
void solve(){
int n;
cin >> n;
vector<int> inputs;
REP(i, n) {
int a;
cin >> a;
inputs.push_back(a);
}
REP(i, 21) {
REP(q, 200001) {
dp[i][q] = 1000;
}
}
dp[0][1] = 0;
for (int i = 0; i < n; ++i) {
for (int q = 1; q <= 100000; ++q) {
for (int j = 1; q * j <= 200000; ++j) {
dp[i + 1][q * j] = min(dp[i + 1][q * j], max(dp[i][q],(long double)(llabs(q * j - inputs[i])) / (long double)(inputs[i])));
}
}
}
long double ans = 1000.0;
REP(i, 200001) {
ans = min(ans, dp[n][i]);
}
cout << ans << endl;
}
#undef int
int main() {
init();
solve();
}
|
#include<cmath>
#include<cstdio>
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const long double EPS=1e-8;
const long double PI=acos(-1);
struct point{
long double x,y;
point():x(0),y(0){}
point(long double x,long double y):x(x),y(y){}
point operator-(const point &a)const{ return point(x-a.x,y-a.y); }
};
long double cross(const point &a,const point &b){ return a.x*b.y-a.y*b.x; }
long double arg(const point &a){
long double t=atan2(a.y,a.x);
return t<0?t+2*PI:t;
}
struct line{
point a,b;
line(){}
line(const point &a,const point &b):a(a),b(b){}
};
enum{CCW=1,CW=-1,ON=0};
int ccw(const point &a,const point &b,const point &c){
long double rdir=cross(b-a,c-a);
if(rdir>0) return CCW;
if(rdir<0) return CW;
return ON;
}
long double dist(const point &a,const point &b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
const long double g=9.8;
int main(){
int n;
long double v; scanf("%d%Lf",&n,&v);
point p[10000];
rep(i,n) scanf("%Lf%Lf",&p[i].x,&p[i].y);
long double a[9999],b[9999]; // y = a*x + b
rep(i,n-1){
a[i]=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x);
b[i]=p[i].y-a[i]*p[i].x;
}
long double ans=dist(p[0],p[1]);
for(int i=1;i<n-1;){
if(ccw(p[i-1],p[i],p[i+1])!=CW){ // テ」ツつクテ」ツδ」テ」ツδウテ」ツδ療」ツ?療」ツ?ェテ」ツ??
ans+=dist(p[i],p[i+1]);
i++;
}
else{ // テ」ツつクテ」ツδ」テ」ツδウテ」ツδ療」ツ?凖」ツつ?
long double vx=v*cos(arg(p[i]-p[i-1]));
long double vy=v*sin(arg(p[i]-p[i-1]));
long double x0=p[i].x,y0=p[i].y;
for(;i+1<n;i++){
// テヲツ鳴愿ゥツ敖「テ」ツ?ィテ」ツ?カテ」ツ?、テ」ツ?凝」ツつ凝ヲツ卍づ・ツ按サ t テ」ツつ津ヲツアツづ」ツつ?」ツつ?
// A*t^2 + B*t + C = 0
long double A=-g/2;
long double B=vy-a[i]*vx;
long double C=y0-a[i]*x0-b[i];
if(B*B-4*A*C<0) continue;
long double t0=(-B+sqrt(B*B-4*A*C))/(2*A);
long double t1=(-B-sqrt(B*B-4*A*C))/(2*A);
long double xx; // テヲツ鳴愿ゥツ敖「テ」ツ?ィテ」ツ?カテ」ツ?、テ」ツ?凝」ツつ凝、ツスツ催ァツスツョ
xx=x0+vx*t0;
if(p[i].x<xx && xx<p[i+1].x){ point q(xx,-g/2*t0*t0+vy*t0+y0); ans+=dist(q,p[i+1]); break; }
xx=x0+vx*t1;
if(p[i].x<xx && xx<p[i+1].x){ point q(xx,-g/2*t1*t1+vy*t1+y0); ans+=dist(q,p[i+1]); break; }
}
i++;
}
}
printf("%.15Lf\n",ans);
return 0;
} |
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
using namespace std;
const long double EPS = 1e-8;
const long double INF = 1e12;
#define EQ(n,m) (abs((n)-(m)) < EPS)
typedef complex<long double> P;
typedef vector<P> VP;
#define X real()
#define Y imag()
vector<long double> solve_quadeq(long double a, long double b, long double c){
vector<long double> ret;
if(abs(a) < EPS){
if(abs(b) > EPS) ret.push_back(-c/b);
return ret;
}
long double d = b*b -4*a*c;
if(abs(d) < EPS) d = 0;
if(d < 0) return ret;
if(d < 0) d = 0;
ret.push_back((-b +sqrt(d))/(2*a));
ret.push_back((-b -sqrt(d))/(2*a));
return ret;
}
const long double g = 9.8;
int main(){
int n;
long double v;
cin >> n >> v;
VP p(n);
for(int i=0; i<n; i++){
long double x,y;
cin >> x >> y;
p[i] = P(x, y);
}
long double ans = abs(p[1] -p[0]);
int pos = 1;
while(pos < n-1){
P launch = p[pos];
P prevdir = p[pos] -p[pos-1];
P nextdir = p[pos+1] -p[pos];
//ジャンプしない
if(arg(prevdir) < arg(nextdir) +EPS){
ans += abs(nextdir);
pos++;
continue;
}
//ジャンプする
//放物線の関数
long double theta = arg(prevdir);
long double vx = v *cos(theta);
long double vy = v *sin(theta);
for(; pos<n-1; pos++){
//直線の式(y = rx+s)
long double r,s;
r = (p[pos+1].Y -p[pos].Y) /(p[pos+1].X -p[pos].X);
s = -p[pos].X *r +p[pos].Y;
//直線と放物線が交わる時間tの方程式(at^2 +bt +c = 0)
long double a,b,c;
a = g/2;
b = r*vx -vy;
c = r*launch.X -launch.Y +s;
//交点をとる
vector<long double> solt = solve_quadeq(a, b, c);
bool solved = false;
for(long double t: solt){
if(t < EPS) continue;
long double x = vx*t +launch.X;
if(p[pos].X +EPS < x && x +EPS < p[pos+1].X){
P cp(x, -g*t*t/2 +vy*t +launch.Y);
ans += abs(cp -p[pos+1]);
pos++;
solved = true;
break;
}
}
if(solved) break;
}
}
cout << fixed << setprecision(10);
cout << ans << endl;
return 0;
}
|
#include <cstdio>
#include <vector>
#include <cmath>
#include <complex>
#include <iostream>
using namespace std;
#define EPS 1e-6
typedef long double ldouble;
typedef complex<ldouble> P;
ldouble cross(const P &a, const P &b){
return real(a) * imag(b) - imag(a) * real(b);
}
int n;
P z[10001];
ldouble Vabs;
const ldouble g = 9.8;
P jump(int &i){
P dir = z[i] - z[i-1];
P v = Vabs / abs(dir) * dir;
const P &base = z[i];
ldouble a = g / (real(v) * real(v) * 2.0);
ldouble b = imag(v) / real(v);
for(int j = i; j < n - 1; ++j){
P z1 = z[j] - base;
P z2 = z[j+1] - base;
ldouble c = imag(z2 - z1) / real(z2 - z1);
ldouble d = imag(z1) - c * real(z1);
ldouble e = sqrt((b-c) * (b-c) - 4.0 * a * d);
for(ldouble sgn = -1.0; sgn < 2.0; sgn += 2.0){
ldouble x = (b - c + sgn * e) / (2.0 * a);
if(x > real(z1) && x < real(z2)){
ldouble y = -a * x * x + b * x;
i = j;
return P(x, y) + base;
}
}
}
i = n;
return z[n-1];
}
int main(){
ldouble x, y;
cin >> n >> Vabs;
for(int i = 0; i < n; ++i){
cin >> x >> y;
z[i] = P(x, y);
}
ldouble ans = 0.0;
P prev = z[0];
for(int i = 1; i < n; ++i){
P dif = z[i] - prev;
ans += abs(dif);
if(i == n - 1) break;
if(cross(dif, z[i+1] - z[i]) < -EPS){
prev = jump(i);
}
else{
prev = z[i];
}
}
printf("%.20f\n", (double)ans);
} |
#include<cmath>
#include<cstdio>
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const long double EPS=1e-8;
const long double PI=acos(-1);
struct point{
long double x,y;
point():x(0),y(0){}
point(long double x,long double y):x(x),y(y){}
point operator-(const point &a)const{ return point(x-a.x,y-a.y); }
};
long double cross(const point &a,const point &b){ return a.x*b.y-a.y*b.x; }
long double arg(const point &a){
long double t=atan2(a.y,a.x);
return t<0?t+2*PI:t;
}
struct line{
point a,b;
line(){}
line(const point &a,const point &b):a(a),b(b){}
};
enum{CCW=1,CW=-1,ON=0};
int ccw(const point &a,const point &b,const point &c){
long double rdir=cross(b-a,c-a);
if(rdir>0) return CCW;
if(rdir<0) return CW;
return ON;
}
long double dist(const point &a,const point &b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
const long double g=9.8;
int main(){
int n;
long double v; scanf("%d%Lf",&n,&v);
point p[10000];
rep(i,n) scanf("%Lf%Lf",&p[i].x,&p[i].y);
long double a[9999],b[9999]; // y = a*x + b
rep(i,n-1){
a[i]=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x);
b[i]=p[i].y-a[i]*p[i].x;
}
long double ans=dist(p[0],p[1]);
for(int i=1;i<n-1;){
if(ccw(p[i-1],p[i],p[i+1])!=CW){ // ジャンプしない
ans+=dist(p[i],p[i+1]);
i++;
}
else{ // ジャンプする
long double vx=v*cos(arg(p[i]-p[i-1]));
long double vy=v*sin(arg(p[i]-p[i-1]));
long double x0=p[i].x,y0=p[i].y;
for(;i+1<n;i++){
// 斜面とぶつかる時刻 t を求める
// A*t^2 + B*t + C = 0
long double A=-g/2;
long double B=vy-a[i]*vx;
long double C=y0-a[i]*x0-b[i];
if(B*B-4*A*C<0) continue;
long double t0=(-B+sqrt(B*B-4*A*C))/(2*A);
long double t1=(-B-sqrt(B*B-4*A*C))/(2*A);
long double xx; // 斜面とぶつかる位置
xx=x0+vx*t0;
if(p[i].x<xx && xx<p[i+1].x){ point q(xx,-g/2*t0*t0+vy*t0+y0); ans+=dist(q,p[i+1]); break; }
xx=x0+vx*t1;
if(p[i].x<xx && xx<p[i+1].x){ point q(xx,-g/2*t1*t1+vy*t1+y0); ans+=dist(q,p[i+1]); break; }
}
i++;
}
}
printf("%.15Lf\n",ans);
return 0;
} |
#include <cstdio>
#include <vector>
#include <cmath>
#include <complex>
#include <iostream>
using namespace std;
#define EPS 1e-6
typedef long double ldouble;
typedef complex<ldouble> P;
ldouble cross(const P &a, const P &b){
return real(a) * imag(b) - imag(a) * real(b);
}
int n;
P z[10001];
ldouble Vabs;
const ldouble g = 9.8;
P jump(int &i){
P dir = z[i] - z[i-1];
P v = Vabs / abs(dir) * dir;
const P &base = z[i];
ldouble a = g / (real(v) * real(v) * 2.0);
ldouble b = imag(v) / real(v);
for(int j = i; j < n - 1; ++j){
P z1 = z[j] - base;
P z2 = z[j+1] - base;
ldouble c = imag(z2 - z1) / real(z2 - z1);
ldouble d = imag(z1) - c * real(z1);
ldouble e = sqrt((b-c) * (b-c) - 4.0 * a * d);
ldouble x = (b - c + e) / (2.0 * a);
if(x > real(z1) && x < real(z2)){
ldouble y = -a * x * x + b * x;
i = j;
return P(x, y) + base;
}
}
i = n;
return z[n-1];
}
int main(){
ldouble x, y;
cin >> n >> Vabs;
for(int i = 0; i < n; ++i){
cin >> x >> y;
z[i] = P(x, y);
}
ldouble ans = 0.0;
P prev = z[0];
for(int i = 1; i < n; ++i){
P dif = z[i] - prev;
ans += abs(dif);
if(i == n - 1) break;
if(cross(dif, z[i+1] - z[i]) < -EPS){
prev = jump(i);
}
else{
prev = z[i];
}
}
printf("%.20f\n", (double)ans);
} |
#include<bits/stdc++.h>
using namespace std;
double EPS=1e-9;
struct L{
double a,b,c;
L(double A,double B,double C){
a=A;
b=B;
c=C;
}
L(){}
};
struct P{
double x,y;
P(double X,double Y){
x=X;
y=Y;
}
P(){}
};
inline bool operator < (const P &a,const P &b){
if(a.x!=b.x){
return a.x<b.x;
}else return a.y<b.y;
}
P inter(L s,L t){
double det=s.a*t.b-s.b*t.a;
if(abs(det)<EPS){
return P(1000000009,1000000009);
}
return P((t.b*s.c-s.b*t.c)/det,(t.c*s.a-s.c*t.a)/det);
}
bool eq(P a, P b){
return abs(a.x-b.x)<EPS&&abs(a.y-b.y)<EPS;
}
double dot(P a,P b){
return a.x*b.x+a.y*b.y;
}
double cross(P a,P b){
return a.x*b.y-a.y*b.x;
}
double norm(P a){
return a.x*a.x+a.y*a.y;
}
int ccw(P a,P b,P c){
b.x-=a.x;
b.y-=a.y;
c.x-=a.x;
c.y-=a.y;
if(cross(b,c)>0)return 1;
if(cross(b,c)<0)return -1;
if(dot(b,c)<0)return 2;
if(norm(b)<norm(c))return -2;
return 0;
}
vector<P> convex_hull(vector<P> ps){
int n=ps.size();
int k=0;
std::sort(ps.begin(),ps.end());
vector<P> ch(2*n);
for(int i=0;i<n;ch[k++]=ps[i++]){
while(k>=2&&ccw(ch[k-2],ch[k-1],ps[i])<=0)--k;
}
for(int i=n-2,t=k+1;i>=0;ch[k++]=ps[i--]){
while(k>=t&&ccw(ch[k-2],ch[k-1],ps[i])<=0)--k;
}
ch.resize(k-1);
return ch;
}
char str[51][51];
int main(){
int a,b;
scanf("%d%d",&a,&b);
double mA1,mA2,mB1,mB2,mX;
scanf("%lf%lf%lf%lf%lf",&mA1,&mA2,&mB1,&mB2,&mX);
for(int i=0;i<a;i++){
scanf("%s",str[i]);
}
double tx=0,ux=0,ty=0,uy=0;
double vx=0,vy=0;
double wa=0,wb=0,wc=0;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
if(str[i][j]=='A'){
wa+=1;
tx+=0.5+i;
ty+=0.5+j;
}else if(str[i][j]=='B'){
wb+=1;
ux+=0.5+i;
uy+=0.5+j;
}else if(str[i][j]=='X'){
wc+=1;
vx+=0.5+i;
vy+=0.5+j;
}
}
}
wc*=mX;
vx*=mX;
vy*=mX;
double ret=0;
double div=(mA2-mA1)*(mB2-mB1);
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
if(str[i][j]=='.')continue;
double xl=i;
double xh=i+1;
double yl=j;
double yh=j+1;
vector<L> lines;
lines.push_back(L(1,0,mA1));
lines.push_back(L(1,0,mA2));
lines.push_back(L(0,1,mB1));
lines.push_back(L(0,1,mB2));
lines.push_back(L(tx-wa*xl,ux-wb*xl,-vx+wc*xl));
lines.push_back(L(tx-wa*xh,ux-wb*xh,-vx+wc*xh));
lines.push_back(L(ty-wa*yl,uy-wb*yl,-vy+wc*yl));
lines.push_back(L(ty-wa*yh,uy-wb*yh,-vy+wc*yh));
// if(i+j==0)for(int k=0;k<8;k++)printf("%f %f %f\n",lines[k].a,lines[k].b,lines[k].c);
vector<P> cons;
for(int k=0;k<8;k++){
for(int l=k+1;l<8;l++){
P v=inter(lines[k],lines[l]);
if(abs(v.x-1000000009)>EPS){
cons.push_back(v);
}
}
}
vector<P> ser;
for(int k=0;k<cons.size();k++){
bool ok=true;
for(int l=0;l<8;l++){
if(l%2==0){
if(cons[k].x*lines[l].a+cons[k].y*lines[l].b+EPS<lines[l].c)ok=false;
}else{
if(cons[k].x*lines[l].a+cons[k].y*lines[l].b>EPS+lines[l].c)ok=false;
}
}
if(ok)ser.push_back(cons[k]);
}
if(ser.size()>=3){
vector<P> D=convex_hull(ser);
// if(i==2&&j==0)for(int k=0;k<D.size();k++)printf("%f %f\n",D[k].x,D[k].y);
double S=0;
for(int k=0;k<D.size();k++){
S+=cross(D[k],D[(k+1)%D.size()]);
}
ret+=S/2;
// printf("%d %d: %f\n",i,j,S/2);
}
}
}
printf("%.12f\n",ret/div);
} |
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
struct Point{
Point(double arg_x,double arg_y){
x = arg_x;
y = arg_y;
}
Point(){
x = y = 0.0;
}
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;
}
double x,y;
};
typedef Point Vector;
typedef vector<Point> Polygon;
struct Line{
Point p[2];
Line(Point p1,Point p2){
p[0] = p1;
p[1] = p2;
}
Line(){
}
};
int H,W;
double num_A,num_B,num_X;
double m_A1,m_A2,m_B1,m_B2,m_X;
char table[55][55];
double norm(Vector a){
return a.x*a.x+a.y*a.y;
}
double abs(Vector a){
return sqrt(norm(a));
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double dot(Vector a,Vector b){
return a.x*b.x + a.y*b.y;
}
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -1;
static const int ON_SEGMENT = 0;
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;
}
//交点を求める関数
Point calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){
Point ret;
ret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));
if(x1 != x2){
ret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);
}else{
ret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);
}
return ret;
}
//インタフェース関数
Point calc_Cross_Point(Point a,Point b,Point c,Point d){
return calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);
}
Polygon ConvexCut(Polygon g,Point a,Point b){
Polygon ret;
int N = g.size();
for(int i = 0; i < g.size(); i++){
Point A = g[i], B = g[(i+1)%N];
if(ccw(a,b,A) != -1)ret.push_back(A);
if(ccw(a,b,A)*ccw(a,b,B) == -1)ret.push_back(calc_Cross_Point(a,b,A,B));
}
return ret;
}
double calc_S(Polygon g){
int N = g.size();
double ret = 0;
for(int i = 0; i < g.size(); i++){
ret += cross(g[i],g[(i+1)%N]);
}
return ret/2.0;
}
double calc_slope(Line A){
if(fabs(A.p[0].x-A.p[1].x) < EPS){
return DBL_MAX;
}else if(fabs(A.p[0].y-A.p[1].y) < EPS){
return 0;
}else{
return (A.p[0].y-A.p[1].y)/(A.p[0].x-A.p[1].x);
}
}
//★★直線の交差判定★★
bool is_Cross(Line A,Line B){
return fabs(calc_slope(A)-calc_slope(B)) > EPS;
}
int main(){
scanf("%d %d",&H,&W);
scanf("%lf %lf %lf %lf %lf",&m_A1,&m_A2,&m_B1,&m_B2,&m_X);
num_A = num_B = num_X = 0.0;
Vector vec_A = Vector(0,0),vec_B = Vector(0,0),vec_X = Vector(0,0);
double d_H,d_row,d_col;
for(int row = 0; row < H; row++){
scanf("%s",table[row]);
for(int col = 0; col < W; col++){
if(table[row][col] == '.')continue;
d_H = H;
d_row = row;
d_col = col;
switch(table[row][col]){
case 'A':
num_A += 1.0;
vec_A.x += d_col+0.5;
vec_A.y += d_H-d_row-0.5;
break;
case 'B':
num_B += 1.0;
vec_B.x += d_col+0.5;
vec_B.y += d_H-d_row-0.5;
break;
case 'X':
num_X += 1.0;
vec_X.x += d_col+0.5;
vec_X.y += d_H-d_row-0.5;
break;
}
}
}
double ans = 0;
bool FLG;
for(int row = 0; row < H; row++){
for(int col = 0; col < W; col++){
if(table[row][col] == '.')continue;
FLG = true;
double x = col;
double y = H-1-row;
Polygon polygon;
polygon.push_back(Point(m_A2,m_B2));
polygon.push_back(Point(m_A2,m_B1));
polygon.push_back(Point(m_A1,m_B1));
polygon.push_back(Point(m_A1,m_B2));
//x < 重心.x
if(fabs(num_A*x-vec_A.x) < EPS && fabs(num_B*x-vec_B.x) < EPS){
if((num_X*x-vec_X.x)*m_X >= 0){
FLG = false;
}
}else{
if(fabs(num_B*x-vec_B.x) > EPS){
double a = (vec_A.x-num_A*x)/(num_B*x-vec_B.x);
double b = ((vec_X.x-num_X*x)*m_X)/(num_B*x-vec_B.x);
Point p2 = Point(0,b);
Point p1 = Point(1,a+b);
if(num_B*x-vec_B.x < 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}else{
double a = ((num_X*x-vec_X.x)*m_X)/(vec_A.x-num_A*x);
Point p2 = Point(a,0);
Point p1 = Point(a,1);
if(vec_A.x-num_A*x > 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}
}
if(!FLG)continue;
//重心.x < x+1
if(fabs(num_A*(x+1)-vec_A.x) < EPS && fabs(num_B*(x+1)-vec_B.x) < EPS){
if((num_X*(x+1)-vec_X.x)*m_X <= 0){
FLG = false;
}
}else{
if(fabs(num_B*(x+1)-vec_B.x) > EPS){
double a = (vec_A.x-num_A*(x+1))/(num_B*(x+1)-vec_B.x);
double b = ((vec_X.x-num_X*(x+1))*m_X)/(num_B*(x+1)-vec_B.x);
Point p2 = Point(0,b);
Point p1 = Point(1,a+b);
if(num_B*(x+1)-vec_B.x > 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}else{
double a = ((num_X*(x+1)-vec_X.x)*m_X)/(vec_A.x-num_A*(x+1));
Point p2 = Point(a,0);
Point p1 = Point(a,1);
if(vec_A.x-num_A*(x+1) < 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}
}
if(!FLG)continue;
//y < 重心.y
if(fabs(num_A*y-vec_A.y) < EPS && fabs(num_B*y-vec_B.y) < EPS){
if((num_X*y-vec_X.y)*m_X >= 0){
FLG = false;
}
}else{
if(fabs(num_B*y-vec_B.y) > EPS){
double a = (vec_A.y-num_A*y)/(num_B*y-vec_B.y);
double b = ((vec_X.y-num_X*y)*m_X)/(num_B*y-vec_B.y);
Point p2 = Point(0,b);
Point p1 = Point(1,a+b);
if(num_B*y-vec_B.y < 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}else{
double a = ((num_X*y-vec_X.y)*m_X)/(vec_A.y-num_A*y);
Point p2 = Point(a,0);
Point p1 = Point(a,1);
if(vec_A.y-num_A*y > 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}
}
if(!FLG)continue;
//重心.y < y+1
if(fabs(num_A*(y+1)-vec_A.y) < EPS && fabs(num_B*(y+1)-vec_B.y) < EPS){
if((num_X*(y+1)-vec_X.y)*m_X <= 0){
FLG = false;
}
}else{
if(fabs(num_B*(y+1)-vec_B.y) > EPS){
double a = (vec_A.y-num_A*(y+1))/(num_B*(y+1)-vec_B.y);
double b = ((vec_X.y-num_X*(y+1))*m_X)/(num_B*(y+1)-vec_B.y);
Point p2 = Point(0,b);
Point p1 = Point(1,a+b);
if(num_B*(y+1)-vec_B.y > 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}else{
double a = ((num_X*(y+1)-vec_X.y)*m_X)/(vec_A.y-num_A*(y+1));
Point p2 = Point(a,0);
Point p1 = Point(a,1);
if(vec_A.y-num_A*(y+1) < 0){
swap(p1,p2);
}
polygon = ConvexCut(polygon,p1,p2);
}
}
if(!FLG)continue;
ans += calc_S(polygon)/((m_A2-m_A1)*(m_B2-m_B1));
}
}
printf("%.10lf\n",fabs(ans));
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P,P> L;
typedef vector<P> Pol;
D eps=1e-9;
bool eq(D a,D b){return abs(a-b)<eps;}
int sig(D a){return eq(a,0)?0:(a>0?1:-1);}
void fix(D &x){if(sig(x)==0) x=0;}
D cro(P a,P b){return imag(conj(a)*b);}
int ccw(P a,P b,P c){
if(sig(cro(b-a,c-a))==1) return 1;
if(sig(cro(b-a,c-a))==-1) return -1;
if(eq(abs(a-c)+abs(c-b),abs(a-b))) return 0;
if(eq(abs(a-b)+abs(c-b),abs(a-c))) return -2;
return 2;
}
D aPol(Pol p){
int N=p.size();
D ret=0;
rep(i,N) ret+=cro(p[i],p[(i+1)%N])/2;
return ret;
}
bool iLSex(L l,L s){
return sig(cro(l.sc-l.fs,s.fs-l.fs)*cro(l.sc-l.fs,s.sc-l.fs))<0;
}
P intLL(L a,L b){
D t=cro(a.sc-a.fs,a.sc-b.fs)/cro(a.sc-a.fs,b.sc-b.fs);
return b.fs+t*(b.sc-b.fs);
}
Pol convexcut(Pol p,L l){
Pol ret;
int N=p.size();
rep(i,N){
if(ccw(l.fs,l.sc,p[i])!=-1) ret.pb(p[i]);
L s=L(p[i],p[(i+1)%N]);
if(iLSex(l,s)) ret.pb(intLL(l,s));
}
return ret;
}
int H,W;
D la,ra,lb,rb,mx;
string s[50];
int main(){
cin>>H>>W>>la>>ra>>lb>>rb>>mx;
rep(i,H) cin>>s[i];
int ca=0,cb=0,cx=0;
D xa=0,xb=0,xx=0,ya=0,yb=0,yx=0;
rep(i,H) rep(j,W){
if(s[i][j]=='A'){
xa+=j+0.5,ya+=i+0.5,ca++;
}else if(s[i][j]=='B'){
xb+=j+0.5,yb+=i+0.5,cb++;
}else if(s[i][j]=='X'){
xx+=j+0.5,yx+=i+0.5,cx++;
}
}
xa/=ca,ya/=ca,xb/=cb,yb/=cb,xx/=cx,yx/=cx;
mx*=cx;
la*=ca,ra*=ca,lb*=cb,rb*=cb;
D ans=0;
rep(ii,H) rep(jj,W){
D x=jj,y=ii;
Pol pol;
pol.pb(P(la,lb));
pol.pb(P(ra,lb));
pol.pb(P(ra,rb));
pol.pb(P(la,rb));
{
D A=xa-x,B=xb-x,C=(xx-x)*mx;
fix(A),fix(B),fix(C);
if(B!=0){
P p(0,-C/B),q(1,-(C+A)/B);
if(B<0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else if(A!=0){
P p(-C/A,0),q(-C/A,1);
if(A>0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else{
if(C<0) pol.clear();
}
}
{
D A=xa-x-1,B=xb-x-1,C=(xx-x-1)*mx;
fix(A),fix(B),fix(C);
if(B!=0){
P p(0,-C/B),q(1,-(C+A)/B);
if(B>0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else if(A!=0){
P p(-C/A,0),q(-C/A,1);
if(A<0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else{
if(C>0) pol.clear();
}
}
{
D A=ya-y,B=yb-y,C=(yx-y)*mx;
fix(A),fix(B),fix(C);
if(B!=0){
P p(0,-C/B),q(1,-(C+A)/B);
if(B<0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else if(A!=0){
P p(-C/A,0),q(-C/A,1);
if(A>0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else{
if(C<0) pol.clear();
}
}
{
D A=ya-y-1,B=yb-y-1,C=(yx-y-1)*mx;
fix(A),fix(B),fix(C);
if(B!=0){
P p(0,-C/B),q(1,-(C+A)/B);
if(B>0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else if(A!=0){
P p(-C/A,0),q(-C/A,1);
if(A<0) swap(p,q);
pol=convexcut(pol,L(p,q));
}else{
if(C>0) pol.clear();
}
}
if(s[ii][jj]!='.') ans+=(aPol(pol)/(ra-la)/(rb-lb));
}
printf("%.12f\n",ans);
} |
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
return is >> p.FF >> p.SS;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9+7;
typedef vector<LL> Col;
typedef vector<Col> Matrix;
Matrix mul(const Matrix& A, const Matrix& B){
const int R = A.size(), C = B[0].size(), sz = B.size();
Matrix AB(R, Col(C));
for(int i=0;i<R;++i)
for(int j=0;j<C;++j)
for(int k=0;k<sz;++k)
(AB[i][j] += A[i][k] * B[k][j]) %= MOD;
return AB;
}
// O(N^3 lgN)
Matrix powA(const Matrix& A, int n){
const int N = A.size();
Matrix p(N, Col(N, 0)), w = A;
for(int i=0;i<N;++i) p[i][i] = 1;
while(n>0){
if(n&1)
p = mul(p, w);
w = mul(w, w);
n >>= 1;
}
return p;
}
void dump(const Matrix& A){
REP(i,SZ(A)){
REP(j,SZ(A[i]))
cout << A[i][j] << (j%5==4?" | ":" ");
cout << endl;
if(i%5==4)cout<<string(SZ(A)+20,'-')<<endl;
}
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, K, C, T; cin >> N >> K >> C >> T;
--C;
VI as(K), bs(K), ts(K);
Matrix A(5*N, Col(5*N));
REP(i,N) REP(t,4)
A[5*i+t][5*i+t+1]++;
REP(i,K){
cin >> as[i] >> bs[i] >> ts[i];
--as[i];
--ts[i];
for(int k=0;k<bs[i];++k)
A[k*5+ts[i]][(as[i]+k)*5]++;
for(int k=0;k<as[i];++k)
A[(bs[i]+k)*5+ts[i]][k*5]++;
for(int k=as[i]+bs[i];k<N;++k)
A[5*k+ts[i]][5*k]++;
}
A = powA(A, T);
cout << A[0][5*C] << endl;
return 0;
} |
#include <cstdio>
#include <vector>
using namespace std;
#define MOD 1000000007
typedef long long int LLI;
typedef vector<vector<LLI> > Mat;
typedef vector<vector<Mat> > MatMat;
int n;
int k;
int c;
int T;
Mat one;
Mat zero;
Mat G[7];
MatMat m;
MatMat m_zero;
MatMat ans;
Mat mul(Mat &a, Mat &b) {
Mat ret(zero);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
for (int k=0; k<n; k++) {
ret[i][j] += a[i][k] * b[k][j] % MOD;
ret[i][j] %= MOD;
}
}
}
return ret;
}
Mat add(Mat &a, Mat &b) {
Mat ret(zero);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
ret[i][j] = (a[i][j] + b[i][j]) % MOD;
}
}
return ret;
}
MatMat mul(MatMat &a, MatMat &b) {
MatMat ret(m_zero);
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
for (int k=0; k<5; k++) {
Mat s = mul(a[i][k], b[k][j]);
ret[i][j] = add(ret[i][j], s);
}
}
}
return ret;
}
int main() {
scanf("%d%d%d%d", &n, &k, &c, &T);
zero = Mat(n, vector<LLI>(n));
one = Mat(zero);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
zero[i][j] = 0;
one[i][j] = 0;
}
one[i][i] = 1;
}
for (int i=0; i<7; i++) {
G[i] = Mat(zero);
}
for (int i=0; i<k; i++) {
int a, b, t;
scanf("%d%d%d", &a, &b, &t);
--a;
for (int j=0; j<a; j++) {
G[t][j][b+j]++;
}
for (int j=0; j<b; j++) {
G[t][a+j][j]++;
}
for (int j=a+b; j<n; j++) {
G[t][j][j]++;
}
}
m_zero = vector<vector<Mat> >(5, vector<Mat>(5, zero));
ans = vector<vector<Mat> >(m_zero);
m = vector<vector<Mat> >(m_zero);
for (int i=0; i<5; i++) {
m[0][i] = G[i+1];
if (i < 4) m[i+1][i] = one;
ans[i][i] = one;
}
while (T > 0) {
if (T&1) {
ans = mul(ans, m);
}
m = mul(m, m);
T >>= 1;
}
printf("%lld\n", ans[0][0][c-1][0]);
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
using vl = vector<ll>;
using mat = vector<vl>;
const ll mod = 1e9+7;
mat mul(const mat &a, const mat &b)
{
int n = a.size();
mat c(n,vl(n));
rep(i,n)rep(j,n)rep(k,n) (c[i][j]+=a[i][k]*b[k][j])%=mod;
return c;
}
vl mul(const mat &a, const vl &b)
{
int n = a.size();
vl c(n);
rep(i,n)rep(j,n) (c[i]+=a[i][j]*b[j])%=mod;
return c;
}
mat mat_pow(const mat &a, int T)
{
int n = a.size();
mat ret(n,vl(n));
rep(i,n) ret[i][i] = 1;
mat p(a);
while(T)
{
if(T&1) ret = mul(ret,p);
p = mul(p,p);
T>>=1;
}
return ret;
}
int main()
{
int n,k,C,T;
cin >>n >>k >>C >>T;
--C;
vector<int> a(k),b(k),t(k);
rep(i,k)
{
cin >>a[i] >>b[i] >>t[i];
--a[i];
}
int SZ = 5*n;
mat A(SZ,vl(SZ));
// make A
for(int i=n; i<5*n; ++i) A[i-n][i] += 1;
rep(i,k)
{
rep(j,n)
{
int idx = (t[i]-1)*n+j;
if(j<a[i]) idx += b[i];
else if(j<a[i]+b[i]) idx -= a[i];
A[idx][j] += 1;
}
}
vl B(SZ);
B[C] = 1;
vl res = mul(mat_pow(A,T),B);
cout << res[0] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD
const int mod = 1000000007;
ll R,R2,modash;
int logR;
ll MR(ll x){
ll ret = (x + ((x*modash)&(R-1ll))*mod)>>(logR);
return (ret>=mod ? ret-mod : ret);
}
int get_mod(ll x){return (int)MR(MR(x)*R2);}
void init(){
R=1ll;logR=0;modash=0;
while(R<mod){R<<=1;logR++;}
R2=R*R%mod;
int t=0,r=R,i=1;
while(r>1){
if((t&1)==0){t+=mod;modash+=i;}
t>>=1;r>>=1;i<<=1;
}
}
vector<vl> mul(vector<vl> a, vector<vl> b){
int n = a.size();
vector<vl> ret(n,vl(n,0));
REP(k,n)REP(i,n)REP(j,n){
ret[i][j] += a[i][k]*b[k][j]%mod;
ret[i][j] %= mod;
// ret[i][j] = get_mod(ret[i][j] + get_mod(a[i][k])*get_mod(b[k][j]));
}
return ret;
}
int main(){
int n,k,c,t;
scanf("%d%d%d%d",&n,&k,&c,&t);
// ????§???????
// 0?§???????1?§????...4?§????????£??????§??\?????????????????°t?????§?????¨?????????
vector<vl> mat(5*n,vl(5*n,0));
// ??????????§?
REP(i,4){
REP(j,n) mat[i*n+j][(i+1)*n+j] += 1;
}
// ?????????????§?
REP(_,k){
int a,b,tm;
scanf("%d%d%d",&a,&b,&tm);
// --a; --b;
--tm;
REP(i,n){
int from;
if(i<b) from = i+a-1;
else if(i<b+a-1) from = i-b;
else from = i;
int to = i;
mat[tm*n+to][from] += 1;
}
}
// REP(po,5*n){
// DEBUG_VEC(mat[po]);
// }
// cout<<endl;
// init();
// ?´????
vector<vl> ans(5*n,vl(5*n,0));
REP(i,5*n) ans[i][i]=1;
while(t){
if(t&1) ans = mul(ans,mat);
mat = mul(mat,mat);
t>>=1;
}
// DEBUG
// REP(po,5*n){
// DEBUG_VEC(ans[po]);
// }
cout<<ans[0][c-1]<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
#define MOD 1000000007
typedef vector<vector<long>> mat;
// return A*B
mat mat_mul(const mat &A, const mat &B){
int n=A.size(), m=B[0].size(), l=B.size();
mat ret(n, vector<long>(m, 0));
rep(i,n) rep(k,l) if(A[i][k]!=0) rep(j,m){
(ret[i][j] += A[i][k] * B[k][j]) %= MOD;
}
return ret;
}
// A^p
mat mat_pow(const mat &A, long p){
int n = A.size();
mat tmp(A), ret(n, vector<long>(n,0));
rep(i,n) ret[i][i] = 1;
while(p>0){
if(p&1) ret = mat_mul(tmp, ret);
tmp = mat_mul(tmp, tmp);
p /= 2;
}
return ret;
}
int main(){
int n,k,c,T;
cin>>n>>k>>c>>T;
c--;
vector<int> a(k), b(k), t(k);
rep(i,k) cin>>a[i]>>b[i]>>t[i];
rep(i,k) a[i]--;
int sz = 5*n;
mat A(sz, vector<long>(sz, 0));
rep(i,n,sz){
A[i-n][i] += 1;
}
rep(i,k){
rep(j,n){
int to = (t[i]-1)*n + j;
if(j < a[i]) to += b[i];
else if(j < a[i]+b[i]) to -= a[i];
A[to][j] += 1;
}
}
A = mat_pow(A, T);
cout << A[0][c] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
typedef vector< vector<ll> > Matrix;
int N, K, C, T;
vector<int> shuffle(vector<int>& v, int a, int b) {
vector<int> res;
for (int i = 0; i < b; ++i) {
res.push_back(v[a+i]);
}
for (int i = 0; i < a; ++i) {
res.push_back(i);
}
for (int i = a+b; i < N; ++i) {
res.push_back(i);
}
return res;
}
vector<int> make_shuffle(int a, int b) {
vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i;
}
v = shuffle(v, a, b);
vector<int> res(N);
for (int i = 0; i < N; ++i) {
res[v[i]] = i;
}
return res;
}
vector< vector<ll> > make_E() {
vector< vector<ll> > res(N*5, vector<ll>(N*5, 0));
for (int i = 0; i < N*5; ++i) {
res[i][i] = 1;
}
return res;
}
vector< vector<ll> > mul(Matrix A, Matrix B) {
vector< vector<ll> > res(N*5, vector<ll>(N*5, 0));
for (int k = 0; k < N*5; ++k) {
for (int j = 0; j < N*5; ++j) {
for (int i = 0; i < N*5; ++i) {
res[i][j] = (res[i][j] + A[i][k] * B[k][j] % mod);
if (res[i][j] >= mod) res[i][j] -= mod;
}
}
}
return res;
}
vector< vector<ll> > power(Matrix A, int n) {
Matrix res = make_E();
for (int i = 1; i <= n; i <<= 1) {
if (n & i) {
res = mul(res, A);
}
A = mul(A, A);
}
return res;
}
int main() {
cin >> N >> K >> C >> T;
vector< vector< vector<ll> > > pat(5, vector< vector<ll> >(N, vector<ll>(N, 0)));
for (int i = 0; i < K; ++i) {
int a, b, t; cin >> a >> b >> t; --a, --t;
vector<int> to = make_shuffle(a, b);
for (int j = 0; j < to.size(); ++j) {
++pat[t][j][to[j]];
}
}
vector< vector<ll> > G(N*5, vector<ll>(N*5, 0));
for (int t = 0; t < 5; ++t) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
int from = i*5;
int to = j*5+t;
G[from][to] += pat[t][i][j];
}
}
}
for (int i = 0; i < N; ++i) {
for (int t = 1; t < 5; ++t) {
G[i*5+t][i*5+t-1] = 1;
}
}
vector< vector<ll> > g = power(G, T);
/* for (int i = 0; i < N*5; ++i) {
for (int j = 0; j < N*5; ++j) {
cout << g[i][j] << " ";
}
cout << endl;
}
*/ cout << g[(C-1)*5][0*5] << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD
const int mod = 1000000007;
ll R,R2,modash;
int logR;
ll MR(ll x){
ll ret = (x + (((x&(R-1ll))*modash)&(R-1ll))*mod)>>(logR);
return (ret>=mod ? ret-mod : ret);
}
int get_mod(ll x){return (int)MR(MR(x)*R2);}
void init(){
R=1ll;logR=0;modash=0;
while(R<mod){R<<=1;logR++;}
R2=R*R%mod;
int t=0,r=R,i=1;
while(r>1){
if((t&1)==0){t+=mod;modash+=i;}
t>>=1;r>>=1;i<<=1;
}
}
vector<vl> mul(vector<vl> a, vector<vl> b){
int n = a.size();
vector<vl> ret(n,vl(n,0));
REP(k,n)REP(i,n)REP(j,n){
ret[i][j] += get_mod(a[i][k]*b[k][j]);
if(ret[i][j]>=mod) ret[i][j] -= mod;
}
return ret;
}
int main(){
int n,k,c,t;
scanf("%d%d%d%d",&n,&k,&c,&t);
// ????§???????
// 0?§???????1?§????...4?§????????£??????§??\?????????????????°t?????§?????¨?????????
vector<vl> mat(5*n,vl(5*n,0));
// ??????????§?
REP(i,4){
REP(j,n) mat[i*n+j][(i+1)*n+j] += 1;
}
// ?????????????§?
REP(_,k){
int a,b,tm;
scanf("%d%d%d",&a,&b,&tm);
// --a; --b;
--tm;
REP(i,n){
int from;
if(i<b) from = i+a-1;
else if(i<b+a-1) from = i-b;
else from = i;
int to = i;
mat[tm*n+to][from] += 1;
}
}
// REP(po,5*n){
// DEBUG_VEC(mat[po]);
// }
// cout<<endl;
init();
// ?´????
vector<vl> ans(5*n,vl(5*n,0));
REP(i,5*n) ans[i][i]=1;
while(t){
if(t&1) ans = mul(ans,mat);
mat = mul(mat,mat);
t>>=1;
}
// DEBUG
// REP(po,5*n){
// DEBUG_VEC(ans[po]);
// }
cout<<ans[0][c-1]<<endl;
return 0;
} |
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
typedef vector<ll> V;
typedef vector<V> MATRIX;
int SIZE;
int base[40],work[40],next_loc[40];
MATRIX calc(MATRIX left,MATRIX right){
MATRIX ret(SIZE,V(SIZE));
for(int i = 0; i < SIZE; i++){
for(int k = 0; k < SIZE; k++)ret[i][k] = 0;
}
for(int row = 0; row < SIZE; row++){
for(int col = 0; col < SIZE; col++){
for(int a = 0; a < SIZE; a++){
ret[row][col] += left[row][a]*right[a][col]%MOD;
ret[row][col] %= MOD;
}
}
}
return ret;
}
MATRIX pow(MATRIX MULT,int count){
MATRIX ret(SIZE,V(SIZE));
for(int row = 0; row < SIZE; row++){
for(int col = 0; col < SIZE; col++){
if(row == col)ret[row][col] = 1;
else{
ret[row][col] = 0;
}
}
}
while(count > 0){
if(count%2 == 1)ret = calc(ret,MULT);
MULT = calc(MULT,MULT);
count /= 2;
}
return ret;
}
int main(){
int N,K,C,T;
scanf("%d %d %d %d",&N,&K,&C,&T);
C--;
for(int i = 0; i < N; i++)base[i] = i;
SIZE = 5*N;
MATRIX MULT(SIZE,V(SIZE));
for(int row = 0; row < SIZE; row++){
for(int col = 0; col < SIZE; col++)MULT[row][col] = 0;
}
int from,num,add_time;
int work_index,base_row = 0,base_col;
for(int loop = 0; loop < K; loop++){
scanf("%d %d %d",&from,&num,&add_time);
from--;
work_index = 0;
for(int i = 0;i < num; i++){
work[work_index++] = base[from+i];
}
for(int i = 0; i < N; i++){
if(i >= from && i <= from+num-1)continue;
work[work_index++] = base[i];
}
base_col = N*(add_time-1);
for(int i = 0; i < N; i++){
MULT[base_row+work[i]][base_col+i]++;
}
}
for(base_row = N; base_row <= 4*N; base_row += N){
for(base_col = 0; base_col <= 4*N; base_col += N){
if(base_row-N != base_col)continue;
for(int i = 0; i < N; i++)MULT[base_row+i][base_col+i] = 1;
}
}
MULT = pow(MULT,T);
printf("%lld\n",MULT[C][0]%MOD);
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
#define rep1(i,N) for(int i=1;i<=(int)N;i++)
#define pb push_back
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> mat;
ll mod=1e9+7;
void add(ll &x,ll y){
x+=y;
x%=mod;
}
mat pro(mat a,mat b){
int N=a.size();
mat c(N,vi(N,0));
rep(i,N) rep(j,N) rep(k,N) add(c[i][j],a[i][k]*b[k][j]);
return c;
}
mat ex(mat x,int p){
int N=x.size();
mat a(N,vi(N,0));
rep(i,N) a[i][i]=1;
while(p){
if(p%2) a=pro(a,x);
x=pro(x,x);
p/=2;
}
return a;
}
int N,K,C,T,t[1600],to[1600][40];
int main(){
cin>>N>>K>>C>>T;
rep(i,K){
int a,b;
cin>>a>>b>>t[i];
a--;
rep(j,N){
if(a<=j&&j<a+b) to[i][j]=j-a;
if(j<a) to[i][j]=j+b;
if(j>=a+b) to[i][j]=j;
}
}
int s=5*N;
mat M(s,vi(s,0));
rep(i,4*N) M[i][i+N]=1;
rep(i,K){
rep(j,N){
int nj=to[i][j];
int x=(5-t[i])*N+j;
int nx=4*N+nj;
M[nx][x]++;
}
}
ll dp[5][40]={};
dp[0][C-1]=1;
rep(i,4){
rep(j,K){
if(i+t[j]>=5) continue;
rep(k,N){
add(dp[i+t[j]][to[j][k]],dp[i][k]);
}
}
}
if(T<=4){
cout<<dp[T][0]<<endl;
return 0;
}
vi v(5*N,0);
rep(i,5) rep(j,N) v[i*N+j]=dp[i][j];
mat e=ex(M,T-4);
ll ans=0;
rep(j,5*N) add(ans,e[4*N][j]*v[j]);
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD
const int mod = 1000000007;
const int logR = 30;
const int R = 1<<logR;
const int Rmask = R-1;
const int R2 = (int)((ll)R*R%mod);
int modash;
int MR(ll x){
int m = (x*modash)&Rmask;
int t = (x+(ll)m*mod)>>logR;
if(t>=mod)t-=mod;
return t;
}
int get_mod(ll x){return (int)MR((ll)MR(x)*R2);}
void init(){
// R=1ll;logR=0;modash=0;
// while(R<mod){R<<=1;logR++;}
// R2=R*R%mod;
int t=0,r=R,i=1;
while(r>1){
if((t&1)==0){t+=mod;modash+=i;}
t>>=1;r>>=1;i<<=1;
}
}
vector<vl> mul(vector<vl> a, vector<vl> b){
int n = a.size();
vector<vl> ret(n,vl(n,0));
REP(k,n)REP(i,n)REP(j,n){
ret[i][j] += get_mod(a[i][k]*b[k][j]);
if(ret[i][j]>=mod) ret[i][j] -= mod;
}
return ret;
}
int main(){
int n,k,c,t;
scanf("%d%d%d%d",&n,&k,&c,&t);
// ????§???????
// 0?§???????1?§????...4?§????????£??????§??\?????????????????°t?????§?????¨?????????
vector<vl> mat(5*n,vl(5*n,0));
// ??????????§?
REP(i,4){
REP(j,n) mat[i*n+j][(i+1)*n+j] += 1;
}
// ?????????????§?
REP(_,k){
int a,b,tm;
scanf("%d%d%d",&a,&b,&tm);
// --a; --b;
--tm;
REP(i,n){
int from;
if(i<b) from = i+a-1;
else if(i<b+a-1) from = i-b;
else from = i;
int to = i;
mat[tm*n+to][from] += 1;
}
}
// REP(po,5*n){
// DEBUG_VEC(mat[po]);
// }
// cout<<endl;
init();
// ?´????
vector<vl> ans(5*n,vl(5*n,0));
REP(i,5*n) ans[i][i]=1;
while(t){
if(t&1) ans = mul(ans,mat);
mat = mul(mat,mat);
t>>=1;
}
// DEBUG
// REP(po,5*n){
// DEBUG_VEC(ans[po]);
// }
cout<<ans[0][c-1]<<endl;
return 0;
} |
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
// ModInt begin
using ll = long long;
template<ll mod>
struct ModInt {
ll v;
ll mod_pow(ll x, ll n) const {
return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;
}
ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
ModInt operator+ ( const ModInt& b ) const {
return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));
}
ModInt operator- () const {
return ModInt(-v);
}
ModInt operator- ( const ModInt& b ) const {
return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));
}
ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}
ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}
bool operator== ( const ModInt &b ) const {return v == b.v;}
bool operator!= ( const ModInt &b ) const {return !(*this == b); }
ModInt& operator+= ( const ModInt &b ) {
v += b.v;
if(v >= mod) v -= mod;
return *this;
}
ModInt& operator-= ( const ModInt &b ) {
v -= b.v;
if(v < 0) v += mod;
return *this;
}
ModInt& operator*= ( const ModInt &b ) {
(v *= b.v) %= mod;
return *this;
}
ModInt& operator/= ( const ModInt &b ) {
(v *= mod_pow(b.v, mod-2)) %= mod;
return *this;
}
ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }
// operator int() const { return int(v); }
// operator long long int() const { return v; }
};
template<ll mod>
ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
template<ll mod>
istream& operator>> (istream& in, ModInt<mod>& a) {
in >> a.v;
return in;
}
// ModInt end
// 行列ライブラリ
// size(): 行数を返す (列数は mat[0].size() で)
// 演算子: 複合代入 (+=, *=, -=), 単項 (-), 二項 (+, -, *, ==)
// eigen(N): N*N 単位行列を返す
// pow(mat, k): mat の k 乗を返す
template <typename T>
struct Matrix {
vector< vector<T> > mat;
Matrix() {}
Matrix(int h, int w, T val = T(0)) : mat(h, vector<T>(w, val)) {}
size_t size() const { return mat.size(); }
const vector<T>& operator[](int i) const { return mat[i]; }
vector<T>& operator[](int i) { return mat[i]; }
Matrix<T> &operator+=(const Matrix<T>& rhs) {
assert(mat.size() == rhs.size());
assert(mat[0].size() == rhs[0].size());
for(size_t i=0; i<mat.size(); i++) {
for(size_t j=0; j<mat[0].size(); j++) {
mat[i][j] += rhs[i][j];
}
}
return *this;
}
Matrix<T> operator-() const {
Matrix<T> res(*this);
for(size_t i=0; i<res.size(); i++) {
for(size_t j=0; j<res[0].size(); j++) {
res[i][j] *= T(-1);
}
}
return res;
}
Matrix<T>& operator-=(const Matrix<T>& rhs) {
return (Matrix<T>(*this) += -rhs);
}
Matrix<T>& operator*=(const Matrix<T>& rhs) {
assert(mat[0].size() == rhs.size());
size_t H = mat.size(), W = rhs[0].size(), C = rhs.size();
Matrix<T> res(H, W);
for(size_t i=0; i<H; i++) {
for(size_t j=0; j<W; j++) {
for(size_t k=0; k<C; k++) {
res[i][j] += mat[i][k] * rhs[k][j];
}
}
}
this->mat = res.mat;
return *this;
}
Matrix<T> operator+(const Matrix<T>& rhs) {
return (Matrix<T>(*this) += rhs);
}
Matrix<T> operator*(const Matrix<T>& rhs) {
return (Matrix<T>(*this) *= rhs);
}
Matrix<T> operator-(const Matrix<T> &rhs) {
return (Matrix<T>(*this) -= rhs);
}
bool operator==(const Matrix<T> &rhs) const {
return this->mat == rhs.mat;
}
bool operator!=(const Matrix<T> &rhs) const {
return !(*this == rhs);
}
};
template <typename T>
Matrix<T> eigen(size_t N) {
Matrix<T> res(N, N, 0);
for(size_t i=0; i<N; i++) res[i][i] = T(1);
return res;
}
template <typename T>
Matrix<T> pow(Matrix<T> mat, long long int k) {
Matrix<T> res = eigen<T>(mat.size());
for(; k>0; k>>=1) {
if(k & 1) res *= mat;
mat *= mat;
}
return res;
}
template <typename T>
ostream& operator<< (ostream& out, Matrix<T> mat) {
int H = mat.size(), W = mat[0].size();
out << "[" << endl;
for(int i=0; i<H; i++) {
out << " [ ";
for(int j=0; j<W; j++) out << mat[i][j] << " ";
out << "]" << endl;
}
out << "]" << endl;
return out;
}
using mint = ModInt<MOD>;
int main() {
int N, K, C, T; cin >> N >> K >> C >> T; C--;
int M = 5 * N;
Matrix<mint> mat(M, M);
auto get_idx = [&](int time, int pos) {
return time * N + pos;
};
for(int i=0; i<K; i++) {
int a, b, t; cin >> a >> b >> t; a--;
int l = a, r = a + b;
vector<int> x, y;
for(int j=0; j<N; j++) {
if(l <= j and j < r) x.emplace_back(j);
else y.emplace_back(j);
}
for(auto e : y) x.emplace_back(e);
for(int j=0; j<N; j++) {
int k = x[j]; // もともと k 番目だったものが j 番目に
for(int s=0; s<1; s++) {
int u = get_idx(s, j); // now
int v = get_idx(s+t-1, k); // prev
mat[u][v] += mint(1);
}
}
}
for(int i=0; i<N; i++) {
for(int t=1; t<=4; t++) {
int u = get_idx(t, i); // now
int v = get_idx(t-1, i); // prev
mat[u][v] += mint(1);
}
}
mat = pow(mat, T);
Matrix<mint> vec(M, 1);
vec[C][0] = mint(1);
vec = mat * vec;
cout << vec[0][0] << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
const int mod = 1000000007;
struct Mod {
public:
int num;
Mod() : Mod(0) { ; }
Mod(long long int n) : num((n % mod + mod) % mod) {
static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
}
Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
operator int() { return num; }
};
Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
if (n == 0) return Mod(1);
Mod res = (a * a) ^ (n / 2);
if (n % 2) res = res * a;
return res;
}
Mod mod_pow(const Mod a, const long long int n) {
if (n == 0) return Mod(1);
Mod res = mod_pow((a * a), (n / 2));
if (n % 2) res = res * a;
return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
assert(b.num != 0);
return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
return a = a / b;
}
#define MAX_MOD_N 1024000
Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init(const int amax = MAX_MOD_N) {
fact[0] = Mod(1); factinv[0] = 1;
for (int i = 0; i < amax - 1; ++i) {
fact[i + 1] = fact[i] * Mod(i + 1);
factinv[i + 1] = factinv[i] / Mod(i + 1);
}
}
Mod comb(const int a, const int b) {
return fact[a] * factinv[b] * factinv[a - b];
}
template<typename T>
vector<vector<T>> keisann(const vector<vector<T>>l, const vector<vector<T>>r) {
vector<vector<T>>ans(l.size(), vector<T>(r[0].size()));
assert(l[0].size() == r.size());
for (unsigned int h = 0; h < l.size(); ++h) {
for (unsigned int i = 0; i < r.size(); ++i) {
for (unsigned int w = 0; w < r[0].size(); ++w) {
ans[h][w] += l[h][i] * r[i][w];
}
}
}
return ans;
}
template<typename T>
vector<vector<T>>powgyou(vector<vector<T>>a, const long long int n) {
assert(a.size() == a[0].size());
if (!n) {
vector<vector<T>>e(a.size(), vector<T>(a[0].size()));
for (unsigned int i = 0; i < a.size(); ++i) {
e[i][i] = 1;
}
return e;
}
if (n == 1)return a;
else {
vector<vector<T>>ans(a.size(), vector<T>(a[0].size(), 0));
ans = powgyou(a, n / 2);
ans = keisann(ans, ans);
if (n % 2) {
ans = keisann(ans, a);
}
return ans;
}
}
int main(){
int N, K, C, T; cin >> N >> K >> C >> T;
C--;
vector<vector<Mod>>from(5*N, vector<Mod>(1));
from[5*C][0]+=1;
vector<vector<Mod>>gyou(5*N, vector<Mod>(5*N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < 4; ++j) {
gyou[5 * i + j][5 *i + j + 1] = 1;
}
}
for (int i = 0; i < K; ++i) {
int a, b, t; cin >> a >> b >> t;
a--;
for (int j = 0; j < N; ++j) {
const int afrom = 5 * j;
int to;
if (j<a) {
to = 5 * (j + b) + t - 1;
}
else if ((j>a + b - 1)) {
to = afrom+t-1;
}
else {
to = 5 * (j - a) + t-1;
}
gyou[to][afrom] += 1;
}
}
auto kake = powgyou(gyou,T);
auto ans = keisann(kake, from);
cout << ans[0][0] << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}
typedef vector<vint>mat;
const int mod=1000000007;
mat mul(mat A,mat B){
mat C(A.size(),vint(B[0].size()));
rep(i,C.size()){
rep(j,C[0].size()){
rep(k,A[0].size()){
(C[i][j]+=A[i][k]*B[k][j])%=mod;
}
}
}
return C;
}
mat mpow(mat A,int m){
mat B(A.size(),vint(A.size()));
rep(i,A.size())B[i][i]=1;
for(;m;m>>=1,A=mul(A,A))if(m&1)B=mul(A,B);
return B;
}
int N,K,C,T;
int a[114514],b[114514],t[114514];
signed main(){
cin>>N>>K>>C>>T;C--;
rep(i,K)cin>>a[i]>>b[i]>>t[i],a[i]--;
mat X(N*5,vint(1)),A(N*5,vint(N*5));
X[C][0]=1;
rep(i,N*4)A[N+i][i]=1;
rep(k,K){
int bb=(t[k]-1)*N;
rep(i,N){
if(i<a[k])A[i+b[k]][bb+i]++;
else if(i<a[k]+b[k])A[i-a[k]][bb+i]++;
else A[i][bb+i]++;
}
}
X=mul(mpow(A,T),X);
cout<<X[0][0]<<endl;
return 0;
} |
#include <valarray>
#include <vector>
#include <cstdio>
using namespace std;
typedef valarray<__int128_t>V;
int n,m=1000000007;
V z;
V &Me(const V &_x,const V &_y){
int i=0,j;
for(;i<n;i++)for(j=0;j<n;j++)z[i*n+j]=(_x[slice(i*n,n,1)]*_y[slice(j,n,n)]).sum()%m;
return z;
}
V &Mx(const V &_x){
int i=0,j;
for(;i<n;i++)for(j=0;j<n;j++)z[i*n+j]=(_x[slice(i*n,n,1)]*_x[slice(j,n,n)]).sum()%m;
return z;
}
int main(){
int N,K,C,T;
scanf("%d%d%d%d",&N,&K,&C,&T);C--;
vector<int>a(K),b(K),t(K);
for(int i=0;i<K;i++)scanf("%d%d%d",&a[i],&b[i],&t[i]),a[i]--;
n=N*5;
V x(n*n);
V e(n*n);
z.resize(n*n);
for(int i=0;i<n;i++)e[i*n+i]=1;
for(int i=0;i<K;i++)for(int j=0;j<N;j++){
int nj=j<a[i]?j+b[i]:j<a[i]+b[i]?j-a[i]:j;
x[j*5*n+nj*5+t[i]-1]++;
}
for(int i=0;i<N;i++)for(int j=1;j<5;j++)x[(i*5+j)*n+i*5+j-1]++;
for(;T;T>>=1){
if(T&1)e=Me(e,x);
x=Mx(x);
}
printf("%lld\n",(long long)e[C*5*n]);
} |
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll MOD = 1000000007;
vector<vector<ll> > matmul(vector<vector<ll> >& A, vector<vector<ll> >& B) {
int n = A.size();
vector<vector<ll> > C(n, vector<ll>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
return C;
}
vector<vector<ll> > matpow(vector<vector<ll> > A, ll x) {
int n = A.size();
vector<vector<ll> > B(n, vector<ll>(n));
for (int i = 0; i < n; i++) B[i][i] = 1;
while (x > 0) {
if (x % 2) B = matmul(B, A);
x /= 2;
A = matmul(A, A);
}
return B;
}
int main() {
int N, K, C, T;
cin >> N >> K >> C >> T;
C--;
vector<vector<ll> > A(N * 5, vector<ll>(N * 5));
for (int i = 0; i < N * 4; i++)
A[i + N][i] = 1;
for (int k = 0; k < K; k++) {
int a, b, t; cin >> a >> b >> t;
a--;
for (int x = 0; x < N; x++) {
int y;
if (x < b) y = x + a;
else if (x < a + b) y = x - b;
else y = x;
A[y][(t - 1) * N + x]++;
}
}
A = matpow(A, T);
cout << A[C][0] << endl;
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
const int mod = 1e9 + 7;
typedef vector<vi> M;
inline M operator*(const M & a, const M &b){
M c(a.size(), vi(b[0].size()));
rep(i, c.size()) rep(j, c[0].size()){
ll s = 0;
rep(k, a[0].size()) s += (ll)a[i][k] * b[k][j] % mod;
c[i][j] = s % mod;
}
return c;
}
inline M pow(M a, ll m){
M res(a.size(), vi(a.size()));
rep(i, a.size()) res[i][i] = 1;
for(; m; m /= 2){
if(m & 1) res = res * a;
a = a * a;
}
return res;
}
int n, K, c, T, a[1000], b[1000], t[1000];
int dp[10][40];
int main(){
cin >> n >> K >> c >> T;
rep(i, K) cin >> a[i] >> b[i] >> t[i], a[i]--;
M A(5 * n, vi(5 * n));
rep(s, 5 * n){
memset(dp, 0, sizeof(dp));
dp[s / n][s % n] = 1;
rep(i, 5) rep(j, n) if(dp[i][j]) rep(k, K){
int nj = j >= a[k] + b[k] ? j : j >= a[k] ? j - a[k] : j + b[k];
(dp[i + t[k]][nj] += dp[i][j]) %= mod;
}
for(int i = 5; i < 10; i++) rep(j, n) (A[(i - 5) * n + j][s] += dp[i][j]) %= mod;
}
A = pow(A, T / 5);
M x(5 * n, vi(1));
x[c - 1][0] = 1;
x = A * x;
memset(dp, 0, sizeof(dp));
rep(i, 5 * n) dp[i / n][i % n] = x[i][0];
rep(i, 5) rep(j, n) if(dp[i][j]) rep(k, K){
int nj = j >= a[k] + b[k] ? j : j >= a[k] ? j - a[k] : j + b[k];
(dp[i + t[k]][nj] += dp[i][j]) %= mod;
}
cout << dp[T % 5][0] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
typedef vector< vector<ll> > Matrix;
int N, K, C, T;
vector<int> shuffle(vector<int>& v, int a, int b) {
vector<int> res;
for (int i = 0; i < b; ++i) {
res.push_back(v[a+i]);
}
for (int i = 0; i < a; ++i) {
res.push_back(i);
}
for (int i = a+b; i < N; ++i) {
res.push_back(i);
}
return res;
}
vector<int> make_shuffle(int a, int b) {
vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i;
}
v = shuffle(v, a, b);
vector<int> res(N);
for (int i = 0; i < N; ++i) {
res[v[i]] = i;
}
return res;
}
vector< vector<ll> > make_E() {
vector< vector<ll> > res(N*5, vector<ll>(N*5, 0));
for (int i = 0; i < N*5; ++i) {
res[i][i] = 1;
}
return res;
}
vector< vector<ll> > mul(Matrix A, Matrix B) {
vector< vector<ll> > res(N*5, vector<ll>(N*5, 0));
for (int i = 0; i < N*5; ++i) {
for (int j = 0; j < N*5; ++j) {
for (int k = 0; k < N*5; ++k) {
res[i][j] = (res[i][j] + A[i][k] * B[k][j] % mod);
if (res[i][j] >= mod) res[i][j] -= mod;
}
}
}
return res;
}
vector< vector<ll> > power(Matrix A, int n) {
Matrix res = make_E();
for (int i = 1; i <= n; i <<= 1) {
if (n & i) {
res = mul(res, A);
}
A = mul(A, A);
}
return res;
}
int main() {
cin >> N >> K >> C >> T;
vector< vector< vector<ll> > > pat(5, vector< vector<ll> >(N, vector<ll>(N, 0)));
for (int i = 0; i < K; ++i) {
int a, b, t; cin >> a >> b >> t; --a, --t;
vector<int> to = make_shuffle(a, b);
for (int j = 0; j < to.size(); ++j) {
++pat[t][j][to[j]];
}
}
vector< vector<ll> > G(N*5, vector<ll>(N*5, 0));
for (int t = 0; t < 5; ++t) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
int from = i*5;
int to = j*5+t;
G[from][to] += pat[t][i][j];
}
}
}
for (int i = 0; i < N; ++i) {
for (int t = 1; t < 5; ++t) {
G[i*5+t][i*5+t-1] = 1;
}
}
vector< vector<ll> > g = power(G, T);
/* for (int i = 0; i < N*5; ++i) {
for (int j = 0; j < N*5; ++j) {
cout << g[i][j] << " ";
}
cout << endl;
}
*/ cout << g[(C-1)*5][0*5] << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD
const int mod = 1000000007;
ll R,R2,modash;
int logR;
ll MR(ll x){
ll ret = (x + ((x*modash)&(R-1ll))*mod)>>(logR);
return (ret>=mod ? ret-mod : ret);
}
ll get_mod(ll x){return MR(MR(x)*R2);}
void init(){
R=1ll;logR=0;modash=0;
while(R<mod){R<<=1;logR++;}
R2=R*R%mod;
int t=0,r=R,i=1;
while(r>1){
if((t&1)==0){t+=mod;modash+=i;}
t>>=1;r>>=1;i<<=1;
}
}
vector<vl> mul(vector<vl> a, vector<vl> b){
int n = a.size();
vector<vl> ret(n,vl(n,0));
REP(k,n)REP(i,n)REP(j,n){
ret[i][j] += get_mod(a[i][k]*b[k][j]);
if(ret[i][j]>=mod) ret[i][j] -= mod;
}
return ret;
}
int main(){
int n,k,c,t;
scanf("%d%d%d%d",&n,&k,&c,&t);
// ????§???????
// 0?§???????1?§????...4?§????????£??????§??\?????????????????°t?????§?????¨?????????
vector<vl> mat(5*n,vl(5*n,0));
// ??????????§?
REP(i,4){
REP(j,n) mat[i*n+j][(i+1)*n+j] += 1;
}
// ?????????????§?
REP(_,k){
int a,b,tm;
scanf("%d%d%d",&a,&b,&tm);
// --a; --b;
--tm;
REP(i,n){
int from;
if(i<b) from = i+a-1;
else if(i<b+a-1) from = i-b;
else from = i;
int to = i;
mat[tm*n+to][from] += 1;
}
}
// REP(po,5*n){
// DEBUG_VEC(mat[po]);
// }
// cout<<endl;
init();
// ?´????
vector<vl> ans(5*n,vl(5*n,0));
REP(i,5*n) ans[i][i]=1;
while(t){
if(t&1) ans = mul(ans,mat);
mat = mul(mat,mat);
t>>=1;
}
// DEBUG
// REP(po,5*n){
// DEBUG_VEC(ans[po]);
// }
cout<<ans[0][c-1]<<endl;
return 0;
} |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(i,n,N) for(ll i=n;i<=N;++i)
#define rap(i,n,N) for(ll i=n;i>=N;--i)
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define ff fi.fi
#define fs fi.se
#define sf se.fi
#define ss se.se
#define lc (id<<1)
#define rc ((id<<1)|1)
#define db(x) cout << ">>>>>> " << #x << " -> " << x << endl;
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define piii pair<int,pii>
#define piiii pair<pii,pii>
#define psi pair<string,int>
#define endl "\n"
const int MAX = 1e5+5;
const ll MAX2 = 11;
const ll MOD = 1000000007;
const ll INF = 2e18;
const int dr[]={1,0,-1,0,1,1,-1,-1,0};
const int dc[]={0,1,0,-1,1,-1,1,-1,0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 450;
ll n,k,inv,x[MAX],y[MAX],bit[MAX],res,id;
inline void upd(int i,int z){
for(; i<=n; i+=(i&-i))bit[i]+=z;
}
int ret;
inline int que(int i){
i--;
ret = 0;
for(; i> 0; i-=(i&-i))ret+=bit[i];
return ret;
}
priority_queue<int, vector<int> ,greater<int> > pq;
priority_queue<pii, vector<pii> ,greater<pii> > tmp;
vector<int> ans;
int main(){
// cout<<fixed<<setprecision(15);
// freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k;
rep(i,1,n)cin>>y[i], x[y[i]] = i;
rap(i,n,1)inv+=que(x[i]), upd(x[i],1);
if(inv<=k)rep(i,1,n)cout<<y[i]<<endl;
else {
k = inv - k;
rep(i,1,n)pq.push(i);
rep(i,1,n){
while(!tmp.empty()&&que(x[tmp.top().se])<=k)pq.push(tmp.top().se), tmp.pop();
while(!pq.empty()){
id = pq.top(); pq.pop();
res = que(x[id]);
if(res>k)tmp.push({x[id],id});
else {
ans.pb(id);
k-=res;
upd(x[id],-1);
break;
}
}
}
for(auto i:ans)cout<<i<<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<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
using namespace std;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
#define getchar getchar_unlocked
#define putchar putchar_unlocked
int in() {
int n, c;
while ((c = getchar()) < '0') if (c == EOF) return -1;
n = c - '0';
while ((c = getchar()) >= '0') n = n * 10 + c - '0';
return n;
}
void out(int n) {
int res[11], i = 0;
do { res[i++] = n % 10, n /= 10; } while (n);
while (i) putchar(res[--i] + '0');
putchar('\n');
}
int n,K;
int x[MAX_N], g[MAX_N];
template<typename V> class BIT {
private:
int n; vector<V> bit;
public:
void add(int i,V x){ i++; while(i < n) bit[i] += x, i += i & -i;}
V sum(int i){ i++; V s = 0; while(i>0) s += bit[i], i -= i & -i; return s;}
BIT(){} BIT(int sz){ n = sz + 1, bit.resize(n,0);} //初期値がすべて0の場合
BIT(vector<V> v){ n = (int)v.size()+1; bit.resize(n); rep(i,n-1) add(i,v[i]);}
void print(){ rep(i,n-1)cout<<sum(i)-sum(i-1)<< " ";cout<<endl;}
void print_sum(){ rep(i,n)cout<<sum(i-1)<<" ";cout<<endl;} //-1スタート
};
ll inv_count()
{
BIT<int> bt(n);
ll ans = 0;
rep(i,n){
ans += i-bt.sum(x[i]);
bt.add(x[i],1);
}
return ans;
}
template<typename T> class segtree {
private:
int n,sz;
vector<T> node;
public:
segtree(int N) : sz(N){
n = 1;
while(n < sz){
n *= 2;
}
node.assign(2*n,numeric_limits<T>::max());
rep(i,sz){
node[i+n] = x[i];
}
for(int i=n-1; i>=1; i--){
node[i] = min(node[2*i],node[2*i+1]);
}
}
void update(int k,T a)
{
node[k+=n] = a;
while(k>>=1){
node[k] = min(node[2*k],node[2*k+1]);
}
}
T query(int a,int b)
{
T res1 = numeric_limits<T>::max();
T res2 = numeric_limits<T>::max();
a += n, b += n;
while(a != b){
if(a % 2) cmn(res1,node[a++]);
if(b % 2) cmn(res2,node[--b]);
a >>= 1, b>>= 1;
}
return min(res1, res2);
}
void print()
{
rep(i,sz){
cout << query(i,i+1) << " ";
}
cout << endl;
}
};
template<typename T> class segtree_ {
private:
int n,sz;
vector<T> node;
public:
segtree_(vector<T>& v){
sz = (int)v.size();
n = 1;
while(n < sz){
n *= 2;
}
node.resize(2*n-1,0);
rep(i,sz){
node[i+n-1] = v[i];
}
for(int i=n-2; i>=0; i--){
node[i] = node[i*2+1] + node[i*2+2];
}
}
void update(int k)
{
k += n-1;
node[k]--;
while(k>0){
k = (k-1)/2;
node[k] = node[2*k+1] + node[2*k+2];
}
}
T query(int a,int b,int k=0,int l=0,int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l){
return 0;
}
if(a <= l && r <= b){
return node[k];
}else{
T vl = query(a,b,2*k+1,l,(l+r)/2);
T vr = query(a,b,2*k+2,(l+r)/2,r);
return vl + vr;
}
}
//cri以上の値を持つ最小のインデックスを返す
int query_(const int cri,int k=0,int l=0,int r=-1)
{
if(r < 0) r = n;
if(r - l == 1){
return k - n + 1;
}
if(node[2*k+1] > cri){
return query_(cri,2*k+1,l,(l+r)/2);
}else{
return query_(cri-node[2*k+1],2*k+2,(l+r)/2,r);
}
}
void print(){rep(i,sz)cout<<query(i,i+1)<<" ";cout << endl;}
};
int main()
{
n = in(), K = in();
rep(i,n){
x[i] = in();
--x[i];
g[x[i]] = i;
}
ll res = inv_count();
ll rem = res - K;
vi hoge(n,1);
segtree<int> sg(n);
segtree_<int> bt(hoge);
if(rem <= 0){
rep(i,n){
out(x[i]+1);
}
return 0;
}
// show(rem);
vector<bool> used(n, false);
rep(i,n){
if(rem <= 0){
rep(j,n){
if(!used[x[j]]){
out(x[j]+1);
}
}
break;
}else{
int id = n-1;
if(rem < n-i){
id = bt.query_(rem);
}
// show(id);
int num = sg.query(0,id+1);
// show(num);
used[num] = true;
out(num+1);
// show(g[num]);
rem -= bt.query(0, g[num]);
// show(rem);
sg.update(g[num],INF);
bt.update(g[num]);
}
}
return 0;
}
|
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#define int long long
#define inf 1000000007
#define pa pair<int,int>
#define pad pair<double,double>
#define ll long long
#define pal pair<double,double>
#define ppap pair<pa,int>
#define PI 3.14159265358979323846
#define paa pair<pa,pa>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
// #define double long double
int dx[4]={0,-1,0,1};
int dy[4]={1,0,-1,0};
using namespace std;
class pa3{
public:
int x,y,z;
pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
bool operator < (const pa3 &p) const{
if(x!=p.x) return x<p.x;
if(y!=p.y) return y<p.y;
return z<p.z;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator > (const pa3 &p) const{
if(x!=p.x) return x>p.x;
if(y!=p.y) return y>p.y;
return z>p.z;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const pa3 &p) const{
return x==p.x && y==p.y && z==p.z;
}
bool operator != (const pa3 &p) const{
return !( x==p.x && y==p.y && z==p.z);
}
};
class pa4{
public:
int x;
int y,z,w;
pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
bool operator < (const pa4 &p) const{
if(x!=p.x) return x<p.x;
if(y!=p.y) return y<p.y;
if(z!=p.z)return z<p.z;
return w<p.w;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator > (const pa4 &p) const{
if(x!=p.x) return x>p.x;
if(y!=p.y) return y>p.y;
if(z!=p.z)return z>p.z;
return w>p.w;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const pa4 &p) const{
return x==p.x && y==p.y && z==p.z &&w==p.w;
}
bool operator != (const pa4 &p) const{
return !(x==p.x && y==p.y && z==p.z &&w==p.w);
}
};
class pa2{
public:
int x,y;
pa2(int x=0,int y=0):x(x),y(y) {}
pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
bool operator < (const pa2 &p) const{
return x!= p.x ? x<p.x: y<p.y;
}
bool operator > (const pa2 &p) const{
return x != p.x ? x>p.x: y>p.y;
}
bool operator == (const pa2 &p) const{
return abs(x-p.x)==0 && abs(y-p.y)==0;
}
bool operator != (const pa2 &p) const{
return !(abs(x-p.x)==0 && abs(y-p.y)==0);
}
};
#define ppa pair<int,pas>
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(x*a,y*a);}
Point operator / (double a) {return Point(x/a,y/a);}
double absv() {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;
#define pl pair<int,pas>
struct Segment{
Point p1,p2;
};
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;
}
bool parareru(Point a,Point b,Point c,Point d){
// if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
return abs(cross(a-b,d-c))<EPS;
}
double distance_ls_p(Point a, Point b, Point c) {
if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
return abs(cross(b-a, c-a)) / (b-a).absv();
}
bool is_intersected_ls(Segment a,Segment b) {
if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return 1;
if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&¶reru((a.p2),(a.p1),(a.p1),(b.p1))){
// cout<<"sss"<<endl;
if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
return false;
}
else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
}
double segment_dis(Segment a,Segment b){
if(is_intersected_ls(a,b))return 0;
double r=distance_ls_p(a.p1, a.p2, b.p1);
r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
return r;
}
Point intersection_ls(Segment a, Segment b) {
Point ba = b.p2-b.p1;
double d1 = abs(cross(ba, a.p1-b.p1));
double d2 = abs(cross(ba, a.p2-b.p1));
double t = d1 / (d1 + d2);
return a.p1 + (a.p2-a.p1) * t;
}
string itos( int i ) {
ostringstream s ;
s << i ;
return s.str() ;
}
int gcd(int v,int b){
if(v==0) return b;
if(v>b) return gcd(b,v);
if(v==b) return b;
if(b%v==0) return v;
return gcd(v,b%v);
}
double distans(double x1,double y1,double x2,double y2){
double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return sqrt(rr);
}
/*
double bekid(double w,int r){
if(r==0) return 1.0;
if(r==1) return w;
if(r%2) return bekid(w,r-1)*w;
double f=bekid(w,r/2);
return f*f;
}*/
int mod;
int beki(int wa,int rr,int warukazu){
if(rr==0) return 1%warukazu;
if(rr==1) return wa%warukazu;
if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;
int zx=beki(wa,rr/2,warukazu);
return (zx*zx)%warukazu;
}
/*
int pr[401000];
int inv[401000];
int comb(int nn,int rr){
int r=pr[nn]*inv[rr];
r%=mod;
r*=inv[nn-rr];
r%=mod;
return r;
}
void gya(int ert){
pr[0]=1;
for(int i=1;i<ert;i++){
pr[i]=(pr[i-1]*i)%mod;
}
for(int i=0;i<ert;i++) inv[i]=beki(pr[i],mod-2,mod);
}
*/
// cin.tie(0);
// ios::sync_with_stdio(false);
//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;
//sort(ve.begin(),ve.end(),greater<int>());
struct segsum{
// 1
// 2 3
// 4 5 6 7
private:
public:
// 1<<10 = 1024
// 1<<11 = 2048
// 1<<12 = 4096
// 1<<13 = 8192
// 1<<14 = 16384
// 1<<15 = 32768
// 1<<16 = 65536
// 1<<17 = 131072
// 1<<18 = 262144
int cor=(1<<17);
vector<int> vec;
vector<int> lazy;
void shoki1(){
vec.resize(2*cor+3, 0);
}
void shoki2(){
for(int i=cor-1;i>0;i--) vec[i]=vec[2*i]+vec[2*i+1];
}
void clear(){
for(int i=0;i<(int)vec.size();i++)vec[i]=0;
}
void updadd(int x,int w){
//x 項目に w加算
x+=cor;
vec[x]+=w;
while(1){
x/=2;
if(x==0) break;
vec[x]=vec[2*x]+vec[2*x+1];
}
}
void updchan(int x,int w){
//x項目をwに変更
x+=cor;
vec[x]=w;
while(1){
x/=2;
if(x==0) break;
vec[x]=vec[2*x]+vec[2*x+1];
}
}
// [a,b)
// k-th node
// k no kukanha [l,r)
int sum(int a,int b,int k=1,int l=0,int r=-10){
if(r<0)r=cor;
if(a>=b) return 0;
// cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
if(a<=l && r<=b){
return vec[k];
}
if(r<=a || b<=l){
return 0;
}
int v1=sum(a,b,k*2,l,(l+r)/2),v2=sum(a,b,k*2+1,(l+r)/2,r);
return v1+v2;
}
};
segsum ss;
struct Segmin{
// 1
// 2 3
// 4 5 6 7
private:
public:
// (1<<15)=32768
// 1<<16 = 65536
// 1<<17 = 131072
// 1<<18 = 262144
int cor=(1<<17);
vector<pa> vec;
void shoki1(){
vec.resize(2*cor+3, mp(inf,0));
for(int i=cor;i<2*cor;i++)vec[i].second=i-cor;
}
void shoki2(){
for(int i=cor-1;i>0;i--) {
if(vec[2*i].first<=vec[2*i+1].first) vec[i]=vec[2*i];
else vec[i]=vec[2*i+1];
}
}
void updadd(int x,int w){
//x 項目に w加算
x+=cor;
vec[x].first+=w;
while(1){
x/=2;
if(x==0) break;
if(vec[2*x].first<=vec[2*x+1].first) vec[x]=vec[2*x];
else vec[x]=vec[2*x+1];
}
}
void updchan(int x,int w){
//x項目をwに変更
x+=cor;
vec[x].first=w;
while(1){
x/=2;
if(x==0) break;
if(vec[2*x].first<=vec[2*x+1].first) vec[x]=vec[2*x];
else vec[x]=vec[2*x+1];
}
}
// [a,b)
// k-th node
// k no kukanha [l,r)
pa segmin(int a,int b,int k=1,int l=0,int r=-10){
if(r<0)r=cor;
// cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
if(a<=l && r<=b){
return vec[k];
}
if(r<=a || b<=l){
return mp((1ll<<31)-1,-1);
}
pa v1=segmin(a,b,k*2,l,(l+r)/2),v2=segmin(a,b,k*2+1,(l+r)/2,r);
if(v1.first<=v2.first)return v1;
else return v2;
}
};
Segmin sm;
int p[100020];
int pinv[100020];
vector<int> ans;
int n;
void outt(){
for(int i=1;i<=n;i++)if(ss.vec[ss.cor+i])ans.pb(sm.vec[sm.cor+i].first);
for(int i=0;i<n;i++)cout<<ans[i]<<endl;
exit(0);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int k;
cin>>n>>k;
ss.shoki1();
ss.shoki2();
sm.shoki1();
for(int i=1;i<=n;i++){
cin>>p[i];
pinv[p[i]]=i;
sm.vec[sm.cor+i].first=p[i];
}
sm.shoki2();
int ten=0;
for(int i=1;i<=n;i++){
ss.updadd(pinv[i],1);
ten+=ss.sum(pinv[i]+1,n+3);
}
k=min(k,n*(n-1)/2);
int nokori=ten-k;
if(nokori<0) nokori=0;
//cout<<ten<<" "<<nokori<<endl;
for(int i=1;i<=n+1;i++){
// cout<<"nokori "<<nokori<<endl;
if(nokori==0){
outt();
}
pa z=sm.segmin(0,n+1);
int kosuu=ss.sum(0,z.second);
if(kosuu<=nokori){
nokori-=kosuu;
ans.pb(z.first);
ss.updchan(z.second,0);
sm.updchan(z.second,inf);
continue;
}
int cn=0;
while(1){
if(nokori==0){
outt();
}
int si=0,ue=n+1,me;
while(ue-si>1){
me=(ue+si)/2;
if(ss.sum(0,me)>=nokori+1)ue=me;
else si=me;
}
cn++;
//if(cn>10) exit(0);
z=sm.segmin(0,ue);
// cout<<ss.sum(0,si)<<endl;
// cout<<ss.sum(0,ue)<<endl;
// cout<<z.first<<" "<<z.second<<endl;
kosuu=ss.sum(0,z.second);
nokori-=kosuu;
ans.pb(z.first);
ss.updchan(z.second,0);
sm.updchan(z.second,inf);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
class fenwick_tree {
const int n;
vector<T> data;
public:
fenwick_tree(int n_) : n(n_), data(n) {}
T find(int p) const {
T res = 0;
while (p >= 0) {
res += data[p];
p = (p & (p + 1)) - 1;
}
return res;
}
T find(int l, int r) const {
return find(r) - find(l - 1);
}
void add(int p, T val) {
while (p < n) {
data[p] += val;
p |= p + 1;
}
}
};
struct RMQ {
using type = int;
static type id() { return INT_MAX; }
static type op(const type& l, const type & r) { return min(l, r); }
};
template <typename M>
class segment_tree {
using T = typename M::type;
const int n;
vector<T> data;
int expand(int x) {
int res;
for (res = 1; res < x; res <<= 1);
return res;
}
public:
segment_tree(int n_) : n(expand(n_)), data(n * 2, M::id()) {}
segment_tree(int n_, T val) : n(expand(n_)), data(n * 2, val) {}
void init(const vector<T>& data_) {
for (int i = 0; i < (int)data_.size(); i++)
data[i + n] = data_[i];
for (int i = n - 1; i >= 0; i--)
data[i] = M::op(data[i * 2], data[i * 2 + 1]);
}
void update(int p, T val) {
data[p += n] = val;
while (p >>= 1) data[p] = M::op(data[p * 2], data[p * 2 + 1]);
}
void add(int p, T val) {
data[p += n] += val;
while (p >>= 1) data[p] = M::op(data[p * 2], data[p * 2 + 1]);
}
T find(int l, int r) {
l += n; r += n + 1;
T res1 = M::id(), res2 = M::id();
while (l < r) {
if (l & 1) res1 = M::op(res1, data[l++]);
if (r & 1) res2 = M::op(data[--r], res2);
l >>= 1; r >>= 1;
}
return M::op(res1, res2);
}
};
int main()
{
int N, K;
cin >> N >> K;
vector<int> x(N);
for (int i = 0; i < N; i++) {
cin >> x[i];
}
auto xs = x;
sort(xs.begin(), xs.end());
for (int i = 0; i < N; i++) {
x[i] = lower_bound(xs.begin(), xs.end(), x[i]) - xs.begin();
}
ll rev = 0;
fenwick_tree<int> ft(N);
for (int i = 0; i < N; i++) {
rev += i - ft.find(x[i]);
ft.add(x[i], 1);
}
if (rev <= K) {
for (int i = 0; i < N; i++) {
printf("%d\n", xs[x[i]]);
}
return 0;
}
rev -= K;
vector<int> pos(N);
for (int i = 0; i < N; i++) {
pos[x[i]] = i;
}
vector<int> top;
segment_tree<RMQ> st(N);
for (int i = 0; i < N; i++) {
st.update(pos[i], i);
}
while (rev > 0) {
int t;
{
int lb = -1, ub = N - 1;
while (ub - lb > 1) {
int c = (lb + ub) >> 1;
if (ft.find(c) <= rev) {
lb = c;
}
else {
ub = c;
}
}
t = ub;
}
int id = st.find(0, t);
assert(0 <= id && id < N);
ft.add(pos[id], -1);
rev -= ft.find(pos[id]);
st.update(pos[id], INT_MAX);
top.push_back(id);
}
vector<int> res(N);
for (int i = 0, j = 0; i < N; i++) {
if (i < (int)top.size()) {
res[i] = top[i];
}
else {
while (ft.find(j, j) == 0) ++j;
res[i] = x[j++];
}
}
for (int i = 0; i < N; i++) {
printf("%d\n", xs[res[i]]);
}
return 0;
}
|
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 100005
struct Info{
Info(){
value = 0;
index = 0;
}
Info(int arg_value,int arg_index){
value = arg_value;
index = arg_index;
}
int value,index;
};
int N,BIT_N;
Info table[8*NUM];
int input[NUM];
int BIT[NUM];
bool used[NUM];
void add(int loc,int value){
BIT[loc] += value;
loc += loc & -loc;
while(loc <= BIT_N){
BIT[loc] += value;
loc += loc & -loc;
}
}
int getSum(int loc){
int sum = BIT[loc];
loc -= loc & -loc;
while(loc > 0){
sum += BIT[loc];
loc -= loc & -loc;
}
return sum;
}
int calc(int left,int right){
return getSum(right)-getSum(left-1);
}
void init(int first_N){
while(N < first_N)N *= 2;
}
void update(int loc,int value){
loc += N-1;
table[loc].value = value; //区間の最小値
table[loc].index = loc-(N-1); //最小値を持つインデックス
if(N == 1)return;
int parent = (loc-1)/2;
while(true){
if(table[2*parent+1].value <= table[2*parent+2].value){
table[parent].value = table[2*parent+1].value;
table[parent].index = table[2*parent+1].index;
}else{
table[parent].value = table[2*parent+2].value;
table[parent].index = table[2*parent+2].index;
}
if(parent == 0)break;
else{
parent = (parent-1)/2;
}
}
}
Info query(int search_left,int search_right,int node_id,int node_left,int node_right){
//今回のノードが検索区間をカバーしていなければ、結果に関係ない値を返す
if(search_right < node_left || search_left > node_right)return Info(NUM,-1);
//今回のノードの区間が、検索区間の部分区間である場合、今回のノードの値を返す
if(search_left <= node_left && search_right >= node_right){
return table[node_id];
}
//今回のノードの区間に、一部検索区間と重なっている区間がある場合→再帰的に子どもに尋ねる
Info left_info = query(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
Info right_info = query(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);
if(left_info.value <= right_info.value){
return left_info;
}else{
return right_info;
}
}
int main(){
int first_N;
ll K;
scanf("%d %lld",&first_N,&K);
N = 1;
BIT_N = first_N;
init(first_N);
for(int i = 0; i <= BIT_N; i++)BIT[i] = 0;
//0~N-2が上位構造のアドレス、N-1~2*N-2が最小要素のアドレス
for(int i = 0; i <= 2*N-2; i++)table[i].value = BIG_NUM;
ll rev_num = 0;
for(int loop = 0; loop < first_N; loop++){
scanf("%d",&input[loop]);
rev_num += loop-getSum(input[loop]);
add(input[loop],1);
update(loop,input[loop]);
}
if(rev_num <= K){
for(int i = 0; i < first_N; i++){
printf("%d\n",input[i]);
}
return 0;
}
for(int i = 0; i <= BIT_N; i++)BIT[i] = 0; //BIT木は1オリジン
for(int i = 1; i <= first_N; i++){
add(i,1);
used[i] = false;
}
int L,R,mid,range_right;
rev_num -= K;
Info info;
vector<int> ans;
while(rev_num > 0){
//2分探索で、要素数がrev_num+1個となる右端を求める
L = 1,R = first_N,mid = (L+R)/2;
range_right = first_N;
while(L <= R){
if(getSum(mid) >= rev_num+1){
range_right = mid;
R = mid-1;
}else{
L = mid+1;
}
mid = (L+R)/2;
}
info = query(0,range_right-1,0,0,N-1); //区間の最小値
ans.push_back(info.value);
used[info.value] = true;
add(info.index+1,-1);
rev_num -= getSum(info.index);
update(info.index,BIG_NUM);
}
for(int i = 0; i < ans.size(); i++){
printf("%d\n",ans[i]);
}
for(int i = 0; i < first_N; i++){
if(used[input[i]])continue;
printf("%d\n",input[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template< typename T >
struct BinaryIndexedTree {
BinaryIndexedTree(size_t n) : n(n), v(n + 1) {}
size_t n;
vector< T > v;
T sum(int k) {
T ret = 0;
for(; k; k -= k & -k) ret += v[k];
return ret;
}
void add(int k, T x) {
for(; k <= n; k += k & -k) v[k] += x;
}
};
int main() {
int n, k;
cin >> n >> k;
vector< int > x(n + 1), idx(n + 1, -1);
for(int i = 1; i <= n; ++i) {
cin >> x[i];
idx[x[i]] = i;
}
BinaryIndexedTree< int > bit(n);
vector< int > inv(n + 1);
int64_t sum = 0;
for(int i = 1; i <= n; ++i) {
inv[x[i]] = i - bit.sum(x[i]) - 1;
sum += inv[x[i]];
bit.add(x[i], 1);
}
vector< int > ans(1), used(n + 1, 0);
for(int i = 1; i <= n; ++i) {
if(sum - k < inv[i]) break;
ans.push_back(i);
sum -= inv[i];
used[i] = true;
}
int mini = 101010, last = -1;
set< pair< int, int > > unused;
for(int i = 1; i <= n; ++i) {
if(!used[x[i]]) {
int sz = (int) unused.size();
unused.emplace(sz, x[i]);
}
}
int64_t rest = sum - k;
while(unused.size()) {
auto it = unused.begin();
int small = it->second;
for(int j = 0; j <= rest; j++) {
if(it == unused.end()) break;
small = min(small, it->second);
++it;
}
it = unused.begin();
for(int j = 0;; j++) {
if(small == it->second) {
rest -= j;
ans.push_back(it->second);
unused.erase(it);
break;
}
++it;
}
}
for(int i = 1; i <= n; ++i) {
cout << ans[i] << endl;
}
}
|
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
template<typename T>
struct BIT{
Int n;
vector<T> bit;
BIT():n(-1){}
BIT(Int n_, T d):n(n_), bit(n_ + 1, d){}
T sum(Int i){
T s = bit[0];
for(Int x = i; x > 0; x -= (x&-x))
s += bit[x];
return s;
}
void add(Int i, T a){
if(i == 0) return;
for(Int x = i; x <= n; x += (x & -x))
bit[x] += a;
}
void sum0(Int i){
return sum(i + 1);
}
void add0(Int i,T a){
add(i+1, a);
}
T query(Int l,Int r){
return sum(r - 1) - sum(l - 1);
}
T query0(Int l,Int r){
return sum(r) - sum(l);
}
};
template<typename T, typename E>
struct SegmentTree{
using F = function<T(T, T)>;
using G = function<T(T, E)>;
using H = function<E(E, E)>;
Int n, height;
F f;
G g;
H h;
T ti;
E ei;
vector<T> dat;
vector<E> laz;
SegmentTree(Int n_, F f, G g, H h, T ti, E ei):
f(f), g(g), h(h), ti(ti), ei(ei)
{init(n_);}
void init(Int n_){
n = 1; height = 0;
while(n < n_) n <<= 1, height++;
dat.assign(2*n, ti);
laz.assign(2*n, ei);
}
void build(Int n_, vector<T> v){
for(Int i=0;i<n_;i++) dat[n+i] = v[i];
for(Int i= n-1; i; i--)
dat[i] = f(dat[ (i<<1)|0 ], dat[ (i<<1)|1 ]);
}
T reflect(Int k){
return g(dat[k], laz[k]);
}
inline void eval(Int k){
if(laz[k] == ei) return;
laz[(k<<1) | 0] = h(laz[ (k<<1) | 0], laz[k]);
laz[(k<<1) | 1] = h(laz[ (k<<1) | 1], laz[k]);
dat[k] = reflect(k);
laz[k] = ei;
}
void update(Int a,Int b,E x){
a += n, b += n - 1;
for(Int i=height;i;i--) eval(a>>i);
for(Int i=height;i;i--) eval(b>>i);
for(Int l=a, r=b+1; l < r; l>>=1, r>>=1){
if(l & 1) laz[l] = h(laz[l], x), l++;
if(r & 1) --r, laz[r] = h(laz[r], x);
}
while(a>>=1)
dat[a] = f(reflect( (a<<1) | 0), reflect( (a<<1) | 1));
while(b>>=1)
dat[b] = f(reflect( (b<<1) | 0), reflect( (b<<1) | 1));
}
T query(Int a,Int b){
a += n; b += n-1;
for(Int i=height; i ; i--) eval(a >> i);
for(Int i=height; i ; i--) eval(b >> i);
T vl = ti, vr = ti;
for(Int l = a, r = b+1; l < r; l>>=1, r>>=1){
if(l & 1) vl = f(vl, reflect(l++));
if(r & 1) vr = f(reflect(--r), vr);
}
return f(vl, vr);
}
};
signed main(){
Int n,k;
cin>>n>>k;
vector<Int> x(n);
for(Int i=0;i<n;i++) cin>>x[i];
BIT<Int> bit(n+1,0);
Int rot=0;
for(Int i=0;i<n;i++){
rot+=i-bit.sum(x[i]);
bit.add(x[i],1);
}
//cout<<rot<<endl;
Int mv=rot-k;
if(mv<0) mv=0;
vector<Int> rev(n+1);
for(Int i=0;i<n;i++) rev[x[i]]=i;
const Int INF = 1e9;
auto f=[](Int a,Int b){return min(a,b);};
auto g=[](Int a,Int b){return b!=INF?b:a;};
SegmentTree<Int, Int> seg(n,f,g,g,INF,INF);
seg.build(n,x);
//for(Int i=0;i<n;i++) cout<<i<<":"<<seg.query(i,i+1)<<endl;
//for(Int i=0;i<n;i++) cout<<i<<":"<<seg.query(0,i+1)<<endl;
BIT<Int> ss(n+1,0);
for(Int i=0;i<n;i++) ss.add0(i,1);
vector<Int> ans;
for(Int i=0;i<n;i++){
Int l=0,r=n;
while(l+1<r){
Int m=(l+r)>>1;
if(ss.query0(0,m+1)<=mv+1) l=m;
else r=m;
}
Int t=seg.query(0,r);
//cout<<mv<<":"<<0<<" "<<r<<":"<<t<<endl;
ans.emplace_back(t);
mv-=ss.query0(0,rev[t]+1)-1;
seg.update(rev[t],rev[t]+1,INF-1);
ss.add0(rev[t],-1);
}
for(Int v:ans) cout<<v<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
struct BIT {
int N;
vector<ll> bit;
void init(int _n){
N = _n;
bit.resize(_n+1, 0LL);
}
ll getSum(int i) { // i番目までの要素の和を求める(1-index)
ll sum = 0;
while (i > 0) {
sum += bit[i];
i -= i & -i;
}
return sum;
}
void add(int i, ll x) { // i番目の要素にxを加算
while (i <= N) {
bit[i] += x;
i += i & -i;
}
}
};
struct SegmentTree {
int N;
vector<int> node;
const int INF = INT_MAX;
SegmentTree(){}
SegmentTree(int n_){
init(n_);
}
void init(int siz){
N = 1;
while (N < siz) N *= 2; // 最下段の要素数を2のべき乗にする
node.resize(2*N-1, INF);
}
void build(vector<int> &dat){
int siz = dat.size();
init(siz);
for(int i=0; i<siz; i++){
node[i+N-1] = dat[i];
}
for(int i=N-2; i>=0; i--){ // 最下段に値を入れたあと下から順に更新
node[i] = min(node[2*i+1], node[2*i+2]);
}
}
void update(int k, int a) {
k += N - 1; // 最下段最左の節点の番号
node[k] = a;
while (k > 0) { //登りながら上の要素を更新
k = (k-1)/2;
node[k] = min(node[k*2+1], node[k*2+2]);
}
}
int getMin(int a, int b) {return getMin(a, b, 0, 0, N);}
int getMin(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INF; //[a,b)と[l,r)に共通区間がないならINF
if (a <= l && r <= b)
return node[k]; //[a,b)が[l,r)を完全に含むならその節点kの値
else {
int vl = getMin(a, b, k*2+1, l, (l+r)/2);
int vr = getMin(a, b, k*2+2, (l+r)/2, r);
return min(vl, vr);
}
}
};
ll n, k;
vector<int> x, pos, ans;
BIT rsq;
SegmentTree rmq;
int main() {
cin >> n >> k;
x.resize(n);
pos.resize(n+1);
ans.resize(n, -1);
for(int i=0;i<n;i++){
cin >> x[i];
pos[x[i]] = i;
}
ll inv = 0; // 転倒数
rsq.init(n);
for(int i=n;i>=1;i--){
inv += rsq.getSum(pos[i]+1);
rsq.add(pos[i]+1, 1);
}
k = max(0LL, inv - k);
if(k == 0) ans = x;
else{
rmq.build(x);
for(int i=0;i<n;i++){
int j = rmq.getMin(0, n); // 貪欲に最小の数を先頭に持っていく
while(rsq.getSum(pos[j]) > k){ // 不可能ならばその前方の最小値
j = rmq.getMin(0, pos[j]);
}
ans[i] = j;
k -= rsq.getSum(pos[j]);
rsq.add(pos[j]+1, -1);
rmq.update(pos[j], rmq.INF);
if(k==0)break;
}
for(int i=0,j=0;i<n;i++){
if(ans[i] < 0){
while(rmq.getMin(j,j+1)==rmq.INF) j++;
ans[i] = rmq.getMin(j,j+1);
rmq.update(j, rmq.INF);
}
}
}
for(int i=0;i<n;i++){
cout << ans[i] << endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
//INSERT ABOVE HERE
signed main(){
Int n,q,k,d;
cin>>n>>q>>k>>d;
k--;d--;
const int MAX = 60;
while(q>=MAX){
cout<<(k&1)<<"\n";
k/=2;
q--;
}
using I = __int128;
I N=n,Q=q,K=k,D=d;
I X=((D+0)*(I(1)<<Q)-K+N-1)/N;
I Y=((D+1)*(I(1)<<Q)-K+N-1)/N;
if(X==Y){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<q;i++){
int b=int((X>>i)&1)^(k&1);
k/=2;
k+=(n/2)*int((X>>i)&1);
cout<<b<<"\n";
}
assert(k==d);
cout<<flush;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
typedef long long ll;
typedef pair<ll,ll> P;
ll n, q, k ,d;
vector<set<P>> s(61);
void build(ll target) {
//cout << "dfs " << target << endl;
s[0].insert(MP(target, target + 1));
REP(i, 60){
for(auto it = s[i].begin();it != s[i].end();){
P p= *it;
it++;
while(it != s[i].end() && it -> FI <= p.SE){
p.SE = max(p.SE, it -> SE);
it++;
}
{
ll l=p.first,r=min(p.second,n/2)-1;
if(l <= r)s[i+1].insert(P(2*(l%(n/2)),2*(r%(n/2))+2));
}
{
ll l=max(p.first,n/2),r=min(p.second,n)-1;
if(l <= r)s[i+1].insert(P(2*(l%(n/2)),2*(r%(n/2))+2));
}
}
}
}
int main(){
cin >> n >> q >> k >> d;
d--;k--;
build(d);
//cout << "out build" << endl;
vector<int> ans;
for(int i = q;i > 0;i--){
//cout << "k " << k << endl;
if(i > 60){
if(k % 2)ans.PB(1);
else ans.PB(0);
k /= 2;
}
else {
bool ok = false;
ll a = (k / 2);
ll b = (k / 2) + n / 2;
//cout << "ab " << a << " " << b << endl;
for(auto it = s[i-1].begin();it != s[i-1].end();it++){
if(it -> FI <= a && a < it -> SE){
if(k%2)ans.PB(1);
else ans.PB(0);
k = a;
ok = true;
break;
}
if(it -> FI <= b && b < it -> SE){
if(k%2)ans.PB(0);
else ans.PB(1);
k = b;
ok = true;
break;
}
}
if(!ok){
cout << -1 << endl;
return 0;
}
}
}
REP(i,q)cout << ans[i] << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll under[1000500];
ll over[1000500];
ll n, q, k, d;
void query(int now) {
ll delta = (over[now+1] - under[now+1] + 1 + n) % n;
if(delta * 2 >= n || delta == 0) {
under[now] = 1;
over[now] = n;
return;
}
ll after = under[now+1];
if(after > n / 2) after - n / 2;
ll new_under = ((after-1) * 2 + 1) % n;
under[now] = new_under;
over[now] = (new_under + delta * 2 - 1 + n) % n;
return;
}
bool isin(ll under, ll over, ll val) {
if(under <= over) {
if(((under <= val) && (val <= over))) {
//cout << -1 << endl;
return true;
}
} else {
if(((val >= under) || (val <= over))) {
//cout << -1 << endl;
return true;
}
}
//cerr << "FAILED " << under << " " << over << " " << val << endl;
return false;
}
ll one(ll now) {
if(now % 2 == 0) {
return now / 2;
}
return n / 2 + (now + 1) / 2;
}
ll zero(ll now) {
if(now % 2 == 0) {
return n / 2 + now / 2;
}
return (now + 1) / 2;
}
int main() {
cin >> n >> q >> k >> d;
over[q] = d;
under[q] = d;
for(int i = q - 1; i >= 0; i--) {
query(i);
}
for(int i = 0; i <= q; i++) {
//cout << under[i] << " " << over[i] << endl;
}
/*
if(under[0] < over[0]) {
if(!((under[0] <= k) && (k <= over[0]))) {
//cout << -1 << endl;
return 0;
}
} else {
if(!((k >= under[0]) || (k <= over[0]))) {
//cout << -1 << endl;
return 0;
}
}
*/
if(!isin(under[0], over[0], k)) {
cout << -1 << endl;
return 0;
}
vector<int> ans;
ll now = k;
for(int i = 1; i <= q; i++) {
ll NEW = zero(now);
//cout << i << " NEWNUMBER : " << NEW << endl;
if(isin(under[i], over[i], NEW)) {
ans.push_back(0);
now = NEW;
//cout << "0 " << now << endl;
continue;
}
NEW = one(now);
ans.push_back(1);
now = NEW;
//cout << "1 " << now << endl;
}
for(int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
return 0;
}
|
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
using namespace std;
const long long int MOD = 1000000007;
//const int MOD = 998244353;
long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R
long long int power(long long int x, long long int n, long long int M, long long int d) {
long long int tmp = d;
while (n) {
if (n & 1) {
tmp *= x;
tmp %= M;
}
x *= x;
x %= M;
n /= 2;
}
return tmp;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long int n, q, k, d;
cin >> n >> q >> k >> d;
k--;
d--;
long long int LMOD = d;
for (int i = 0; i < q; i++) {
LMOD *= 2;
LMOD %= n;
}
if (q < 55) {
if (n > 1LL << q) {
//long long int LMOD = d;
long long int RMOD = LMOD + (1LL << q) - 1;
//cout << LMOD << " " << k << " " << RMOD << endl;
if (!((LMOD <= k && k <= RMOD) || (LMOD <= k + n && k + n <= RMOD))) {
cout << -1 << endl;
return 0;
}
}
}
//cout << bitset<15>(k - LMOD) << endl;
long long int box = (k - LMOD + n) % n;
vector<long long int>ans, tapi({ d });
for (int i = 0; i < q; i++) {
//if (d >= N / 2) {
//}
//else {
//}
d *= 2;
d += q - 1 - i > 63 ? 0 : ((box >> q - 1 - i) & 1);
d %= n;
tapi.push_back(d);
///cout << d << endl;
//box /= 2;
}
// cout << d << " " << k << endl;
assert(d == k);
reverse(tapi.begin(), tapi.end());
for (int i = 0; i < q; ++i) {
ans.push_back((tapi[i] & 1) ^ (tapi[i + 1] >= n / 2));
}
for (auto i : ans)cout << i << endl;
}
|
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll N;
class Set{
public:
vpll a;
void Add(ll u,ll v){a.push_back({u,v});}
vpll Fix(){
vpll b,c;
for(auto p:a){
ll L=p.first,R=p.second;
if(L>=N) b.push_back({L-N,R-N});
else if(R>=N){
b.push_back({L,N-1});
b.push_back({0,R-N});
}
else b.push_back({L,R});
}
sort(b.begin(),b.end());
ll L,R=-1;
for(auto p:b){
if(R<p.first){
if(R>=0) c.push_back({L,R});
L=p.first;R=p.second;
}
else R=max(R,p.second);
}
c.push_back({L,R});
for(auto &p:c) p={p.first*2,p.second*2};
return c;
}
bool Check(ll x){
for(auto p:a) if(p.first<=x&&x<p.second) return 1;
return 0;
}
void Open(){
for(auto p:a) cout<<p.first<<' '<<p.second<<endl;
}
};
const int M=1000005;
ll n,q,x,y;
Set st[M];
int main(){
cin>>n>>q>>x>>y;
x--;y--;
N=n/2;
st[q-1].Add(y,y+1);
for(int i=q-1;i>0;i--){
vpll tmp=st[i].Fix();
for(auto p:tmp) st[i-1].Add(p.first,p.second);
}
//for(int i=0;i<q;i++){st[i].Open();cout<<endl;}
if(!st[0].Check(x/2)&&!st[0].Check(x/2+N)){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<q;i++){
if(st[i].Check(x/2)){
cout<<x%2<<endl;
x=x/2;
}
else{
cout<<(x+1)%2<<endl;
x=x/2+N;
}
}
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using pint = pair<int,int>;
long long N, Q, K, D;
void solve() {
--K, --D;
long long pK = K, pD = D;
for (int i = 0; i < Q; ++i) D = D * 2 % N;
long long diff = (K - D + N) % N;
// tmp: 逆順で見たときの 0: 2x、1: 2x+1
vector<long long> tmp;
while (diff) {
tmp.push_back(diff & 1);
diff >>= 1;
}
if (tmp.size() > Q) { cout << -1 << endl; return; }
while (tmp.size() < Q) tmp.push_back(0);
reverse(tmp.begin(), tmp.end());
// 実際の操作に適合した復元
vector<long long> res;
for (int q = 0; q < tmp.size(); ++q) {
long long nD = pD * 2;
if (tmp[q]) ++nD;
nD %= N;
// nD -> pD が実際には 0 なのか 1 なのかを判定
if (nD % 2 == 0) {
if (pD == nD/2) res.push_back(0);
else res.push_back(1);
}
else {
if (pD == nD/2) res.push_back(1);
else res.push_back(0);
}
pD = nD;
}
reverse(res.begin(), res.end());
for (int i = 0; i < Q; ++i) cout << res[i] << endl;
}
int main() {
cin >> N >> Q >> K >> D;
solve();
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int main(){
ll n,q,k,d;
cin>>n>>q>>k>>d;
--k;--d;
ll res=d;
rep(i,q)res=(res*2)%n;
ll ret=(k-res+2*n)%n;
if(q<62&&ret>=(1LL<<q)){
cout<<-1<<endl;
return 0;
}
vector<int> tmp(q);
rep(i,60){
if((1LL<<i)&ret)tmp[i]=1;
}
vector<ll> ans(q);
ll cur=d;
for(int i=q-1;i>=0;--i){
if(cur<n/2)ans[i]=tmp[i];
else ans[i]=1-tmp[i];
cur=(2*cur+tmp[i])%n;
}
rep(i,q){
cout<<ans[i]<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7;
const ll LINF=1e18;
using namespace std;
#define int long long
//template
//main
signed main(){
int n,q,k,d;cin>>n>>q>>k>>d;
k--;d--;
std::vector<int> ans;
int sd=d;
for(int i=0;i<q;i++)sd=sd*2%n;
int a=(k-sd+n)%n;
std::vector<int> vs;
for(int i=0;i<q;i++){
vs.push_back(a%2);
a/=2;
}
if(a){cout<<-1<<endl;return 0;}
for(int i=0;i<q;i++){
int b1=vs[q-1-i];
int b2=(d>=n/2);
ans.push_back(b1^b2);
d=d*2%n+b1;
}
reverse(ans.begin(),ans.end());
//cout<<d<<endl;
for(int p:ans)cout<<p<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll
#define INF 1e16
#define mod 1000000007
ll n,q,k,d;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin>>n>>q>>k>>d;
k--;d--;
ll crt=d;
rep(i,q)crt=(crt*2)%n;
ll rest=(k-crt+n)%n;
if(q<60&&rest>=(1LL<<q)){
cout<<-1<<endl;
return 0;
}
vector<ll> mv(q,0);
rep(i,60){
if((rest>>i)&1)mv[i]=1;
}
vector<ll> res(q);
crt=d;
for(ll i=q-1;i>=0;i--){
if(crt<n/2) res[i]=mv[i];
else res[i]=1-mv[i];
crt=(crt*2+mv[i])%n;
}
assert(crt==k);
rep(i,q)cout<<res[i]<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
ll n,q,k,d;
ll beg[1000005];
int main(){
cin>>n>>q>>k>>d;
ll cur = d; beg[0] = cur;
rep(i,q){
if(cur > n/2) cur -= n/2;
cur = 2*cur-1;
beg[i+1] = cur;
}
//cur+x = k (mod n)
//x = k-cur (mod n)
ll hoge = k-cur;
hoge = (hoge%n+n)%n;
if(q < 51 && hoge >= (1LL<<q)){
puts("-1");
}
else{
vector<int>vec;
ll V = hoge;
for(int i=0;V;i++,V/=2){
ll up = beg[q-i-1]+V/2;
while(up > n) up-=n;
if(up > n/2){
vec.pb( ((hoge>>i)&1) );
}
else{
vec.pb( 1-((hoge>>i)&1) );
}
}
while(vec.size() < q){
ll up = beg[q-vec.size()-1];
if(up > n/2){
vec.pb( 0 );
}
else{
vec.pb( 1 );
}
}
for(int i=0;i<q;i++) printf("%d\n",1-vec[i]);
}
}
|
#include <iostream>
#include <ccomplex>
#include <vector>
#include <cassert>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <list>
#include <assert.h>
#include <math.h>
#include <valarray>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <complex>
#include <list>
#include <time.h>
#include <stack>
#include <locale>
#include <clocale>
#include <ctype.h>
#include <wchar.h>
#include <random>
#include <vector>
#include <unordered_map>
#include <bitset>
using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;
#define SORT(c) sort((c).begin(),(c).end())
#define BACKSORT(c) sort((c).begin(),(c).end(),std::greater<LL>())
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
LL mod = 1000000007;
struct hoge{
public:
LL a, b, c, d;
public:
LL num;
public:
hoge(LL a, LL b, LL c, LL d,LL num) : a(a), b(b), c(c), d(d),num(num) {}
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
LL n, q, k, d;
cin >> n >> q >> k >> d;
k--;
d--;
vector<hoge> v;
v.push_back(hoge(d, d, d, d, 1));
FOR(i,1,q+1){
hoge prev = v[i - 1];
hoge nowhoge = hoge((prev.a * 2) % n, (prev.b * 2) % n, (prev.c * 2) % n, (prev.d * 2+1) % n, (prev.num * 2));
if(nowhoge.d<nowhoge.a){
nowhoge.d += n;
}
if (nowhoge.num >= n)
{
nowhoge.d += n;
v.push_back(nowhoge);
break;
}
v.push_back(nowhoge);
}
hoge last = v[v.size() - 1];
LL now = k;
if(last.num>=n){
for (LL i = 0; i < q - v.size() + 1;++i)
{
if(now%2==0){
cout << 0 << endl;
}
else
{
cout << 1 << endl;
}
now /= 2;
}
}
REP(i,v.size()-1){
hoge nowhoge = v[v.size() - i-1];
hoge nexthoge = v[v.size() - i-2];
bool flag = false;
REP(j, 2)
{
//cout << nowhoge.a << " " << now << " " << nowhoge.d << endl;
if (nowhoge.a <= now && now <= nowhoge.d)
{
bool a = now % 2 == 0;
now = ((now - nowhoge.a) / 2 + nexthoge.a) % n;
if(a ^ (now/(n/2)!=0))
{
cout << 0 << endl;
}else{
cout << 1 << endl;
}
flag = true;
break;
}
now += n;
}
if(flag)
continue;
cout << -1 << endl;
break;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, s, t;
int q;
cin >> n >> q >> s >> t;
n /= 2; s--; t--;
using P = pair<ll, ll>;
VV<P> ok(q + 1);
ok[q].push_back({t, t + 1});
for (int ph = q - 1; ph >= 0; ph--) {
for (auto p: ok[ph + 1]) {
ll lw = p.first, up = p.second;
assert(lw <= up);
if (lw < n && up <= n) {
ok[ph].push_back({2 * lw, 2 * up});
} else if (n <= lw && n <= up) {
ok[ph].push_back({2 * (lw - n), 2 * (up - n)});
} else {
ok[ph].push_back({2 * lw, 2 * n});
ok[ph].push_back({0LL, 2 * (up - n)});
}
}
sort(ok[ph].begin(), ok[ph].end());
V<P> buf;
for (auto p: ok[ph]) {
if (!buf.empty() && p.first <= buf.back().second) {
buf.back().second = max(buf.back().second, p.second);
continue;
}
buf.push_back(p);
}
ok[ph] = buf;
/* for (auto p: ok[ph]) {
cout << "(" << p.first << " " << p.second << "), ";
}
cout << endl;*/
}
auto contain = [&](int ph, ll x) {
for (auto p: ok[ph]) {
if (p.first <= x && x < p.second) return true;
}
return false;
};
if (!contain(0, s)) {
cout << -1 << endl;
return 0;
}
V<int> ans;
for (int i = 1; i <= q; i++) {
int f = s % 2; s /= 2;
if (contain(i, s)) {
ans.push_back(f ^ 0);
} else {
s += n;
assert(contain(i, s));
ans.push_back(f ^ 1);
}
}
for (int d: ans) {
cout << d << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))
// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD
// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;
ll n,q,k,d;
ll low[1252525], high[1252525];
int main(){
scanf("%lld%lld%lld%lld",&n,&q,&k,&d);
--k;--d;
low[0] = high[0] = d;
REP(i,q){
low[i+1] = n; high[i+1] = -1;
{
ll x = low[i];
if(x>=n/2)x-=n/2;
CHMIN(low[i+1], 2*x);
CHMAX(high[i+1], 2*x+1);
}
{
ll x = high[i];
if(x>=n/2)x-=n/2;
CHMIN(low[i+1], 2*x);
CHMAX(high[i+1], 2*x+1);
}
if(low[i]<=n/2-1 && n/2-1<=high[i]){
ll x = n/2-1;
if(x>=n/2)x-=n/2;
CHMIN(low[i+1], 2*x);
CHMAX(high[i+1], 2*x+1);
}
if(low[i]<=n/2 && n/2<=high[i]){
ll x = n/2;
if(x>=n/2)x-=n/2;
CHMIN(low[i+1], 2*x);
CHMAX(high[i+1], 2*x+1);
}
}
if(k<low[q] || k>high[q]){
puts("-1");return 0;
}
ll x = k;
FORR(i,0,q){
if(low[i]<=x/2 && x/2<=high[i]){
puts(x%2==0 ? "0" : "1");
x = x/2;
}else{
puts(x%2==0 ? "1" : "0");
x = x/2 + n/2;
}
}
assert(x == d);
return 0;
}
|
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;
bool check(vector<pair<ll, ll>> &v, ll x) {
REP(i, 0, v.size()) {
if (v[i].first <= x && x < v[i].second) {
return true;
}
}
return false;
}
int main(void) {
ll N, Q, K, D;
cin >> N >> Q >> K >> D, K--, D--;
vector<vector<pair<ll, ll>>> z(Q + 1);
z[Q].push_back(make_pair(D, D + 1));
for (ll i = Q - 1; i >= 0; i--) {
vector<pair<ll, ll>> v;
REP(k, 0, z[i + 1].size()) {
ll l = z[i + 1][k].first, h = z[i + 1][k].second;
if (l < N / 2 && h <= N / 2) {
v.push_back(make_pair(l * 2, h * 2));
} else if (l < N / 2 && h > N / 2) {
v.push_back(make_pair(l * 2, N));
v.push_back(make_pair(0, h * 2 - N));
} else if (l >= N / 2 && h > N / 2) {
v.push_back(make_pair(l * 2 - N, h * 2 - N));
}
}
sort(v.begin(), v.end());
REP(k, 0, v.size()) {
assert(v[k].first < v[k].second);
if (z[i].size() >= 1) {
if (z[i].back().second >= v[k].first) {
z[i].back().second = v[k].second;
continue;
}
}
z[i].push_back(v[k]);
}
}
if (!check(z[0], K)) {
cout << -1 << endl;
} else {
vector<ll> ans(Q);
ll x = K;
REP(i, 0, Q) {
ll n = x % 2 == 0 ? x / 2 : x / 2 + N / 2;
if (check(z[i + 1], n)) {
x = n;
ans[i] = 0;
} else {
x = x % 2 == 0 ? x / 2 + N / 2 : x / 2;
ans[i] = 1;
}
}
REP(i, 0, Q) printf("%ld\n", ans[i]);
}
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
double x[10010],y[10010];
double ans;
int pd;
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>x[i]>>y[i];
x[i+n]=x[i],y[i+n]=y[i];
}
for(int j=1;j<=n-1;j++){
double xx=x[j],yy=y[j];
for(int i=1;i<=2*n;i++){
x[i]-=xx,y[i]-=yy;
}
for(int i=3+j-1;i<=n+j-1;i++){
if(x[i-1]*y[i]-x[i]*y[i-1]<0) pd=1;
}
for(int i=1;i<=2*n;i++){
x[i]-=xx,y[i]-=yy;
}
}
cout<<(pd^1)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace Computational_Geometry
{
const long double eps=1e-10;
struct Vector2
{
long double x,y;
Vector2(long double X=0.0,long double Y=0.0):x(X),y(Y) {}
Vector2 *read() {scanf("%Lf%Lf",&x,&y); return this;}
void print() {printf("%.10Lf %.10Lf",x,y); return;}
inline Vector2 operator - () const{return Vector2(-x,-y);}
inline Vector2 operator + (const Vector2 &A) const{return Vector2(x+A.x,y+A.y);}
inline Vector2 operator - (const Vector2 &A) const{return Vector2(x-A.x,y-A.y);}
inline Vector2 operator * (const long double &k) const{return Vector2(x*k,y*k);}
inline Vector2 operator / (const long double &k) const{return Vector2(x/k,y/k);}
inline long double operator * (const Vector2 &A) const{return x*A.x+y*A.y;}
inline long double operator ^ (const Vector2 &A) const{return x*A.y-y*A.x;}
inline long double norm2() const{return x*x+y*y;}
inline long double norm() const{return sqrtl(x*x+y*y);}
inline bool operator < (const Vector2 &A) const{return (x<A.x-eps)||(x<=A.x+eps)&&(y<A.y-eps);}
inline bool operator == (const Vector2 &A) const{return fabsl(x-A.x)<=eps&&fabsl(y-A.y)<=eps;}
inline bool operator << (const Vector2 &A) const{return (y<-eps)^(A.y<-eps)?A.y<-eps:((*this^A)>eps)||((*this^A)>=-eps)&&(x>=-eps)&&(A.x<-eps);}
inline Vector2 trans(long double a_11,long double a_12,long double a_21,long double a_22) const{return Vector2(x*a_11+y*a_12,x*a_21+y*a_22);}
};
#define Point Vector2
Point Projection(Point P,Point L1,Point L2)
{
long double k1=(L2-L1)*(P-L1),k2=(L2-L1).norm2();
return L1+((L2-L1)*(k1/k2));
}
Point Reflection(Point P,Point L1,Point L2)
{
long double k1=(L2-L1)*(P-L1),k2=(L2-L1).norm2();
Point mid=L1+(L2-L1)*(k1/k2);
return mid*2-P;
}
/*
COUNTER_CLOCKWISE 1
CLOCKWISE 2
ONLINE_BACK 3
ONLINE_FRONT 4
ON_SEGMENT 5
*/
int Counter_Clockwise(Point S,Point T1,Point T2)
{
if(((T1-S)^(T2-S))>eps) return 1;
else if(((T1-S)^(T2-S))<-eps) return 2;
else if(((T1-S)*(T2-S))<-eps) return 3;
else if(((T2-S).norm2()-(T1-S).norm2())>eps) return 4;
else return 5;
}
int Counter_Clockwise_Bool(Point S,Point T1,Point T2)
{
if(((T1-S)^(T2-S))>eps) return 1;
else if(((T1-S)^(T2-S))<-eps) return -1;
else if(((T1-S)*(T2-S))<-eps) return -1;
else if(((T2-S).norm2()-(T1-S).norm2())>eps) return 1;
else return 0;
}
int Parallel(Point S1,Point T1,Point S2,Point T2)
{
return fabsl((T1-S1)^(T2-S2))<=eps;
}
int Orthogonal(Point S1,Point T1,Point S2,Point T2)
{
return fabsl((T1-S1)*(T2-S2))<=eps;
}
/*
PARALLEL 1
ORTHOGONAL 2
OTHER 3
*/
int Parallel_Orthogonal(Point S1,Point T1,Point S2,Point T2)
{
if(Parallel(S1,T1,S2,T2)) return 1;
else if(Orthogonal(S1,T1,S2,T2)) return 2;
else return 3;
}
int Intersection(Point S1,Point T1,Point S2,Point T2)
{
return Counter_Clockwise_Bool(S1,T1,S2)*Counter_Clockwise_Bool(S1,T1,T2)<=0&&Counter_Clockwise_Bool(S2,T2,S1)*Counter_Clockwise_Bool(S2,T2,T1)<=0;
}
Point Crosspoint(Point S1,Point T1,Point S2,Point T2)
{
long double d1=fabsl((S1-S2)^(T2-S2)),d2=fabsl((T1-S2)^(T2-S2));
return S1+(T1-S1)*(d1/(d1+d2));
}
long double DistanceLP(Point S,Point T,Point P)
{
return fabsl((P-S)^(T-S)/(T-S).norm());
}
long double DistanceSP(Point S,Point T,Point P)
{
if((P-S)*(T-S)<-eps) return (P-S).norm();
else if((P-T)*(S-T)<-eps) return (P-T).norm();
else return DistanceLP(S,T,P);
}
long double Distance(Point S1,Point T1,Point S2,Point T2)
{
if(Intersection(S1,T1,S2,T2)) return 0;
else return min(min(DistanceSP(S1,T1,S2),DistanceSP(S1,T1,T2)),min(DistanceSP(S2,T2,S1),DistanceSP(S2,T2,T1)));
}
long double Area(Point *P,int N)
{
long double Ans=0;
for(int i=1;i<=N;i++) Ans+=P[i]^P[i==N?1:i+1]/2;
return Ans;
}
int Is_Convex(Point *P,int N)
{
P[N+1]=P[1],P[N+2]=P[2];
for(int i=1;i<=N;i++)
if(Counter_Clockwise(P[i],P[i+1],P[i+2])==2) return 0;
return 1;
}
};
using namespace Computational_Geometry;
Point p,p0,p1,s0,s1,t0,t1,p_[100009];
int q,n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) p_[i].read();
printf("%d\n",Is_Convex(p_,n));
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef complex<double>Point;
struct Segment{
Point p1,p2;
Segment(const Point &p1=Point(),const Point &p2=Point()):p1(p1),p2(p2){}
};
struct Circle{
Point p;
double r;
Circle(const Point &p=Point(),double r=0.0):p(p),r(r){}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point>Polygon;
const double PI=acos(-1);
const double EPS=1e-8;
const double INF=1e16;
const int COUNTER_CLOCKWISE=1;
const int CLOCKWISE=-1;
const int ONLINE_BACK=2;
const int ONLINE_FRONT=-2;
const int ON_SEGMENT=0;
inline double square(double a){return a*a;}
inline double norm(const Point &a){return square(a.real())+square(a.imag());}
inline double dot(const Point &a,const Point &b){return (conj(a)*b).real();}
inline double cross(const Point &a,const Point &b){return (conj(a)*b).imag();}
inline double toDeg(double t){return t/PI*180.0;}
inline double toRad(double t){return t/180.0*t;}
#define equals(a,b) (fabs((a)-(b))<EPS)
#define curr(P,i) P[(i)%P.size()]
#define next(P,i) P[(i+1)%P.size()]
#define prev(P,i) P[(i-1+P.size())%P.size()]
void getPoint(Point &p){
double x,y;
scanf("%lf%lf",&x,&y);
p=Point(x,y);
}
void getSegment(Segment &s){
getPoint(s.p1);
getPoint(s.p2);
}
bool isOrthogonal(Vector a,Vector b){
return equals(dot(a,b),0.0);
}
bool isOrthogonal(Point a1,Point a2,Point b1,Point b2){
return isOrthogonal(a1-a2,b1-b2);
}
bool isOrthogonal(Segment s1,Segment s2){
return isOrthogonal(s1.p1-s1.p2,s2.p1-s2.p2);
}
bool isParallel(Vector a,Vector b){
return equals(cross(a,b),0.0);
}
bool isParallel(Point a1,Point a2,Point b1,Point b2){
return isParallel(a1-a2,b1-b2);
}
bool isParallel(Segment s1,Segment s2){
return isParallel(s1.p1-s1.p2,s2.p1-s2.p2);
}
Point project(Segment s,Point p){
Vector base=s.p2-s.p1;
double r=dot(p-s.p1,base)/norm(base);
return s.p1+base*r;
}
Point reflection(Segment s,Point p){
return p+(project(s,p)-p)*2.0;
}
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(norm(a)<norm(b))return ONLINE_FRONT;
return ON_SEGMENT;
}
bool isIntersectSS(Point p1,Point p2,Point p3,Point p4){
return ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&
ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0;
}
bool isIntersectSS(Segment s1,Segment s2){
return isIntersectSS(s1.p1,s1.p2,s2.p1,s2.p2);
}
Point getCrossPointSS(Segment s1,Segment s2){
Vector base=s2.p2-s2.p1;
double d1=abs(cross(base,s1.p1-s2.p1));
double d2=abs(cross(base,s1.p2-s2.p1));
double t=d1/(d1+d2);
return s1.p1+(s1.p2-s1.p1)*t;
}
double getDistancePP(Point a,Point b){
return abs(a-b);
}
double getDistanceLP(Line l,Point p){
return fabs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));
}
double getDistanceLL(Line l,Line m){
if(isParallel(l,m))return getDistanceLP(l,m.p1);
return 0.0;
}
double getDistanceSP(Segment s,Point p){
if(dot(s.p2-s.p1,p-s.p1)<0.0)return abs(p-s.p1);
if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
return getDistanceLP(s,p);
}
double getDistanceSS(Segment s1,Segment s2){
if(isIntersectSS(s1,s2))return 0;
return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}
double area(Polygon g){
double res=0.0;
for(int i=0;i<g.size();i++){
res+=cross(curr(g,i),next(g,i));
}
return res/2.0;
}
bool isConvex(Polygon g){
for(int i=0;i<g.size();i++){
if(ccw(prev(g,i),curr(g,i),next(g,i))==CLOCKWISE)return false;
}
return true;
}
int main(){
int n;
scanf("%d",&n);
Polygon g;
for(int i=0;i<n;i++){
Point a;
getPoint(a);
g.push_back(a);
}
printf("%d\n",isConvex(g));
return 0;
} |
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,a,b) for(register ll(i)=(ll)(a);(i)<=(ll)(b);++i)
const ll inf=1e9+7;
#define y1 _y_1_
#ifndef gc
#define gc getchar
#endif
#ifndef pc
#define pc putchar
#endif
inline ll read(){ll f=0,x=0;register char c=gc();while(!isdigit(c))f^=c=='-',c=gc();while(isdigit(c))x=(x<<3)+(x<<1)+(c^'0'),c=gc();return x=f?-x:x;}
inline ll read(ll&x){ll f=0;x=0;register char c=gc();while(!isdigit(c))f^=c=='-',c=gc();while(isdigit(c))x=(x<<3)+(x<<1)+(c^'0'),c=gc();return x=f?-x:x;}
inline void writeln(){pc('\n');}
inline void write(ll x){if(x<0)pc('-'),x=-x;if(x>9)write(x/10);pc(x%10+'0');}
inline void writeln(const ll&x){write(x),pc('\n');}
using namespace std;
namespace Computation_Geometry{
const ld eps=1e-9;
struct vec{
ld x,y;
inline bool is_zero(){return (abs(x)<=eps)&(abs(y)<=eps);}
inline void scan(){x=read(),y=read();}
inline vec(){x=y=0;}
inline vec(const ld&X,const ld&Y){x=X,y=Y;}
inline friend vec operator +(const vec&A,const vec&B){return vec(A.x+B.x,A.y+B.y);}
inline friend vec operator -(const vec&A,const vec&B){return vec(A.x-B.x,A.y-B.y);}
inline friend vec operator *(const vec&A,const ld&k){return vec(A.x*k,A.y*k);}
inline friend vec operator /(const vec&A,const ld&k){return A*(1.0/k);}
inline ld len(){return sqrt(x*x+y*y);}
inline ld Len(){return x*x+y*y;}
inline friend ld len(vec A){return A.len();}
inline friend ld Len(vec A){return A.Len();}
inline ld len(const ll&X,const ll&Y){return sqrt((x-X)*(x-X)+(y-Y)*(y-Y));}
inline ld k(){return (x==0)?inf:y/x;}
inline ld k(const ll&X,const ll&Y){return (x==X)?inf:(y-Y)/(x-X);}
inline friend ld Dot(const vec&A,const vec&B){return A.x*B.x+A.y*B.y;}
inline friend ld Cross(const vec&A,const vec&B){return A.x*B.y-B.x*A.y;}
inline friend ld Theta(vec A,vec B){return acos(Cross(A,B)/A.len()/B.len());}
inline friend ll ccw(vec p0,vec p1,vec p2){
p1=p1-p0,p2=p2-p0;
if (Cross(p1,p2)>=eps) return 1;//COUNTER_CLOCKWISE
else if (Cross(p1,p2)<-eps) return -1;//CLOCKWISE
else if (Dot(p1,p2)<-eps) return 2;//ONLINE_BACK
else if (p1.len()<p2.len())return -2;//ONLINE_FRONT
return 0;//ON_SEGMENT
}
};
typedef vec point;
struct line{
ld A,B,C;
inline line(){A=B=C=0;}
inline line(const ld&_A,const ld&_B,const ld&_C){A=_A,B=_B,C=_C;}
inline line(const ld&x1,const ld&y1,const ld&x2,const ld&y2){A=y2-y1,B=x1-x2,C=x2*y1-x1*y2;}
inline line(const vec&_A,const vec&_B){A=_A.y-_B.y,B=_B.x-_A.x,C=Cross(_A,_B);}
inline vec Vec(){return vec(A,B);}
inline friend bool is_on_line(line A,point B){return abs(A.A*B.x+A.B*B.y+A.C)<=eps;}
inline friend bool parallel(line A,line B){return abs(Cross(A.Vec(),B.Vec()))<=eps;}
inline friend bool perpendicular(line A,line B){return abs(Dot(A.Vec(),B.Vec()))<=eps;}
inline friend point intersection(const line A, const line B){return point(A.B*B.C-A.C*B.B,A.C*B.A-A.A*B.C)/(A.A*B.B-A.B*B.A);}
};
struct segment{
ld x1,x2,y1,y2;
inline point p1(){return vec(x1,y1);}
inline point p2(){return vec(x2,y2);}
inline segment(){x1=y1=x2=y2=0;}
inline segment(const ld&X1,const ld&Y1,const ld&X2,const ld&Y2){x1=X1,y1=Y1,x2=X2,y2=Y2;}
inline line Line(){return line(x1,y1,x2,y2);}
inline friend bool is_on_segment(segment A,point B){
if (!is_on_line(A.Line(),B)) return false;
return ((A.x1<=B.x&&B.x<=A.x2)||(A.x2<=B.x&&B.x<=A.x1))&&((A.y1<=B.y&&B.y<=A.y2)||(A.y2<=B.y&&B.y<=A.y1));
}
inline friend bool is_intersected(segment A,segment B){
vec a=vec(A.x1,A.y1),b=vec(A.x2,A.y2),c=vec(B.x1,B.y1),d=vec(B.x2,B.y2);
return ((ccw(a,b,c)*ccw(a,b,d)<=0)&&(ccw(c,d,a)*ccw(c,d,b)<=0));
}
inline friend ld Distance_SP(segment s,point p){return fabs(Cross(s.p2()-s.p1(),p-s.p1())/len(s.p2()-s.p1()));}
inline friend ld distance_SP(segment s,point p){
point p1=s.p1(),p2=s.p2();
if(Dot(p2-p1,p-p1)<0.0) return len(p-p1);
if(Dot(p1-p2,p-p2)<0.0) return len(p-p2);
return Distance_SP(s,p);
}
inline friend ld distance_SS(segment s1,segment s2){return min(min(distance_SP(s1,s2.p1()),distance_SP(s1,s2.p2())),min(distance_SP(s2,s1.p1()),distance_SP(s2,s1.p2())));}
};
struct polygon{
vector<vec>polygon;
inline void scan(){
ll n=read();
rep(i,1,n){
ll x=read(),y=read();
polygon.push_back(vec(x,y));
}
}
inline ld area(){
ll n=polygon.size();
ld ret=Cross(polygon[n-1],polygon[0]);
rep(i,0,n-2) ret+=Cross(polygon[i],polygon[i+1]);
return fabs(ret)/2;
}
inline bool is_convex(){
ll n=polygon.size();
rep(i,0,n-1) if (ccw(polygon[(i+n-1)%n],polygon[i],polygon[(i+1)%n])==-1) return 0;
return 1;
}
};
}
using namespace Computation_Geometry;
polygon p;
signed main(){
p.scan();
puts(p.is_convex()?"1":"0");
fclose(stdin);fclose(stdout);return 0;
}
|
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
class Vector {
public:
double x, y;
// コンストラクタ
Vector() {
x = 0.0;
y = 0.0;
}
// xを設定する
void setX(double nx) { x = nx; }
// yを設定する
void setY(double ny) { y = ny; }
// コンストラクタ(引数あり)
Vector(double nx, double ny) {
x = nx;
y = ny;
}
// x,yを設定する
void set(double nx, double ny) {
x = nx;
y = ny;
}
// 大きさを求める
double norm() const {
return sqrt(x * x + y * y);
}
// 回転する
void rotate(double radian) {
x = x * cos(radian) - y * sin(radian);
x = x * sin(radian) + y * cos(radian);
}
// 内積を求める
double dot(const Vector& vec) const {
return x * vec.x + y * vec.y;
}
// 外積を求める
double cross(const Vector& vec) const {
return x * vec.y - y * vec.x;
}
// 余弦を返す
double getCos(const Vector& vec) const {
double nor = norm() * vec.norm();
if (nor == 0.0) {
return 0.0;
} else {
return dot(vec) / nor;
}
}
// 単位ベクトルを返す
Vector getUnit() const {
Vector v;
double mag = norm();
if (mag == 0.0) {
v.set(0.0, 0.0);
} else {
v.set(x / mag, y / mag);
}
return v;
}
// ベクトル1をベクトル2に正射影したベクトルを求める
Vector getProjection(const Vector& vec1, const Vector& vec2) {
Vector vec(vec2.getUnit() * vec1.norm() * vec1.getCos(vec2));
return vec;
}
void operator = (const Vector& vec) {
x = vec.x;
y = vec.y;
}
Vector operator + (const Vector& vec) const {
Vector v(x + vec.x, y + vec.y);
return v;
}
Vector operator - (const Vector& vec) const {
Vector v(x - vec.x, y - vec.y);
return v;
}
Vector operator * (const Vector& vec) const {
Vector v(x * vec.x, y * vec.y);
return v;
}
Vector operator * (double f) const {
Vector v(x * f, y * f);
return v;
}
Vector operator / (const Vector& vec) const {
Vector v;
if (vec.x == 0.0) {
v.setX(0.0);
} else {
v.setX(x / vec.x);
}
if (vec.y == 0.0) {
v.setY(0.0);
} else {
v.setY(y / vec.y);
}
return v;
}
Vector operator / (double f) const {
Vector v;
if (f == 0.0) {
v.set(0.0, 0.0);
} else {
v.set(x / f, y / f);
}
return v;
}
void operator += (const Vector& vec) {
x += vec.x;
y += vec.y;
}
void operator -= (const Vector& vec) {
x -= vec.x;
y -= vec.y;
}
void operator *= (const Vector& vec) {
x *= vec.x;
y *= vec.y;
}
void operator /= (const Vector& vec) {
if (vec.x == 0.0) {
x = 0.0;
} else {
x /= vec.x;
}
if (vec.y == 0.0) {
y = 0.0;
} else {
y /= vec.y;
}
}
bool operator == (const Vector& vec) const {
if (x == vec.x && y == vec.y) {
return true;
} else {
return false;
}
}
};
class Polygon {
private:
vector<Vector> vertex; // 頂点
public:
// コンストラクタ
Polygon() {}
// 頂点を追加する
void addVertex(const Vector& vec) {
vertex.push_back(vec);
}
// 頂点配列を設定する
void setVertex(const vector<Vector>& src) {
vertex = src;
}
// 面積を計算する
double calculateArea() {
double sum = 0;
for (int i = 0; i < vertex.size(); i++) {
Vector vec1, vec2;
vec1 = vertex[i];
if (i == vertex.size() - 1) {
vec2 = vertex[0];
} else {
vec2 = vertex[i + 1];
}
sum += vec1.cross(vec2);
}
return sum / 2.0;
}
// 凸多角形か
bool isConvex() {
for (int i = 0; i < vertex.size(); i++) {
Vector ver1 = vertex[i];
Vector ver2 = vertex[(i + 1) % vertex.size()];
Vector ver3 = vertex[(i + 2) % vertex.size()];
Vector edge1 = ver2 - ver1;
Vector edge2 = ver3 - ver1;
if (edge1.cross(edge2) < 0) { //
return false;
}
}
return true;
}
};
int main(int argc, const char * argv[]) {
int n;
cin >> n;
Polygon poly;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
Vector v(x, y);
poly.addVertex(v);
}
cout << poly.isConvex() << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);
using Point = complex<double>;
using Polygon = vector<Point>;
inline int sgn(const double &a) { return (a < -EPS ? -1 : (a > EPS ? 1 : 0)); }
inline bool eq(const Point &a, const Point &b) { return abs(a - b) < EPS; }
namespace std {
inline bool operator<(const Point &a, const Point &b) {
if (sgn(a.real() - b.real()))
return sgn(a.real() - b.real()) < 0;
return sgn(a.imag() - b.imag()) < 0;
}
} // namespace std
double dot(const Point &a, const Point &b) { return real(conj(a) * b); }
double det(const Point &a, const Point &b) { return imag(conj(a) * b); }
struct Line {
Point p1, p2;
Line(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
bool operator<(const Line &rhs) const {
if (eq(p2, rhs.p2))
return p1 < rhs.p1;
return p2 < rhs.p2;
}
bool operator==(const Line &rhs) const {
return (eq(p1, rhs.p1) && eq(p2, rhs.p2)) ||
(eq(p1, rhs.p2) && eq(p2, rhs.p1));
}
};
enum CCW {
ONLINE_FRONT = -2,
CLOCKWISE,
ON_SEGMENT,
COUNTER_CLOCKWISE,
ONLINE_BACK
};
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0;
Point b = p2 - p0;
if (sgn(det(a, b)) == 1)
return COUNTER_CLOCKWISE;
if (sgn(det(a, b)) == -1)
return CLOCKWISE;
if (sgn(dot(a, b)) == -1)
return ONLINE_BACK;
if (sgn(norm(b) - norm(a)) == 1)
return ONLINE_FRONT;
return ON_SEGMENT;
}
inline Point curr(const Polygon &p, const int &i){
return p[i % p.size()];
}
inline Point next(const Polygon &p, const int &i){
return p[(i+1) % p.size()];
}
inline Point prev(const Polygon &p, const int &i){
return p[(i+p.size()-1) % p.size()];
}
bool is_convex(const Polygon &poly){
if(poly.size() < 3)return false;
if(poly.size() == 3)return true;
int r = 0;
for(int i=0;i<poly.size();i++){
int c = ccw(prev(poly, i), curr(poly, i), next(poly, i));
if(r == 0 && abs(c) == 1)r = c;
if(c != ONLINE_FRONT && c*r != 1)return false;
}
return true;
}
int main(){
int n;
cin >> n;
Polygon pg;
for(int i=0;i<n;++i){
double x, y;
cin >> x >> y;
Point p = Point(x, y);
pg.push_back(p);
}
cout << (is_convex(pg) ? 1 : 0) << endl;
return 0;
}
|
#include <cstdio>
#include <complex>
#include <iostream>
#include <vector>
#include <utility>
#define X real()
#define Y imag()
#define EPS 1e-10
using namespace std;
typedef complex<double> P;
typedef pair<P, P> L;
typedef vector<P> Poly;
double cross(P a, P b){
return a.X*b.Y-a.Y*b.X;
}
int is_convex(Poly p){
int n = p.size();
if(cross(p[0]-p[n-1],p[n-2]-p[n-1]) < 0) return 0;
for(int i = 1; i < n-1; ++i){
if(cross(p[i+1]-p[i],p[i-1]-p[i]) < 0) return 0;
}
return 1;
}
int main(){
int n;
cin >> n;
double x, y;
Poly p(n);
for(int i = 0; i < n; ++i){
cin >> x >> y;
p[i] = P(x,y);
}
cout << is_convex(p) << endl;
return 0;
} |
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// ------ Classes ------ //
class Point {
public:
long double px, py;
Point() : px(0), py(0) {};
Point(long double px_, long double py_) : px(px_), py(py_) {};
friend bool operator==(const Point& p1, const Point& p2) { return p1.px == p2.px && p1.py == p2.py; }
friend bool operator!=(const Point& p1, const Point& p2) { return p1.px != p2.px || p1.py != p2.py; }
friend bool operator<(const Point& p1, const Point& p2) { return p1.px < p2.px ? true : (p1.px == p2.px && p1.py < p2.py); }
friend bool operator>(const Point& p1, const Point& p2) { return p1.px > p2.px ? true : (p1.px == p2.px && p1.py > p2.py); }
friend bool operator<=(const Point& p1, const Point& p2) { return !(p1 > p2); }
friend bool operator>=(const Point& p1, const Point& p2) { return !(p1 < p2); }
friend Point operator+(const Point& p1, const Point& p2) { return Point(p1.px + p2.px, p1.py + p2.py); }
friend Point operator-(const Point& p1, const Point& p2) { return Point(p1.px - p2.px, p1.py - p2.py); }
friend Point operator*(const Point& p1, long double d) { return Point(p1.px * d, p1.py + d); }
friend Point operator*(long double d, const Point& p1) { return p1 * d; }
friend Point operator/(const Point& p1, long double d) { return Point(p1.px / d, p1.py / d); }
Point& operator+=(const Point& p1) { px += p1.px; py += p1.py; return *this; }
Point& operator-=(const Point& p1) { px -= p1.px; py -= p1.py; return *this; }
Point& operator*=(long double d) { px *= d; py *= d; return *this; }
Point& operator/=(long double d) { px /= d; py /= d; return *this; }
};
// ------ Functions ------ //
long double norm(const Point& a) { return a.px * a.px + a.py * a.py; }
long double abs(const Point& a) { return sqrtl(norm(a)); }
long double dot(const Point& a, const Point& b) { return a.px * b.px + a.py * b.py; }
long double crs(const Point& a, const Point& b) { return a.px * b.py - a.py * b.px; }
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0, b = p2 - p0;
if (crs(a, b) > 1e-10) return 1;
if (crs(a, b) < -1e-10) return -1;
if (dot(a, b) < -1e-10) return 2;
if (norm(a) < norm(b)) return -2;
return 0;
}
bool is_convex(vector<Point> v) {
if (v.size() < 3) return false;
int s = -3;
for (int i = 0; i < v.size(); i++) {
int r = ccw(v[i], v[(i + v.size() - 1) % v.size()], v[(i + 1) % v.size()]);
if (abs(r) == 1 && s == -3) s = r;
if (s * r == -1) return false;
}
return true;
}
// ------ Main ------ //
int n; vector<Point> v;
int main() {
cin >> n; v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i].px >> v[i].py;
cout << (is_convex(v) ? 1 : 0) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " ";} cout<<endl;
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
typedef complex<double> P;
double dot(P a, P b){ //内積
return (a.real() * b.real() + a.imag() * b.imag());
}
double cross(P a, P b){ //外積
return (a.real() * b.imag() - a.imag() * b.real());
}
bool is_orth(P a1, P a2, P b1, P b2){ //垂直か
return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
bool is_para(P a1, P a2, P b1, P b2){ //平行か
return EQ(cross(a1 - a2, b1 - b2), 0.0);
}
bool is_online(P a, P b, P c){ //cが線分上か
return (abs(a-c) + abs(c-b) < abs(a-b) + EPS);
}
bool distance_lp(P a, P b, P c){ //直線abと点cの距離
return abs(cross(b-a, c-a)) / abs(b-a);
}
double norm(P a){
return a.real() * a.real() +a.imag() *a.imag();
}
ll ccw(P p0,P p1,P p2){
P a = p1-p0;
P b = p2-p0;
if(cross(a,b) > EPS) return 1;
if(cross(a,b) < -EPS) return -1;
if(dot(a,b) < -EPS) return 2;
if(abs(a) < abs(b)) return -2;
return 0;
}
bool is_intersect(P p1, P p2, P p3, P p4){
return (ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0 &&
ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0 );
}
P intersect_pos(P a1, P a2, P b1, P b2){ //直線同士の交点
P a = a2 - a1;
P b = b2 - b1;
return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
double getDistanceLP(P a1, P a2, P b){
return abs(cross(a2-a1, b-a1) / abs(a2 - a1));
}
double getDistanceSP(P a1, P a2, P b){
if(dot(a2 - a1, b - a1) < EPS) return abs(b - a1);
if(dot(a1 - a2, b - a2) < EPS) return abs(b - a2);
return getDistanceLP(a1, a2, b);
}
double getDistance(P a1, P a2, P b1, P b2){
if(is_intersect(a1, a2, b1, b2)) return 0.0;
double v1 = min(getDistanceSP(a1, a2, b1), getDistanceSP(a1, a2, b2));
double v2 = min(getDistanceSP(b1, b2, a1), getDistanceSP(b1, b2, a2));
return min(v1,v2);
}
#define printP(P) cout << "(" << P.real() << ", " << P.imag() << ")" << endl;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
double x[110], y[110];
vector<P> p;
rep(i,0,N){
cin >> x[i] >> y[i];
P t(x[i], y[i]);
p.push_back(t);
}
rep(i,0,N){
double sin = cross(p[(i-1+N)%N] - p[i], p[(i+1)%N] - p[i]) * -1;
if(sin < 0){
print(0);
return 0;
}
}
print(1);
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
const double eps = 1e-10;
const double pi = acos(-1);
inline int sgn(double d){if(fabs(d) < eps) return 0;if(d > 0) return 1;return -1;}
inline double rad(double _deg){return _deg / 180.0 * pi;}
inline double deg(double _rad){return _rad / pi * 180.0;}
struct Point{
double x, y;
void read(){scanf("%lf%lf", &x, &y);}
Point():x(0.0), y(0.0){}
Point(double a, double b = 0.0): x(a), y(b){}
Point(const Point& p): x(p.x), y(p.y){}
Point operator + (const Point& a){return {x + a.x, y + a.y};}
Point operator - (const Point& a){return {x - a.x, y - a.y};}
double operator * (const Point& a){return x * a.x + y * a.y;}
double operator ^ (const Point& a){return x * a.y - y * a.x;}
bool operator == (const Point& p){return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;}
bool operator < (const Point &p) const{
if(sgn(x - p.x) == 0)
return sgn(y - p.y) == -1;
return sgn(x - p.x) == -1;
}
double length(){return sqrt(x * x + y * y);}
}typedef Vector;
struct Line{
Point a, b;
void read(){a.read(); b.read();}
Line() : a(0.0, 0.0) , b(0.0, 0.0){}
};
struct Segment{
Point a, b;
void read(){a.read(); b.read();}
Segment() : a(0.0, 0.0) , b(0.0, 0.0){}
Segment(Point _a, Point _b) : a(_a), b(_b){}
};
double Distance(const Point &a, const Point &b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x * x + y * y);
}
Point Projection(Point p, Line l)
{
// 点在直线上的投影点
Vector v1 = l.b - l.a;
Vector v2 = p - l.a;
double k1 = v1 * v2;
double k2 = v1.length();
k1 /= k2 * k2;
return {l.a.x + v1.x * k1, l.a.y+ v1.y * k1};
}
Point Reflection(Point p, Line l)
{
// 点关于直线对称
Point p0 = Projection(p, l);
Vector v0 = p0 - p;
return p0 + v0;
}
int ccw(Point p, Segment sg)
{ // Counter_Clockwise
// p 关于(sg.a -> sg.b)的位置关系
Vector v0 = sg.b - sg.a;
Vector v1 = p - sg.a;
int fcross = sgn(v0 ^ v1);
if(fcross == 1) return 1; // 逆时针方向
if(fcross == -1) return -1; // 顺时针方向
int fdot = sgn(v0 * v1);
if(fdot == -1) return 2; // 反向线段外共线
if(v0.length() < v1.length()) return -2; //正向线段外共线
return 0; //点在线段上
}
bool isParallel(Line l1, Line l2)
{
//没有判断重合
return sgn((l1.b - l1.a) ^ (l2.b - l2.a)) == 0;
}
bool isOrthogonal(Line l1, Line l2)
{
//正交
return sgn((l1.b - l1.a) * (l2.b - l2.a)) == 0;
}
bool isIntersection(Segment sg1, Segment sg2)
{
// 判断线段是否有交点
return ccw(sg2.a, sg1) * ccw(sg2.b, sg1) <= 0 && ccw(sg1.a, sg2) * ccw(sg1.b, sg2) <= 0;
}
bool isConvex(vector<Point> p)
{
if(p.size() < 3) return 0;
bool flag1 = 1, flag2 = 1;
int n = p.size();
for(int i = 0; i < p.size(); ++i)
{
if(ccw(p[i], {p[(i + 1) % n], p[(i + 2) % n]}) == -1) flag1 = 0; //逆时针方向
if(ccw(p[i], {p[(i + 1) % n], p[(i + 2) % n]}) == 1) flag2 = 0; //顺时针方向
}
return flag1 || flag2;
}
int main()
{
int n;
vector<Point> p;
scanf("%d", &n);
while(n --)
{
Point _p;
_p.read();
p.push_back(_p);
}
bool ans = isConvex(p);
printf("%d\n", ans);
return 0;
}
|
#include <stdio.h>
int main(void) {
int i, j, n, ans = 1;
scanf("%d", &n);
int x[n], y[n];
scanf("%d%d", &x[0], &y[0]);
scanf("%d%d", &x[1], &y[1]);
for(i = 2; i < n; ++i) {
scanf("%d%d", &x[i], &y[i]);
if((y[i] - y[i - 2]) * (x[i - 1] - x[i - 2]) - (x[i] - x[i - 2]) * (y[i - 1] - y[i - 2]) < 0) ans = 0;
}
if((y[0] - y[n - 2]) * (x[n - 1] - x[n - 2]) - (x[0] - x[n - 2]) * (y[n - 1] - y[n - 2]) < 0) ans = 0;
if((y[1] - y[n - 1]) * (x[0] - x[n - 1]) - (x[1] - x[n - 1]) * (y[0] - y[n - 1]) < 0) ans = 0;
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
/*******************??????********************/
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);
}
struct L : public vector<P> {//??????
L(const P &a, const P &b) {
push_back(a); push_back(b);
}
};
typedef vector<P> G;
struct C {//???
P p; double r;
C(const P &p, double r) : p(p), r(r) { }
};
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;
}
bool intersectLL(const L& l,const L& m){
//non-parallel || sameline
return abs(cross(l[1]-l[0],m[1]-m[0])) > EPS || abs(cross(l[1]-l[0],m[0]-l[0])) < EPS;
}
bool intersectLS(const L& l,const L& s){
// s[0] is left of l,s[1] is right of l
return cross(l[1]-l[0],s[0]-l[0]) * cross(l[1]-l[0],s[1]-l[0]) < EPS;
}
bool intersectSS(const L &s, const L &t) {
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
bool intersectLP(const L& l,const P& p){
return abs(cross(l[1]-p,l[0]-p)) < EPS;
}
bool intersectSP(const L& s,const P& p){
return abs(s[0]-p)+abs(s[1]-p) - abs(s[1]-s[0]) < EPS;// triangle inequality
}
// ??´???????????????
bool orthogonalLL(const L &l1, const L &l2){
return (dot(l1[0]-l1[1], l2[0]-l2[1]) <= EPS && dot(l1[0]-l1[1], l2[0]-l2[1]) >= -EPS);
}
// ??????????????????
bool parallelLL(const L &l1, const L &l2){
return (cross(l1[0]-l1[1], l2[0]-l2[1]) <= EPS && cross(l1[0]-l1[1], l2[0]-l2[1]) >= -EPS);
}
// p??????l?????????????????????????????????
P projection(const L& l,const P& p){
double t = dot(p-l[0],l[0]-l[1]) / norm(l[0]-l[1]);
return l[0] + t*(l[0]-l[1]);
}
// l???????§°?????¨????????????p??¨???????§°?????????????????????
P reflection(const L& l,const P& p){
return p + 2. * (projection(l,p) - p);
}
double distancePP(const P &a, const P &b){
return abs(a - b);
}
double distanceSP(const L &s, const P &p) {
const P r = projection(s, p);
if (intersectSP(s, r)) return abs(r - p);
return min(abs(s[0] - p), abs(s[1] - p));
}
double distanceLP(const L& l,const P& p){
return abs(p - projection(l,p));
}
double distanceLL(const L& l,const L& m){
return intersectLL(l,m) ? 0 : distanceLP(l,m[0]);
}
double distanceLS(const L& l,const L& s){
if(intersectLS(l,s)) return 0;
return min(distanceLP(l, s[0]),distanceLP(l, s[1]));
}
double distanceSS(const L &s, const L &t) {
if (intersectSS(s, t)) return 0;
return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}
// ??????
P crosspointLL(const L &l, const L &m) {
double A = cross(l[1] - l[0], m[1] - m[0]);
double B = cross(l[1] - l[0], l[1] - m[0]);
if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line
if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
return m[0] + B / A * (m[1] - m[0]);
}
// ????§???¢?????¢???
double area(const G &g){
double A = 0.0;
for(int i=0;i<g.size();i++)
A += cross(g[i],g[(i+1)%g.size()]);
return A / 2.0;
}
// ?????????
bool isconvex(const G &g){
for(int i=0;i<g.size();i++){
if(ccw(g[(i-1+g.size())%g.size()], g[i], g[(i+1)%g.size()]) == -1)return false;
}
return true;
}
// convex full
G convex_full(G &ps){
int n = ps.size(), k = 0;
sort(ps.begin(), ps.end());
G ch(2*n);
for (int i = 0; i < n; ch[k++] = ps[i++])//lower-hull
while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) == -1) --k;
for (int i = n-2, t = k+1; i >= 0; ch[k++] = ps[i--])//upper-hull
while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) == -1) --k;
ch.resize(k-1);
return ch;
}
bool is_convex(G &g){
G cf = convex_full(g);
if(cf.size() == g.size())return true;
return false;
}
int main(void){
int n;
cin >> n;
G g(n);
for(int i=0;i<n;i++){
int x,y;
cin >> x >> y;
g[i] = P(x,y);
}
if(isconvex(g))cout << 1 << endl;
else cout << 0 << endl;
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
template <class T> void readVal(vector<T>& vec, int idx) {
T input;
std::cin >> input;
vec[idx] = input;
}
int main()
{
int size;
cin >> size;
vector<int> x(size);
vector<int> y(size);
for (int i = 0; i < size; i++) {
readVal(x, i);
readVal(y, i);
}
int ans = 1;
int dir(0), vecx(0), vecy(0), new_vecx(0), new_vecy(0);
for (int i = 0; i <= size; i++) {
new_vecx = x[(i + 1) % size] - x[i % size];
new_vecy = y[(i + 1) % size] - y[i % size];
int cross_prod = (vecx * new_vecy - vecy * new_vecx);
if (cross_prod != 0) {
if (cross_prod * dir >= 0) {
dir = cross_prod > 0 ? 1 : -1;
}
else {
ans = 0;
break;
}
}
vecx = new_vecx;
vecy = new_vecy;
}
cout << ans << "\n";
return 0;
}
|
#include<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<cctype>
#include<cmath>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 1010
#define M 1000100
int sgn(double x){
if(fabs(x) < eps)
return 0;
if(x < 0)
return -1;
return 1;
}
int n;
struct Point{
double x, y;
Point(){};
Point(double _x, double _y){
x = _x;
y = _y;
}
bool operator < (const Point &A) const{
if(x == A.x)
return y < A.y;
return x < A.x;
}
bool operator == (Point A) const{
return sgn(x-A.x) == 0 && sgn(y-A.y) == 0;
}
Point operator - (const Point &A) const{
return Point(x-A.x, y-A.y);
}
double operator * (const Point &A) const{
return x*A.x + y*A.y;
}
double operator ^ (const Point &A) const{
return x*A.y - y*A.x;
}
double distance(Point A){
return sqrt((x-A.x)*(x-A.x) + (y-A.y)*(y-A.y));
}
}p[N], sta[N];
int cnt;
double xmul(Point p0, Point p1, Point p2){
return (p1-p0)^(p2-p0);
}
bool convex_hull(){
map<int, int> dir;
for(int i = 0; i < n; i++){
int tmp = sgn(xmul(p[i+1], p[i], p[i+2]));
dir[tmp]++;
if(dir[1] && dir[-1])
return false;
}
return true;
}
int main(){
while(~scanf("%d", &n)){
for(int i = 1; i <= n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
p[0].x = p[n].x; p[0].y = p[n].y;
p[n+1].x = p[1].x; p[n+1].y = p[1].y;
if(convex_hull())
printf("1\n");
else
printf("0\n");
}
return 0;
}
|
#ifndef ___Geometry_Library
#define ___Geometry_Library
// ------ Includes ------ //
#include <cmath>
#include <vector>
#include <complex>
#include <utility>
#include <algorithm>
// ------ Defines ------ //
typedef long double GType;
typedef std::complex<GType> Point;
typedef std::pair<Point, Point> Segment;
// ------ Constants ------ //
const GType EPS = 1.0e-10L;
// ------ Functions Level 1 ------ //
GType dot(const Point& p1, const Point& p2) {
return (std::conj(p1) * p2).real();
}
GType cross(const Point& p1, const Point& p2) {
return (std::conj(p1) * p2).imag();
}
int ccw(const Point& p1, const Point& p2, const Point& p3) {
Point v1 = p2 - p1, v2 = p3 - p1;
if (cross(v1, v2) > EPS) return +1;
if (cross(v1, v2) < -EPS) return -1;
if (dot(v1, v2) < -EPS) return +2;
if (std::norm(v1) < std::norm(v2)) return -2;
return 0;
}
// ------ Functions Level 3 ------ //
bool is_convex(std::vector<Point> v) {
if (v.size() < 3) return false;
int s = -3;
for (int i = 0; i < v.size(); i++) {
int r = ccw(v[i], v[(i != 0 ? i - 1 : v.size() - 1)], v[(i + 1 != v.size() ? i + 1 : 0)]);
if (abs(r) == 1 && s == -3) s = r;
if (s * r == -1) return false;
}
return true;
}
#endif
#include <iostream>
using namespace std;
int n, x, y; vector<Point> v;
int main() {
cin >> n; v.resize(n);
for (int i = 0; i < n; i++) {
cin >> x >> y;
v[i] = Point(x, y);
}
cout << (int)(is_convex(v)) << endl;
return 0;
} |
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#include<complex>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef complex<double> P;
typedef vector<P> G;
struct L : public vector<P> {
L(const P &a, const P &b) {
push_back(a); push_back(b);
}
};
P pin(){
double x,y;
cin>>x>>y;
P p(x,y);
return p;
}
void PIN(P* a,int n){
rep(i,n)a[i]=pin();
}
double dot(P a,P b){
return real(conj(a)*b);
}
double cross(P a,P b){
return imag(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;
}
P projection(L a,P p){
double t=dot(p-a[0],a[0]-a[1])/norm(a[0]-a[1]);
return a[0]+t*(a[0]-a[1]);
}
P reflection(L a,P p){
return p+2.0*(projection(a,p)-p);
}
bool intersectLL(const L &l, const L &m) {
return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel
abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line
}
bool intersectLS(const L &l, const L &s) {
return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l
cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l
}
bool intersectLP(const L &l, const P &p) {
return abs(cross(l[1]-p, l[0]-p)) < EPS;
}
bool intersectSS(const L &s, const L &t) {
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
bool intersectSP(const L &s, const P &p) {
return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
P intersect_ls(L a,L b){
double t1=abs(cross(a[1]-a[0],b[0]-a[0]));
double t2=abs(cross(a[1]-a[0],b[1]-a[0]));
return b[0]+(b[1]-b[0])*t1/(t1+t2);
}
double distanceLP(const L &l, const P &p) {
return abs(p - projection(l, p));
}
double distanceLL(const L &l, const L &m) {
return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}
double distanceLS(const L &l, const L &s) {
if (intersectLS(l, s)) return 0;
return min(distanceLP(l, s[0]), distanceLP(l, s[1]));
}
double distanceSP(const L &s, const P &p) {
const P r = projection(s, p);
if (intersectSP(s, r)) return abs(r - p);
return min(abs(s[0] - p), abs(s[1] - p));
}
double distanceSS(const L &s, const L &t) {
if (intersectSS(s, t)) return 0;
return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}
double area(const G &g){
double S =0;
for(int i =0;i <g.size();i++){
S +=(cross(g[i],g[(i+1)%g.size()]));
}
return abs(S/2.0);
}
bool inconvex(const G &g){
int n=g.size();
rep(i,n)if(ccw(g[(i+n-1)%n],g[i%n],g[(i+1)%n])==-1)return false;
return true;
}
int main(){
int n;
cin>>n;
G g(n);
PIN(&g[0],n);
cout<<inconvex(g)<<endl;
} |
#include <cstdio>
#include <iostream>
#include <complex>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cassert>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) c.begin(),c.end()
#define fs first
#define sc second
#define pb push_back
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P,P> L; //fs->sc
typedef vector<P> Pol;
struct C{P p;D r;};
D inf=1e50,eps=1e-10;
//template<class T> bool eq(T a, T b) { return abs(a-b)<eps;}
//template<class T> int sig(T a) { return eq(a,0) ? 0 : (a>0 ? 1 : -1);}
bool eq(D a, D b) { return abs(a-b)<eps;}
bool eq(P a, P b) { return abs(a-b)<eps;}
int sig(D a) { return eq(a,0) ? 0 : (a>0 ? 1 : -1);}
bool operator < (const P& l, const P& r){ //sort x -> y
return eq(l.real(),r.real()) ? l.imag()<r.imag() : l.real() < r.real();
}
bool compxy (const P& l, const P& r){ //sort x -> y
return eq(l.real(),r.real()) ? l.imag()<r.imag() : l.real() < r.real();
}
inline D dot(P a, P b) { return real(conj(a)*b);};
inline D cro(P a, P b) { return imag(conj(a)*b);};
enum ENCCW{CCW=1, CW=-1, FRONT=-2, BACK=2, ON=0}; //!!MAEHARAと一緒(convが書きやすい(次の点を取る条件をccw>0とかける))
//ON優先(including endpoint)
inline int ccw (P a, P b, P c){
// cout << cro(b-a,c-a) << endl;
if(sig(cro(b-a,c-a))==1) return CCW;
if(sig(cro(b-a,c-a))==-1) return CW;
if(eq(abs(a-c)+abs(c-b),abs(a-b))) return ON;
if(eq(abs(a-b)+abs(b-c),abs(a-c))) return FRONT;
if(eq(abs(c-a)+abs(a-b),abs(c-b))) return BACK;
assert(false);
}
inline P proj(P a, P b){ //ベクトルaのbへの射影
return (dot(a,b)/norm(b))*b;
}
inline P perp(L l, P p){ //垂線の足
D t=dot(p-l.fs,l.fs-l.sc)/norm(l.fs-l.sc);
return l.fs+t*(l.fs-l.sc);
}
inline P refl(L l, P p){
return p+2.0*(perp(l,p)-p);
}
inline bool ispal(L a, L b){
return sig(cro(a.fs-a.sc,b.fs-b.sc))==0;
}
inline bool ovLL(L a, L b){
return ispal(a,b) && sig(cro(a.fs-a.sc,b.fs-a.sc))==0;
}
inline bool iLL(L a, L b){ //intersect or overload
return !ispal(a,b) || ovLL(a,b);
}
inline bool iLS(L l, L s){ //intersect(including endpoint) or overload
return cro(l.sc-l.fs,s.fs-l.fs)*cro(l.sc-l.fs,s.sc-l.fs)<eps;
}
inline bool iLP(L l, P p){ //on line
return sig(cro(l.sc-p,l.fs-p));
}
inline bool iSS(L a, L b){ //intersect(including endpoint) or overload
return ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0 && ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0;
}
inline bool iSP(L s, P p){ //intersect(including endpoint) or overload
return ccw(s.fs,s.sc,p)==ON;
}
inline D dLP(L l, P p) { return abs(perp(l,p)-p);}
inline D dLL(L a, L b) { return iLL(a,b) ? 0 : dLP(a,b.fs);}
inline D dLS(L l, L s) { return iLS(l,s) ? 0 : min(dLP(l,s.fs),dLP(l,s.sc));}
inline D dSP(L s, P p) {
P q=perp(s,p);
return iSP(s,q) ? abs(p-q) : min(abs(p-s.fs),abs(p-s.sc));
}
inline D dSS(L a, L b) {
if(iSS(a,b)) return 0;
return min(min(dSP(a,b.fs),dSP(a,b.sc)),min(dSP(b,a.fs),dSP(b,a.sc)));
}
inline P intLL(L a, L b) { //intersection
assert(!ispal(a,b));
D t=cro(a.sc-a.fs,a.sc-b.fs)/cro(a.sc-a.fs,b.sc-b.fs);
return b.fs+t*(b.sc-b.fs);
}
enum ENICP{INC=1,ONC=0,OUTC=-1};
inline int iCP(C c, P p){
D d=abs(p-c.p);
return eq(d,c.r) ? ONC : (d<c.r ? INC : OUTC);
}
inline int iCL(C c, L l){ //num of intersection(s)
D d=dLP(l,c.p);
return eq(d,c.r) ? 1 : (d<c.r ? 2 : 0);
}
//inline int iCS
//inline int iCC(C a, C b){
/*inline P intCL1(C c, L l){ //iCL=1
assert(iCL(c,l)==1);
}
inline L intCL2(C c, L l){ //iCL=2
assert(iCL(c,l)==2);
}*/
D aTri(P a, P b, P c){ return cro(b-a,c-a)/2;}
D aPol(Pol p){ //点集合はCCWに与える
int n=p.size();
D ret=0;
rep(i,n) ret+=cro(p[i],p[(i+1)%n])/2;
return ret;
}
P gPol(Pol p){ //多角形内部が一様な重さを持つときの重心
int n=p.size();
P g;
D s=aPol(p);
assert(s>eps);
rep(i,n){
D ds=cro(p[i],p[(i+1)%n])/2;
g+=ds/3*(p[i]+p[(i+1)%n]);
}
return g/s;
}
enum ENCONT{INP=1,ONP=0,OUTP=-1};
int contain(Pol pol, P p){
bool in=false;
rep(i,pol.size()){
P a=pol[i],b=pol[(i+1)%pol.size()];
if(ccw(a,b,p)==ON) return ONP;
if(!iLS(L(p,p+1.),L(a,b))) continue;
if(ovLL(L(p,p+1.),L(a,b))){
if(sig(max(real(a),real(b))-real(p))>=0) in=!in;
continue;
}
if(sig(real(intLL(L(p,p+1.),L(a,b)))-real(p))>=0) in=!in;
}
return in ? INP : OUTP;
}
inline D heron(D a, D b, D c){
double s=(a+b+c)/2;
if(s-a<eps || s-b<eps || s-c<eps) return 0; //S=0 || 三角形できない
return sqrt(s*(s-a)*(s-b)*(s-c));
}
inline Pol conv(Pol p){ //convex
int n=p.size(),k=0;
assert(n>=3);
sort(all(p),compxy);
// if(p[0]<p[1]) ;
Pol ret(2*n);
rep(i,n){
while(k>=2 && ccw(ret[k-2],ret[k-1],p[i])<=0) --k;
ret[k++]=p[i];
}
for(int i=n-2,t=k+1;i>=0;i--){
while(k>=t && ccw(ret[k-2],ret[k-1],p[i])<=0) --k;
ret[k++]=p[i];
}
ret.resize(k-1);
return ret;
}
inline Pol convall(Pol p){ //conv上の点全部
int n=p.size(),k=0;
assert(n>=3);
sort(all(p),compxy);
// if(p[0]<p[1]) ;
Pol ret(2*n);
rep(i,n){
while(k>=2 && ccw(ret[k-2],ret[k-1],p[i])==-1) --k;
ret[k++]=p[i];
}
for(int i=n-2,t=k+1;i>=0;i--){
while(k>=t && ccw(ret[k-2],ret[k-1],p[i])==-1) --k;
ret[k++]=p[i];
}
ret.resize(k-1);
sort(all(ret),compxy);
ret.erase(unique(all(ret)),ret.end());
return ret;
}
int main(){
int n;
scanf("%d",&n);
Pol pol;
rep(i,n){
D x,y;
scanf("%lf%lf",&x,&y);
pol.push_back(P(x,y));
}
/* Pol p=convall(pol);
cout << "convall\n";
rep(i,p.size()) cout << p[i] << endl;
p=conv(pol);
cout << "conv\n";
rep(i,p.size()) cout << p[i] << endl;*/
printf("%d\n",convall(pol).size()==n ? 1 : 0);
return 0;
} |
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#define f first
#define s second
#define mp make_pair
#define eps (1e-11)
#define equals(a,b) (fabs((a)-(b))<eps)
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 k){ return Point(x*k,y*k);}
Point operator/(double k){ return Point(x/k,y/k);}
double abs(){ return sqrt(norm());}
double norm(){ return (x*x+y*y);}
};
typedef Point Vector;
typedef vector<Point> Polygon;
class Segment{
public:
Point p1,p2;
Segment(Point p1,Point p2):p1(p1),p2(p2){}
};
typedef Segment Line;
class Circle{
public:
Point c;
double r;
Circle(Point c=Point(),double r=0.0):c(c),r(r){}
};
double norm(Vector a){ return (a.x*a.x+a.y*a.y);}
double abs(Vector a){ return sqrt(norm(a));}
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);}
Point project(Segment s,Point p){
Vector base=(s.p2-s.p1);
double r=(dot(p-s.p1,base)/base.norm());
return (s.p1+base*r);
}
Point reflect(Segment s,Point p){
return p+(project(s,p)-p)*2;
}
bool isOrthogonal(Vector a,Vector b){
return equals(dot(a,b),0.0);
}
bool isParallel(Vector a,Vector b){
return equals(cross(a,b),0.0);
}
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;
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;
}
bool intersect(Point p1,Point p2,Point p3,Point p4){
return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&
ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);
}
bool intersect(Segment s1,Segment s2){
return intersect(s1.p1,s1.p2,s2.p1,s2.p2);
}
Point getCrossPoint(Segment s1,Segment s2){
Vector base=s2.p2-s2.p1;
double d1=abs(cross(base,s1.p1-s2.p1));
double d2=abs(cross(base,s1.p2-s2.p1));
double t=d1/(d1+d2);
return (s1.p1+(s1.p2-s1.p1)*t);
}
double getDistanceLP(Line l,Point p){
return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));
}
double getDistanceSP(Segment s,Point p){
if(dot(s.p2-s.p1,p-s.p1)<0.0)return abs(p-s.p1);
if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
return getDistanceLP(s,p);
}
double getDistance(Segment s1,Segment s2){
if(intersect(s1,s2))return 0.0;
return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}
pair<Point,Point> getCrossPoints(Circle c,Line l){
Vector pr=project(l,c.c);
Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);
double base=sqrt(c.r*c.r-norm(pr-c.c));
return mp(pr+e*base,pr-e*base);
}
double arg(Vector p){ return atan2(p.y,p.x);}
Vector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}
pair<Point,Point> getCrossPoints(Circle c1,Circle c2){
double d=abs(c1.c-c2.c);
double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
double t=arg(c2.c-c1.c);
return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));
}
double getPolygonArea(Polygon p){
double area=0.0;
for(int i=0;i<p.size();i++)area+=cross(p[i%p.size()],p[(i+1)%p.size()]);
return area/2;
}
bool isConvex(Polygon p){
for(int i=0;i<p.size();i++){
if(ccw(p[(i+1)%p.size()],p[i%p.size()],p[(i+2)%p.size()])==1)return false;
}
return true;
}
int main()
{
int n;
Polygon p;
cin>>n;
p.resize(n);
for(int i=0;i<n;i++)cin>>p[i].x>>p[i].y;
if(isConvex(p))cout<<1<<endl;
else cout<<0<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef complex<double> Point;
const double epx = 1e-10;
bool notNorm(Point a,Point b)
{
return imag(b*conj(a))<-epx;
}
bool isConvex(vector<Point> points,int sz)
{
if(sz<3) return false;
points.push_back(points[0]);
points.push_back(points[1]);
for(int i=1;i<=sz;i++)
{
if(notNorm(points[i+1]-points[i],points[i-1]-points[i])) return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector<Point> points;
for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
points.emplace_back(x,y);
}
cout<<isConvex(points,n)<<endl;
return 0;
}
|
#include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.00000000001
#define prid(x) printf("%.15lf\n", x)
struct vec{
double x, y;
vec operator+(const vec& a) const { return (vec){x + a.x, y + a.y}; }
vec operator-(const vec& a) const { return (vec){x - a.x, y - a.y}; }
vec sca(double t){ return (vec){t * x, t * y}; }
double dot(vec a){ return x * a.x + y * a.y; }
double cross(vec a){ return x * a.y - y * a.x; }
double norm(){ return sqrt(x * x + y * y); }
double norm2(){ return x * x + y * y; }
double ppdist(vec p){ return sqrt( (p.x - x) * (p.x - x) + (p.y - y) * (p.y - y) ); }
double ppdist2(vec p){ return (p.x - x) * (p.x - x) + (p.y - y) * (p.y - y); }
};
struct line{
vec a, b;
vec getvec(){ return b - a; }
vec proj(vec p){
vec pa = p - a, ba = b - a;
double t = pa.dot(ba) / ba.dot(ba);
return a + ba.sca(t);
}
vec vref(vec p){ return proj(p).sca(2.0) - p; }
int ccw(vec p){
vec q = p - a, ba = b - a;
if(ba.cross(q) > 0)return 1; //ccw
if(ba.cross(q) < 0)return -1; //cw
if(ba.dot(q) < 0)return -2; //back
if(ba.dot(ba) < q.dot(q))return 2; //front
return 0; //on
}
bool paral(line l){
double res = abs(l.getvec().cross(getvec()));
return res < eps;
}
bool orth(line l){
double res = abs(l.getvec().dot(getvec()));
return res < eps;
}
bool intersec(line l){
bool res0 = (ccw(l.a) * ccw(l.b) == 4); //syukutai
bool res1 = (getvec().cross(l.a - a) * getvec().cross(l.b - a)) <= eps;
bool res2 = (l.getvec().cross(a - l.a) * l.getvec().cross(b - l.a)) <= eps;
return !res0 && res1 && res2;
}
vec crosspoint(line l){
double t = (l.a - a).cross(l.getvec()) / getvec().cross(l.getvec());
return a + getvec().sca(t);
}
double pldist(vec p){
double res = min((a - p).norm2(), (b - p).norm2());
vec h = proj(p);
if((a - h).dot(b - h) < 0)res = min(res, (h - p).norm2());
return sqrt(res);
}
double lldist(line l){
if(intersec(l))return 0.0;
return min(min(pldist(l.a), pldist(l.b)), min(l.pldist(a), l.pldist(b)));
}
};
struct polygon{
vector<vec> p;
double area(){
double res = 0.0;
for(int i = 0;i < p.size();++i)res += p[i].cross(p[(i + 1) % p.size()]);
return res / 2.0;
}
bool isconv(){
for(int i = 0;i < p.size();++i)
if((p[(i + 1) % p.size()] - p[i]).cross(p[(i + 2) % p.size()] - p[i]) < -eps)return false;
return true;
}
};
signed main(void){
int i, j, k;
polygon A;
int(n);
for(;n--;){
double x, y; cin >> x >> y;
A.p.pb((vec){x, y});
}
puts(A.isconv() ? "1" : "0");
return 0;
} |
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
int dcmp(double x) {
static double eps = 1e-8;
return (x > eps) - (x < -eps);
}
struct vec{
double x, y;
vec(double _x=0, double _y=0)
:x(_x), y(_y) {}
vec operator- () const{ return vec(-x, -y); }
vec operator+ (const vec& v) const{ return vec(x+v.x, y+v.y); }
vec operator- (const vec& v) const{ return vec(x-v.x, y-v.y); }
vec operator* (double c) const{ return vec(x*c, y*c); }
vec operator/ (double c) const{ return vec(x/c, y/c); }
double dot(const vec& v) const{ return x*v.x + y*v.y; }
double cross(const vec& v) const{ return x*v.y - y*v.x; }
double len2() const{ return x*x + y*y; }
double len() const{ return sqrt(len2()); }
vec& norm() { return *this = *this / len(); }
vec& rot(double rad) {
return *this = vec(x*cos(rad) - y*sin(rad), x*sin(rad) + y*cos(rad));
}
bool operator< (const vec& v) const{
return dcmp(x-v.x) < 0 || (dcmp(x-v.x)==0 && dcmp(y-v.y) < 0);
}
};
struct line{
vec u, v; // u + x*v
line() {}
line(vec _u, vec _v)
:u(_u), v(_v) {}
vec get(double x) {
return u + v * x;
}
};
line makeline(vec a, vec b) {
return line(a, b - a);
}
bool on_left(line l, vec u) {
return dcmp(l.v.cross(u - l.u)) > 0;
}
vec line_intersection(line a, line b) {
return b.u + b.v * b.v.cross(b.u-a.u) / a.v.cross(b.v);
}
bool segment_intersection(vec a, vec b, vec c, vec d) {
line x = line(a, b-a), y = line(c, d-c);
return on_left(x, c) != on_left(x, d) && on_left(y, a) != on_left(y, b);
}
bool is_convex(const std::vector<vec>& p) {
for (size_t i = 0, j = 1, k; j < p.size(); i++, j++) {
k = (j + 1) % p.size();
if ((p[j] - p[i]).cross(p[k] - p[j]) < 0)
return false;
}
return true;
}
std::vector<vec> convex_hull(std::vector<vec> p) {
std::sort(p.begin(), p.end());
std::vector<vec> ch;
for (size_t i = 0; i < p.size(); ++i) {
while (ch.size() > 1 && on_left(makeline(ch[ch.size()-2], p[i]), ch[ch.size()-1]))
ch.pop_back();
ch.push_back(p[i]);
}
size_t k = ch.size();
for (int i = p.size() - 2; i >= 0; --i) {
while (ch.size() > k && on_left(makeline(ch[ch.size()-2], p[i]), ch[ch.size()-1]))
ch.pop_back();
ch.push_back(p[i]);
}
ch.pop_back();
return ch;
}
std::pair<vec, vec> convex_diameter(const std::vector<vec> &p) {
int xmax = 0, xmin = 0, n = p.size();
for (int i = 1; i < n; ++i) {
if (p[i].x > p[xmax].x) xmax = i;
if (p[i].x < p[xmin].x) xmin = i;
}
int i = xmax, j = xmin;
int u = xmax, v = xmin;
double ans = (p[xmax] - p[xmin]).len2();
do {
if ((p[i] - p[(i + 1) % n]).cross(p[j] - p[(j + 1) % n]) < 0) i = (i + 1) % n;
else j = (j + 1) % n;
double dist = (p[i] - p[j]).len2();
if (dist > ans) {
ans = dist;
u = i; v = j;
}
}while(i != xmax || j != xmin);
return std::make_pair(p[u], p[v]);
}
double polygon_area(const std::vector<vec>& v) {
double area = 0;
for (size_t i = 0; i < v.size(); ++i)
area += v[i].cross(v[(i + 1) % v.size()]);
return area * 0.5;
}
int relation(const std::vector<vec>& p, const vec& v) {
int ret = -1, n = p.size();
for (int i = 0; i < n; ++i) {
vec a = p[i] - v, b = p[(i + 1) % n] - v;
if (a.y > b.y) std::swap(a, b);
if (a.y <= 0 && 0 < b.y && a.cross(b) < 0)
ret = -ret;
if (a.cross(b) == 0 && a.dot(b) <= 0) return 0;
}
return ret; // 1 - contain, -1 - out
}
// end template
int main() {
std::vector<vec> p;
int n;
double x, y;
for(scanf("%d",&n); n--; p.push_back(vec(x, y)))
scanf("%lf%lf",&x,&y);
puts(is_convex(p) ? "1": "0");
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-10;
int sign(double x)
{
if(fabs(x)<=eps)return 0;
if(x>0)return 1;
return -1;
}
struct Point{
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 k){return Point(x*k,y*k);}
Point operator / (double k){return Point(x/k,y/k);}
double norm(){return x*x+y*y;}
double abs(){return sqrt(norm());}
};
typedef Point Vector;
typedef vector<Point> polygon;
int n;
Point p;
polygon g;
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 a,Point b,Point c)
{
if(sign(cross(b-a,c-a))==-1)return 1;
return -1;
}
int main()
{
scanf("%d",&n);
while(n--){
scanf("%lf%lf",&p.x,&p.y);
g.push_back(p);
}
g.push_back(g[0]);
for(int i=0;i<g.size()-2;i++){
if(ccw(g[i],g[i+1],g[i+2])==1){
printf("0\n");
return 0;
}
}
printf("1\n");
return 0;
}
|
#include<bits/stdc++.h>
#define mset(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const ll maxn=1e4+200;
const int inf=0x3f3f3f3f;
const double EPS=1e-10;
//typedef pair<int,int> PA;
double add(double a,double b){//考虑误差的加法运算
if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
return a+b;
}
int sgn(double x){
if(abs(x)<EPS) return 0;
if(x<0) return -1;
else return 1;
}
class Point{
public:
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){
}
Point operator+ (Point p){
return Point(add(x,p.x),add(y,p.y));
}
Point operator -(Point p){
return Point(add(x,-p.x),add(y,-p.y));
}
Point operator *(double d){
return Point(x*d,y*d);
}
double operator *(Point p){
return add(x*p.x,y*p.y);//外积
}
double operator ^(Point p){//内积
return add(x*p.y,-y*p.x);
}
double det(Point p){
return add(x*p.y,-y*p.x);
}
double len(){
return sqrt(add(x*x,y*y));
}
};
class Line{
public:
Point s,e;
Line(){}
Line(Point s,Point e):s(s),e(e){}
};
bool onSge(Line l,Point q){
return ((l.s-q)^(l.e-q))==0&&((l.s-q)*(l.e-q))<=0;
}
Point operator *(double d,Point p){
return Point(p.x*d,p.y*d);
}
Point getProject(Point p1,Point p2,Point q){//q 在直线p1,p2上的投影坐标
double d1=1.0/(p2-p1).len();
Point ans=(q-p1)*(p2-p1)*(p2-p1)*d1*d1;
return ans+p1;
}
Point getReflect(Point p1,Point p2,Point q){//q 关于直线p1 p2的 反射坐标
return 2.0*getProject(p1,p2,q)-q;
}
double getDisPP(Point a,Point b){//两点之间距离
return (b-a).len();
}
double getDisPL(Line l,Point p){//点到直线距离
Point x=getProject(l.s,l.e,p);
return (p-x).len();
}
double getDisPS(Line l,Point p){//点到线段最短距离
Point x=getProject(l.s,l.e,p);
if(onSge(l,x))
return (x-p).len();
else
return min((p-l.s).len(),(p-l.e).len());
}
double getDisSS(Line la,Line lb){//保证两线段不相交 isInterSS==false
return min(min(getDisPS(la,lb.s),getDisPS(la,lb.e)),
min(getDisPS(lb,la.s),getDisPS(lb,la.e)));
}
int getDirPPP(Point p,Point p1,Point p2){
return sgn((p1-p)^(p2-p));//1代表 p2在p1的逆时针方向 0共线 ,-1是p2在p1的顺时针方向
}
int getStatePP(Point p,Point p1,Point p2){//返回 p2 关于p 在p1的什么方向
int a=sgn((p1-p)^(p2-p));
int b=sgn((p1-p)*(p2-p));
if(a>0){//
puts("COUNTER_CLOCKWISE");
}
else if(a<0){
puts("CLOCKWISE");
}
else if(b<0){
puts("ONLINE_BACK");
}
else if(b>0&&sgn((p1-p)*(p2-p1))>0){
puts("ONLINE_FRONT");
}
else{
puts("ON_SEGMENT");
}
return 0;
}
bool isInterSS(Line la,Line lb){//线段是否想交
int d1=getDirPPP(lb.s,lb.e,la.s);
int d2=getDirPPP(lb.s,lb.e,la.e);
int d3=getDirPPP(la.s,la.e,lb.s);
int d4=getDirPPP(la.s,la.e,lb.e);
if(d1*d2<0&&d3*d4<0)
return true;
else if(d1==0&&onSge(lb,la.s))
return true;
else if(d2==0&&onSge(lb,la.e))
return true;
else if(d3==0&&onSge(la,lb.s))
return true;
else if(d4==0&&onSge(la,lb.e))
return true;
else
return false;
}
int getStateLL(Line la,Line lb){//两个直线 垂直,相交或者平行
if(sgn((la.e-la.s)^(lb.e-lb.s))==0){
return 2;//平行
}//两向量平行 内积为0
else if(sgn((la.e-la.s)*(lb.e-lb.s))==0){
return 1;//垂直
}
else return 0;
}
typedef Point P;
Point getInterLL(Point p1,Point p2,Point q1, Point q2){//得到直线交点
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}
double getAreaPolygon(Point p[],int n){//下标从0开始 计算多边形面积
double res=0.0;
for(int i=0;i<n;++i)
res+=(p[i]^p[(i+1)%n])/2;
return res;
}
Point PA[110000];
bool _cmp(Point p1,Point p2)//相对于pa[0]的
{
double tmp=(p1-PA[0])^(p2-PA[0]);
if(sgn(tmp)>0) return true;
else if(sgn(tmp)==0 &&sgn((PA[0]-p1).len()-(PA[0]-p2).len())<=0)
return true;
else return false;
}
void sortPolarAngle(Point PA[],int n)//对数组进行极角排序 点将逆时针排序
{
Point p0=PA[0];
int k=0;
/* 找到最下边的一个点 靠左的一个点*/
for(int i=1;i<n;++i){
if((p0.y>PA[i].y)||(p0.y==PA[i].y&&p0.x>PA[i].x))
{
p0=PA[i];
k=i;
}
}
swap(PA[k],PA[0]);
sort(PA+1,PA+n,_cmp);
return ;
}
//判断是否为凸包
//允许共线边
//点可以是顺时针给出也可以是逆时针给出
//点的编号为0~n-1
bool isConvex(Point poly[],int n)
{
bool s[3];
memset(s,false,sizeof(s));
for(int i=0;i<n;++i){
s[sgn( (poly[(i+1)%n]-poly[i]) ^ (poly[(i+2)%n]-poly[i]) )+1]=true;
if(s[0]&&s[2]) return false;
}
return true;
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lf%lf",&PA[i].x,&PA[i].y);
}
// sortPolarAngle(PA,n);
bool ans=isConvex(PA,n);
printf("%d\n",(int)ans);
return 0;
}
//double getDistanceSP(Segment s,Point p){
// if(dot(calc_minus(s.p2,s.p1),calc_minus(p,s.p1)) < 0.0)return calc_len(calc_minus(p,s.p1));
// if(dot(calc_minus(s.p1,s.p2),calc_minus(p,s.p2)) < 0.0)return calc_len(calc_minus(p,s.p2));
// return getDistanceLP(s,p);
//}
|
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
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; }
using CP = complex<long double>;
const long double EPS = 1e-10; // 許容する誤差ε
#define EQ(a, b) (abs((a)-(b)) < EPS) // 2つのスカラーが等しいかどうか
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) // 2つのベクトルが等しいかどうか
// double length = abs(a); // ベクトルaの絶対値
// double distance = abs(a-b); // 2点a,b間の距離
// CP b = a/abs(a); // ベクトルaの単位ベクトル
// CP n1 = a*CP(0,+1); CP n2 = a*CP(0,-1); // ベクトルaの法線ベクトル
// CP un1 = (a*CP(0,+1)/abs(a)); CP un2 = (a*CP(0,-1)/abs(a)); // ベクトルaの単位法線ベクトル
// 内積(dot product) : a・b = |a||b|cosΘ
long double dot(CP a, CP b) {
return (a.real()*b.real() + a.imag()*b.imag());
}
// 外積(cross product) : a×b = |a||b|sinΘ
long double cross(CP a, CP b) {
return (a.real()*b.imag() - a.imag()*b.real());
}
// 2直線の直交判定 : a⊥b ⇔ dot(a,b) = 0
int is_orthogonal(CP a1, CP a2, CP b1, CP b2) {
return EQ(dot(a1-a2, b1-b2), 0.0);
}
// 2直線の平行判定 : a//b ⇔ cross(a,b) = 0
int is_parallel(CP a1, CP a2, CP b1, CP b2) {
return EQ(cross(a1-a2, b1-b2), 0.0);
}
// 点cが直線a,b上にあるかないか
int is_point_on_line(CP a, CP b, CP c) {
return EQ(cross(b-a, c-a), 0.0);
}
// 点cが線分a,b上にあるかないか
int is_point_on_lines(CP a, CP b, CP c) {
// |a-c|+|c-b| <= |a-b|なら線分上
return (abs(a-c)+abs(c-b) < abs(a-b)+EPS);
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
int is_intersected_lines(CP a1, CP a2, CP b1, CP b2) {
if(is_parallel(a1, a2, b1, b2)) {
// 平行なので線分の重なり判定
return is_point_on_lines(a1, a2, b1) || is_point_on_lines(a1, a2, b2) ||
is_point_on_lines(b1, b2, a1) || is_point_on_lines(b1, b2, a2);
}
return (cross(a2-a1, b1-a1)*cross(a2-a1, b2-a1) < EPS) &&
(cross(b2-b1, a1-b1)*cross(b2-b1, a2-b1) < EPS);
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算
CP intersection_lines(CP a1, CP a2, CP b1, CP b2) {
CP b = b2-b1;
long double d1 = abs(cross(b, a1-b1));
long double d2 = abs(cross(b, a2-b1));
long double t = d1/(d1+d2);
return a1+(a2-a1)*t;
}
// a1,a2を通る直線とb1,b2を通る直線の交差判定
int is_intersected_line(CP a1, CP a2, CP b1, CP b2) {
return !EQ(cross(a1-a2, b1-b2), 0.0);
}
// a1,a2を通る直線とb1,b2を通る直線の交点計算(平行ではない前提)
CP intersection_line(CP a1, CP a2, CP b1, CP b2) {
CP a = a2-a1; CP b = b2-b1;
return a1 + a*cross(b, b1-a1)/cross(b, a);
}
// 点a,bを通る直線と点cとの距離
long double distance_line_p(CP a, CP b, CP c) {
return abs(cross(b-a, c-a))/abs(b-a);
}
// 点a,bを端点とする線分と点cとの距離
long double distance_lines_p(CP a, CP b, CP c) {
if(dot(b-a, c-a) < EPS) return abs(c-a);
if(dot(a-b, c-b) < EPS) return abs(c-b);
return abs(cross(b-a, c-a))/abs(b-a);
}
// 点a1,a2を端点とする線分と点b1,b2を端点とする線分の最短距離
long double distance_lines_lines(CP a1, CP a2, CP b1, CP b2) {
long double res = 1e18;
if(is_intersected_lines(a1, a2, b1, b2)) return 0.0L;
res = min(res, distance_lines_p(a1, a2, b1));
res = min(res, distance_lines_p(a1, a2, b2));
res = min(res, distance_lines_p(b1, b2, a1));
res = min(res, distance_lines_p(b1, b2, a2));
return res;
}
// s,tを通る直線に対する点pの射影
CP projection(CP s, CP t, CP p) {
if(EQV(s, t)) return s;
CP base = t-s;
return s + base*(dot(p-s, base)/norm(base));
}
// s,tを通る直線に対する点pの反射
CP reflection(CP s, CP t, CP p) {
CP tmp = projection(s, t, p) - p;
return p + tmp*2.0L;
}
// n多角形の面積計算
long double polygon_area(const vector<CP> &v) {
int n = v.size();
long double res = 0;
for(int i = 0; i < n; ++i) {
res += cross(v[(i+n-1)%n], v[(i+n)%n]);
}
return fabsl(res)/2.0L;
}
// n多角形の凸性判定
int is_convex(const vector<CP> &v) {
int n = v.size();
for(int i = 0; i < n; ++i) {
if(cross(v[(i+1)%n]-v[i], v[(i+2)%n]-v[(i+1)%n]) < -EPS) return 0;
}
return 1;
}
// 3点の位置関係を判定
int calc_clockwise(CP p0, CP p1, CP p2) {
CP x = p1-p0, y = p2-p0;
if(cross(x, y) > EPS) return 1; // "COUNTER_CLOCKWISE"
if(cross(x, y) < -EPS) return -1; // "CLOCKWISE"
if(dot(x, y) < 0) return 2; // "ONLINE_BACK"
if(norm(x) < norm(y)) return -2; // "ONLINE_FRONT"
return 0; // "ON_SEGMENT"
}
int main() {
int n; cin >> n;
vector<CP> point;
for(int i = 0; i < n; ++i) {
long double a, b; cin >> a >> b;
point.emplace_back(CP(a, b));
}
cout << is_convex(point) << endl;
}
|
#include <iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const double EPS=1e-10;
bool equals(double a,double b)
{
return a>b?a-b<EPS:b-a<EPS;
}
class Point
{
public:
double x;
double y;
Point(double inx=0.0,double iny=0.0):x(inx),y(iny){}
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 double k){return Point(x*k,y*k);}
Point operator/(const double k){return Point(x/k,y/k);}
Point operator=(const Point &p){x=p.x;y=p.y;return *this;}
bool operator<(const Point &p)const{return x!=p.x?x<p.x:y<p.y;}
bool operator==(const Point &p)const{return equals(x,p.x)&&equals(y,p.y);}
double norm()
{
return x*x+y*y;
}
double abs()
{
return sqrt(norm());
}
};
typedef Point Vector;
class Segment
{
public:
Point p1;
Point p2;
Vector getvector()
{
return p2-p1;
}
};
typedef Segment Line;
class Circle
{
public:
Point c;
double r;
Circle(Point inc=Point(),double inr=0.0):c(inc),r(inr){}
};
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;
}
const int counter_clockwise=1;
const int clockwise=-1;
const int on_segment=0;
const int online_back=2;
const int online_front=-2;
double norm(Vector a)
{
return a.x*a.x+a.y*a.y;
}
double abs(Vector a)
{
return sqrt(norm(a));
}
bool isorthogonal(Vector a,Vector b)
{
return equals(dot(a,b),0.0);
}
bool isorthogonal(Point a1,Point a2,Point b1,Point b2)
{
return isorthogonal(a1-a2,b1-b2);
}
bool isorthogonal(Segment s1,Segment s2)
{
return equals(dot(s1.getvector(),s2.getvector()),0.0);
}
bool isparallel(Vector a,Vector b)
{
return equals(cross(a,b),0.0);
}
bool isparallel(Point a1,Point a2,Point b1,Point b2)
{
return isparallel(a1-a2,b1-b2);
}
bool isparallel(Segment s1,Segment s2)
{
return equals(cross(s1.getvector(),s2.getvector()),0.0);
}
Point project(Segment s,Point p)
{
Vector base=s.getvector();
double r=dot(p-s.p1,base)/norm(base);
return s.p1+base*r;
}
Point reflect(Segment s,Point p)
{
return project(s,p)*2.0-p;//p+(project(s,p)-p)*2.0
}
double getdistance(Point a,Point b)
{
return abs(a-b);
}
double getdistancelp(Line l,Point p)//Line,Point
{
double s=abs(cross(l.p2-l.p1,p-l.p1));
return s/abs(l.getvector());
}
double getdistancesp(Segment s,Point p)
{
if(dot(s.getvector(),p-s.p1)<0.0)return abs(p-s.p1);
else if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
else return getdistancelp(s,p);
}
int ccw(Point p0,Point p1,Point p2)
{
Vector a=p1-p0;
Vector b=p2-p0;
if(cross(a,b)>EPS)return counter_clockwise;
else if(cross(a,b)<-EPS)return clockwise;
else if(dot(a,b)<-EPS)return online_back;
else if(a.norm()<b.norm())return online_front;
else return on_segment;
}
bool intersect(Point p1,Point p2,Point p3,Point p4)
{
return ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0;
}
bool intersect(Segment s1,Segment s2)
{
return intersect(s1.p1,s1.p2,s2.p1,s2.p2);
}
double getdistance(Segment s1,Segment s2)
{
if(intersect(s1,s2))return 0.0;
else return min(min(getdistancesp(s1,s2.p1),getdistancesp(s1,s2.p2)),
min(getdistancesp(s2,s1.p1),getdistancesp(s2,s1.p2)));
}
Point getcrosspoint(Segment s1,Segment s2)
{
Vector base=s2.getvector();
double d1=abs(cross(base,s1.p1-s2.p1));
double d2=abs(cross(base,s1.p2-s2.p1));
double t=d1/(d1+d2);
return s1.p1+s1.getvector()*t;
}
pair<Point,Point> getcrosspoint(Circle c,Line l)
{
Vector pr=project(l,c.c);
Vector e=l.getvector()/abs(l.getvector());
double base=sqrt(c.r*c.r-norm(pr-c.c));
return make_pair(pr+e*base,pr-e*base);
}
double arg(Vector p)
{
return atan2(p.y,p.x);
}
Point polar(double r,double theta)
{
return Point(r*cos(theta),r*sin(theta));
}
pair<Point,Point> getcrosspoint(Circle c1,Circle c2)
{
double d=abs(c2.c-c1.c);
double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));
double t=arg(c2.c-c1.c);
return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));
}
Polygon andrewscan(Polygon s)
{
Polygon u,l;
if(s.size()<=2)return s;
sort(s.begin(),s.end(),[](const Point &p1,const Point &p2){return p1.y==p2.y?p1.x<p2.x:p1.y<p2.y;});
u.push_back(s[0]);
u.push_back(s[1]);
l.push_back(s[s.size()-1]);
l.push_back(s[s.size()-2]);
for(int i=2;i<s.size();i++)
{
for(int n=u.size();n>=2&&ccw(u[n-2],u[n-1],s[i])!=clockwise&&ccw(u[n-2],u[n-1],s[i])!=online_front;n--)
{
u.pop_back();
}
u.push_back(s[i]);
}
for(int i=s.size()-3;i>=0;i--)
{
for(int n=l.size();n>=2&&ccw(l[n-2],l[n-1],s[i])!=clockwise&&ccw(l[n-2],l[n-1],s[i])!=online_front;n--)
{
l.pop_back();
}
l.push_back(s[i]);
}
reverse(l.begin(),l.end());
for(int i=u.size()-2;i>=1;i--)l.push_back(u[i]);
return l;
}
const int in=2;
const int on=1;
const int out=0;
int contain(Polygon g,Point p)
{
int n=g.size();
bool x=false;
Point a,b,sto;
for(int i=0;i<n;i++)
{
a=g[i]-p;
b=g[(i+1)%n]-p;
if(abs(cross(a,b))<EPS&&dot(a,b)<EPS)return on;
if(a.y>b.y)
{
sto=a;
a=b;
b=sto;
}
if(a.y<EPS&&EPS<b.y&&cross(a,b)>EPS)x=!x;
}
return x?in:out;
}
bool isconvex(Polygon p)
{
int n=p.size(),now;
for(int i=0;i<n;i++)
{
now=ccw(p[i],p[(i+1)%n],p[(i+2)%n]);
if(now==clockwise)return false;
}
return true;
}
int main() {
Polygon p;
int n;cin>>n;
Point a;
for(int i=0;i<n;i++)
{
cin>>a.x>>a.y;
p.push_back(a);
}
if(isconvex(p))cout<<1<<endl;
else cout<<0<<endl;
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1<<30
#define LINF 1LL<<60
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-9, pi = acos(-1.0);
namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
if (lhs.real() < rhs.real() - eps) return true;
if (lhs.real() > rhs.real() + eps) return false;
return lhs.imag() < rhs.imag();
}
}
Point input_point() {ld x, y; cin >> x >> y; return Point(x, y);} // ????????\???
bool eq(ld a, ld b) {return (abs(a - b) < eps);} // ????????????????????????
ld dot(Point a, Point b) {return real(conj(a) * b);} // ??????
ld cross(Point a, Point b) {return imag(conj(a) * b);} // ??????
// ??´????????????
class Line {
public:
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
Point operator[](const int _num) {
if (_num == 0)return a;
else if (_num == 1)return b;
else assert(false);
}
};
// ????????????
class Circle {
public:
Point p;
ld r;
Circle() : p(Point(0, 0)), r(0) {}
Circle(Point p, ld r) : p(p), r(r) {}
};
// CCW
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross(b, c) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶
if (cross(b, c) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶
if (dot(b, c) < 0) return 2; // c,a,b???????????´???????????¶
if (norm(b) < norm(c)) return -2; // a,b,c???????????´???????????¶
return 0; // a,c,b???????????´???????????¶
}
/* ????§???¢ */
typedef vector<Point> Polygon;
// ?????§??????
bool isConvex(const Polygon &poly){
int p_size = (int)poly.size();
if(p_size < 3) return false;
int s = -3;
for(int i = 0; i < p_size;i++){
int r = ccw(poly[(i+p_size-1)%p_size],poly[i%p_size],poly[(i+1)%p_size]);
if(r == 1 && s == -3) s = r;
if(s*r == -1) return false;
}
return true;
}
int main(void) {
cin.tie(0); ios::sync_with_stdio(false);
int n; cin >> n;
Polygon ps;
for(int i = 0; i < n;i++) ps.emplace_back(input_point());
if(isConvex(ps)){
cout << 1 << endl;
}else{
cout << 0 << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef complex<double> P;
typedef vector<P> G;
#define here(g, i) g[i]
#define next(g, i) g[(i + 1) % g.size()]
#define prev(g, i) g[(i - 1 + g.size()) % g.size()]
const double EPS = 1e-10;
const double INF = 1e12;
struct L {
P a, b, v;
L(){}
L(P _a, P _b) : a(_a), b(_b), v(b - a) {}
L(double _ax, double _ay, double _bx, double _by) : L(P(_ax, _ay), P(_bx, _by)) {}
};
double cross(P a, P b) {
return imag(conj(a) * b);
}
double dot(P a, P b) {
return real(conj(a) * b);
}
int ccw(P p0, P p1, P p2) {
if (cross(p1 - p0, p2 - p0) > 0) return +1; // counter-clockwise
if (cross(p1 - p0, p2 - p0) < 0) return -1; // clockwise
if (dot(p1 - p0, p2 - p0) < 0) return +2; // online_back
if (dot(p0 - p1, p2 - p1) < 0) return -2; // online_front
return 0; // on_segment
}
bool isConvex(G g) {
int n = g.size();
rep(i, n) {
if (ccw(prev(g, i), here(g, i), next(g, i)) == -1) {
return false;
}
}
return true;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
G g;
while (n--) {
double x, y;
cin >> x >> y;
g.emplace_back(x, y);
}
cout << isConvex(g) << endl;
} |
#include<bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
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;
template<class T>
bool equals(T a,T b){ return fabs((a)-(b)) < eps; }
struct Point{
double x, y;
Point(){}
Point(double x, double y) : 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(x*a, y*a); }
Point operator / (double a) { return Point(x/a, y/a); }
double norm() {return x*x+y*y;}
double abs() {return sqrt(norm());}
bool operator < (const Point &p) const {
return x!=p.x ? x<p.x : y<p.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;
}
};
istream &operator >> (istream &is, Point &p){
is>>p.x>>p.y;
return is;
}
ostream &operator << (ostream &os, Point p){
os<<p.x<<" "<<p.y;
return os;
}
bool sort_x(Point a, Point b){ return !equals(a.x,b.x)?a.x<b.x:a.y<b.y; }
bool sort_y(Point a, Point b){ return !equals(a.y,b.y)?a.y<b.y:a.x<b.x; }
typedef Point Vector;
typedef vector<Point> Polygon;
double norm(Vector a) { return a.x*a.x+a.y*a.y; }
double abs(Vector a) { return sqrt(norm(a)); }
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; }
struct Segment {
Point p1, p2;
Segment(){}
Segment(Point p1, Point p2):p1(p1),p2(p2){}
};
typedef Segment Line;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c, double r) : c(c),r(r) {}
};
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;
}
bool intersect(Point p1, Point p2, Point p3, Point p4){
return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);
}
bool intersect(Segment s1, Segment s2){
return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
bool isOrthogonal(Vector a, Vector b){
return equals(dot(a,b), 0.0);
}
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2){
return isOrthogonal(a1-a2, b1-b2);
}
bool isOrthogonal(Segment s1, Segment s2){
return equals(dot(s1.p2-s1.p1, s2.p2-s2.p1),0.0);
}
bool isParallel(Vector a, Vector b){
return equals(cross(a,b), 0.0);
}
bool isParallel(Point a1, Point a2, Point b1, Point b2){
return isParallel(a1-a2, b1-b2);
}
bool isParallel(Segment s1, Segment s2){
return equals(cross(s1.p2-s1.p1, s2.p2-s2.p1),0.0);
}
Point project(Segment s, Point p){
Vector base=s.p2-s.p1;
double r=dot(p-s.p1,base)/norm(base);
return s.p1+base*r;
}
Point reflect(Segment s,Point p){
return p+(project(s,p)-p)*2.0;
}
double getDistance(Point a, Point b){
return abs(a-b);
}
double getDistanceLP(Line l, Point p){
return abs(cross(l.p2-l.p1, p-l.p1))/abs(l.p2-l.p1);
}
double getDistanceSP(Segment s, Point p){
if(dot(s.p2-s.p1,p-s.p1) < 0.0) return abs(p-s.p1);
if(dot(s.p1-s.p2,p-s.p2) < 0.0) return abs(p-s.p2);
return getDistanceLP(s,p);
}
double getDistanceSS(Segment s1, Segment s2){
if(intersect(s1,s2)) return 0.0;
return min( min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}
Point getCrossPoint(Segment s1, Segment s2){
Vector base=s2.p2-s2.p1;
double d1=abs(cross(base, s1.p1-s2.p1));
double d2=abs(cross(base, s1.p2-s2.p1));
double t=d1/(d1+d2);
return s1.p1+(s1.p2-s1.p1)*t;
}
pair<Point, Point> getCrossPoints(Circle c, Line l){
Vector pr=project(l,c.c);
Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);
double base=sqrt(c.r*c.r-norm(pr-c.c));
return make_pair(pr+e*base, pr-e*base);
}
bool isConvex(Polygon s){
ll n=s.size();
rep(i,n){
if(ccw(s[(i+n-1)%n],s[i],s[(i+1)%n])==CLOCKWISE) return false;
}
return true;
}
double PolygonArea(Polygon s){
double res=0.0;
rep(i,s.size()){
res+=cross(s[i],s[(i+1)%s.size()])/2.0;
}
return abs(res);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n;cin>>n;
Polygon p(n);
rep(i,n) cin>>p[i];
cout<<isConvex(p)<<endl;
return 0;
}
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
#include<set>
#include<map>
#define re register
#define ll long long
using namespace std;
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9'|| ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return sum*f;
}
const double eps=1e-10,Pi=acos(-1.0);
struct node{
double x,y;
node operator+(node b){return (node){x+b.x,y+b.y};}
node operator-(node b){return (node){x-b.x,y-b.y};}
node operator*(double b){return (node){x*b,y*b};}
double len(){return sqrt(x*x+y*y);}
};
int fh(double a){return fabs(a)<eps?0:a>eps?1:-1;}
double cross(node a,node b){return a.x*b.y-a.y*b.x;}
double dot(node a,node b){return a.x*b.x+a.y*b.y;}
node projection(node p1,node p2,node p){node v=p2-p1;return p1+v*(dot(v,p-p1)/dot(v,v));}
node reflection(node p1,node p2,node p){node P=projection(p1,p2,p);P=P*2-p;return P;}
void Counter_Clockwise(node p1,node p2,node p){
if(cross(p2-p1,p-p1)>eps){puts("COUNTER_CLOCKWISE");return;}
if(cross(p2-p1,p-p1)<-eps){puts("CLOCKWISE");return;}
if(dot(p2-p1,p-p1)<-eps){puts("ONLINE_BACK");return;}
if(dot(p2-p1,p2-p1)>=dot(p-p1,p2-p1)){puts("ON_SEGMENT");return;}
puts("ONLINE_FRONT");
}
bool insegment(node A,node B,node p){
if(fh(cross(B-A,p-A)))return false;
return ((fh(p.x-A.x)>=0 && fh(B.x-p.x)>=0) || (fh(p.x-A.x)<=0 && fh(B.x-p.x)<=0)) && ((fh(p.y-A.y)>=0 && fh(B.y-p.y)>=0) || (fh(p.y-A.y)<=0 && fh(B.y-p.y)<=0));
}
int parallel(node A1,node A2,node B1,node B2){
if(!fh(cross(B2-B1,A2-A1)))return 2;
if(!fh(dot(B2-B1,A2-A1)))return 1;
return 0;
}
bool intersection(node A1,node A2,node B1,node B2){
if(!fh(cross(B2-B1,A2-A1))){
if(insegment(A1,A2,B1) || insegment(A1,A2,B2) || insegment(B1,B2,A1) || insegment(B1,B2,A2))return true;
return false;
}
return fh(cross(A1-B1,B2-B1)*cross(A2-B1,B2-B1))<=0 && (fh(cross(B1-A1,A2-A1)*cross(B2-A1,A2-A1))<=0);
}
node getintersection(node A1,node A2,node B1,node B2){
node v=A2-A1;
return A1+v*(cross(B2-B1,B1-A1)/cross(B2-B1,A2-A1));
}
double distance(node A1,node A2,node B1,node B2){
if(intersection(A1,A2,B1,B2))return 0.0;
node p;double ans=1e18;
ans=min(ans,(A1-B1).len());ans=min(ans,(A1-B2).len());ans=min(ans,(A2-B1).len());ans=min(ans,(A2-B2).len());
p=projection(A1,A2,B1);
if(insegment(A1,A2,p))ans=min(ans,(B1-p).len());
p=projection(A1,A2,B2);
if(insegment(A1,A2,p))ans=min(ans,(B2-p).len());
p=projection(B1,B2,A1);
if(insegment(B1,B2,p))ans=min(ans,(A1-p).len());
p=projection(B1,B2,A2);
if(insegment(B1,B2,p))ans=min(ans,(A2-p).len());
return ans;
}
double area(node *p,int n){
double area=0;
for(int i=1;i<n;i++)area+=cross(p[i]-p[1],p[i+1]-p[1]);
area+=cross(p[n]-p[1],p[1]-p[1]);
return area*0.5;
}
bool is_convex(node *p,int n){
for(int i=2;i<n;i++)if(cross(p[i]-p[i-1],p[i+1]-p[i-1])<0)return false;
if(cross(p[n]-p[n-1],p[1]-p[n-1])<0)return false;
if(cross(p[1]-p[n],p[2]-p[n])<0)return false;
return true;
}
node p[110];
int main(){
int n=gi();
for(int i=1;i<=n;i++)p[i].x=gi(),p[i].y=gi();
if(is_convex(p,n))puts("1");
else puts("0");
return 0;
}
|
#include <complex>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cassert>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i,a,b) for(int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define curr(P, i) P[(i) % P.size()]
#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;
struct L : public vector<P> {
L(const P &a, const P &b) {
push_back(a); push_back(b);
}
};
typedef vector<P> Polygon;
namespace std {
bool operator < (const P& a, const P& b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
}
// ??????
double dot(const P& a, const P& b) {
return real(conj(a)*b);
}
// ????¬?????????????2??????
// not verified
double norm(const P& a) {
return real(conj(a)*a);
}
// ??????
double cross(const P& a, const P& b) {
return imag(conj(a)*b);
}
// ???p2?????????p0, p1???????????´???????°???±?????????
P projection(const P& p0, const P& p1, const P& p2) {
// ???????????????????????´???
P vec1 = p1 - p0;
P vec2 = p2 - p0;
return p0 + vec1 * dot(vec1, vec2) / dot(vec1, vec1);
}
// projection?????\??¨???
// not verified
P projection(const L &l, const P &p) {
double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
return l[0] + t*(l[0]-l[1]);
}
// ???p0, p1???????????´??????????§°?????¨????????????p2??¨????§°??????
P reflection(const P& p0, const P& p1, const P& p2) {
P proj = projection(p0, p1, p2);
return proj - (p2 - proj);
}
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; // a--c--b on line
}
// 2??´?????????????????????
bool is_parallel(const L& l1, const L& l2) {
P p1 = l1[1] - l1[0];
P p2 = l2[1] - l2[0];
auto res = ccw(p1, p2, P(0,0));
return (res == 2 || res == -2 || res == 0);
}
// 2??´???????????´?????????
bool is_ortho(const L& l1, const L& l2) {
P p1 = l1[1] - l1[0];
P p2 = l2[1] - l2[0];
return dot(p1, p2) == 0;
}
// l: line(??´???)
// s: segment(??????)
// not verified
bool intersectLL(const L &l, const L &m) {
return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel
abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line
}
// not verified
bool intersectLS(const L &l, const L &s) {
return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l
cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l
}
// not verified
bool intersectLP(const L &l, const P &p) {
return abs(cross(l[1]-p, l[0]-p)) < EPS;
}
bool intersectSS(const L &s, const L &t) {
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
// not verified
bool intersectSP(const L &s, const P &p) {
return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
// not verified
double distanceLP(const L &l, const P &p) {
return abs(p - projection(l, p));
}
// not verified
double distanceLL(const L &l, const L &m) {
return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}
// not verified
double distanceLS(const L &l, const L &s) {
if (intersectLS(l, s)) return 0;
return min(distanceLP(l, s[0]), distanceLP(l, s[1]));
}
double distanceSP(const L &s, const P &p) {
const P r = projection(s, p);
if (intersectSP(s, r)) return abs(r - p);
return min(abs(s[0] - p), abs(s[1] - p));
}
double distanceSS(const L &s, const L &t) {
if (intersectSS(s, t)) return 0;
return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}
// 2??´????????????
P crosspoint(const L &l, const L &m) {
double A = cross(l[1] - l[0], m[1] - m[0]);
double B = cross(l[1] - l[0], l[1] - m[0]);
if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line
if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
return m[0] + B / A * (m[1] - m[0]);
}
// ????§???¢?????¢???????±???????
// P???????????¨???????????????
double area(const Polygon& poly) {
double area = 0.0;
REP(i, SIZE(poly))
area += cross(curr(poly, i), next(poly, i));
return abs(area) * 0.5;
}
// ??????????±???????
// ????????????????¨?????????§????????????
Polygon convexHull(const Polygon& poly) {
Polygon ps = poly;
int n = ps.size(), k = 0;
sort(ALL(ps));
Polygon ch(2*n);
for (int i = 0; i < n; ch[k++] = ps[i++]) // lower-hull
while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;
for (int i = n-2, t = k+1; i >= 0; ch[k++] = ps[i--]) // upper-hull
while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;
ch.resize(k-1);
return ch;
}
// ?????§??????
// poly?????????????¨???¨?????§?????????????´?????????????????????¨???????????????
// ???????????´??????????????¶???????¨±???
// not fully verified
bool isConvex(const Polygon &poly) {
REP(i, SIZE(poly)) {
if (ccw(prev(poly, i), curr(poly, i), next(poly, i)) == -1)
return false;
}
return true;
}
int main(){
int N;
cin >> N;
Polygon poly;
REP(n, N) {
double x, y;
cin >> x >> y;
poly.pb( P(x,y) );
}
cout << isConvex(poly) << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>
#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <complex>
using namespace std;
typedef complex<double> xy_t;
xy_t P[30];
int N;
double x,y;
int aa=1;
int main() {
cin >> N;
double s =0.0;
for(int i = 0;i < N;i++) {
cin >> x >> y;
P[i] = xy_t(x,y);
}
for ( int i = 0; i < N;i++ ){
xy_t a = P[(i+1)%N] - P[i], b = P[(i+2)%N]-P[(i+1)%N];
if((a.real()*b.imag()-a.imag()*b.real())<0){
aa=0; break;
}
}
printf("%d\n", aa);
} |
#include <complex>
#include <vector>
#include <cstdio>
using namespace std;
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;
#define curr(P, i) P[(i+1) % P.size()]
#define next(P, i) P[(i+2) % P.size()]
#define prev(P, i) P[(i) % P.size()]
bool isconvex(const G &P){
for(int i = 0; i < P.size(); ++i){
int d=ccw(prev(P, i), curr(P, i), next(P, i));
if(d==-1) return false;
}
return true;
}
int main(){
G poly;
P p;
int n;
double x,y;
for(scanf("%d",&n);n--;poly.push_back(p))scanf("%lf%lf",&x,&y),p=P(x,y);
printf("%d\n",isconvex(poly));
} |
#include <bits/stdc++.h>
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 MP make_pair
typedef double db;
const db EPS = 1e-8;
inline int sign(db a) {
return a < -EPS ? -1 : a > EPS;
}
inline int cmp(db a, db b){
return sign(a-b);
}
struct P {
db x, y;
P() {
}
P(db _x, db _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*(db d) {
return P(x * d, y * d);
}
P operator/(db d) {
return P(x / d, y / d);
}
bool operator<(P p) const {
int c = sign(x - p.x);
if (c)
return c == -1;
return sign(y - p.y) == -1;
}
db dot(P p) {
return x * p.x + y * p.y;
}
db det(P p) {
return x * p.y - y * p.x;
}
db distTo(P p) { return (*this-p).abs(); }
db alpha() {
return atan2(y, x);
}
void read() {
cin>>x>>y;
}
db abs() {
return sqrt(abs2());
}
db abs2() {
return x * x + y * y;
}
P rot90(){
return P(-y,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) sign(cross(p1,p2,p3))
P isLL(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 !( cmp(r1,l2) == -1 || cmp(r2,l1) == -1 );
}
bool isSS(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 isMiddle(db a, db m, db b) {
return sign(a - m) == 0 || sign(b - m) == 0 || (a < m != b < m);
}
bool isMiddle(P a, P m, P b) {
return isMiddle(a.x, m.x, b.x) && isMiddle(a.y, m.y, b.y);
}
bool onSegment(P p1, P p2, P q){
return crossOp(p1,p2,q) == 0 && isMiddle(p1, q, p2);
}
P proj(P p1, P p2, P q) {
P dir = p2 - p1;
return p1 + dir * (dir.dot(q - p1) / dir.abs2());
}
P reflect(P p1, P p2, P q){
return proj(p1,p2,q) * 2 - q;
}
db nearest(P p1,P p2,P q){
P h = proj(p1,p2,q);
if(isMiddle(p1,h,p2))
return q.distTo(h);
return min(p1.distTo(q),p2.distTo(q));
}
db disSS(P p1, P p2, P q1, P q2){
if(isSS(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)) );
}
db getrad(P p1,P p2){
return atan2l(p1.det(p2),p1.dot(p2));
}
db incircle(P p1, P p2, P p3){
db A = p1.distTo(p2);
db B = p2.distTo(p3);
db C = p3.distTo(p1);
return sqrtl(A*B*C/(A+B+C));
}
db area(vector<P> ps){
db ret = 0; rep(i,0,ps.size()) ret += ps[i].det(ps[(i+1)%ps.size()]);
return abs(ret/2);
}
int main(){
int n;cin>>n;
vector<P> ps(n);
rep(i,0,n) ps[i].read();
bool ok = 1;
rep(i,0,n) ok &= crossOp(ps[i],ps[(i+1)%n],ps[(i+2)%n])>=0;
printf("%d\n",ok);
return 0;
} |
//be naame khodaa
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef complex<ld> PT;
typedef vector<PT> Poly;
typedef pair<PT, PT> LS;
#define F first
#define S second
#define X real()
#define Y imag()
#define pb push_back
inline int in(){int x, y; y = scanf("%d", &x); return x; }
const int N = -1;
const ld EPS = 1e-12;
const int LEFT = 0, RIGHT = 1, BACK = 2, FRONT = 3, ON = 4;
inline bool Geq(ld a, ld b){ return a + EPS > b; }
inline bool Grt(ld a, ld b){ return a > b + EPS; }
inline bool Leq(ld a, ld b){ return a < b + EPS; }
inline bool Lss(ld a, ld b){ return a + EPS < b; }
inline bool Equ(ld a, ld b){ return Geq(a, b) && Geq(b, a); }
istream& operator>> (istream& is, complex<ld>& p){
ld val;
is >> val;
p.real(val);
is >> val;
p.imag(val);
return is;
}
ld dot(PT a, PT b){ return real(conj(a)*b); }
ld cross(PT a, PT b){ return imag(conj(a)*b); }
ld sqlen(PT a){ return dot(a, a); }
ld len(PT a){ return sqrt(sqlen(a)); }
PT proj(PT a, PT b, PT c){
b -= a, c -= a;
return a + b*real(c/b);
}
PT reflect(PT a, PT b, PT c){
b -= a, c -= a;
return a + conj(c/b)*b;
}
PT rotate(PT a, PT b, ld theta){
return (b-a)*polar<ld>(1, theta) + a;
}
int relpos(PT a, PT b, PT c){
b -= a, c -= a;
c /= b;
if (Grt(c.imag(), 0)) return LEFT;
if (Lss(c.imag(), 0)) return RIGHT;
if (Lss(c.real(), 0)) return BACK;
if (Grt(c.real(), 1)) return FRONT;
return ON;
}
int side(PT a, PT b, PT c){
b -= a, c -= a;
ld cr = (c/b).Y;
return Grt(cr, 0) ? 1 : (Lss(cr, 0) ? -1 : 0);
}
//If LineSegments Intersect
bool intersect(PT a, PT b, PT c, PT d){
int as = side(c, d, a), bs = side(c, d, b), cs = side(a, b, c), ds = side(a, b, d);
if (as && as == bs || cs && cs == ds) return false;
else if (as || bs || cs || ds) return true;
for (int j = 0; j < 2; j++, swap(a, c), swap(b, d)){
ld mx = min(a.X, b.X), Mx = max(a.X, b.X), my = min(a.Y, b.Y), My = max(a.Y, b.Y);
for (int k = 0; k < 2; k++, swap(c, d))
if (Geq(c.X, mx) && Leq(c.X, Mx) && Geq(c.Y, my) && Leq(c.Y, My))
return true;
}
return false;
}
//LineSegments Intersection (intersect is true)
PT intersection(PT a, PT b, PT c, PT d){
ld c1 = cross(b-a, c-a), c2 = cross(b-a, d-a);
return (c1*d - c2*c)/(c1-c2);
}
ld distLSP(PT a, PT b, PT c){
int rpos = relpos(a, b, proj(a, b, c));
if (rpos == BACK) return len(c-a);
if (rpos == FRONT) return len(c-b);
b -= a, c -= a;
return abs(imag(c/b))*len(b);
}
ld distLS(PT a, PT b, PT c, PT d){
if (intersect(a, b, c, d)) return 0;
return min(min(distLSP(a, b, c), distLSP(a, b, d)), min(distLSP(c, d, a), distLSP(c, d, b)));
}
ld signedArea(Poly p){
int n = p.size();
ld res = 0;
for (int i = 0; i < n; i++)
res += cross(p[i], p[(i+1)%n]);
return res/2;
}
ld area(Poly poly){
return abs(signedArea(poly));
}
bool isConvex(Poly p){
int n = p.size();
bool neg = false, pos = false;
for (int i = 0; i < n; i++){
int rpos = relpos(p[i], p[(i+1)%n], p[(i+2)%n]);
if (rpos == LEFT) pos = true;
if (rpos == RIGHT) neg = true;
}
return (neg&pos) == false;
}
int main(){
Poly poly;
cout << setprecision(1) << fixed;
for (int i = in(); i; i--){
PT p;
cin >> p;
poly.pb(p);
}
cout << isConvex(poly) << endl;
return 0;
}
|
#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back
const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
out<<'('<<a.first<<','<<a.second<<')';
return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
out<<'[';
REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
out<<']';
return out;
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const double EPS = 1e-8;
using R = long double;
using P = complex<R>;
using L = pair<P,P>;
using G = vector<P>;
using C = pair<P,R>;
struct S : public L {
S() {}
S(const P &a, const P &b) : L(a,b) {}
};
inline int sgn(const R& r) { return (r>EPS) - (r<-EPS); }
namespace std {
bool operator < (const P& a, const P& b) {
return sgn(real(a-b)) ? real(a-b) < 0 : sgn(imag(a-b)) < 0;
}
bool operator == (const P& a, const P& b) {
return sgn(real(a-b)) == 0 && sgn(imag(a-b)) == 0;
}
}
inline R dot(const P& a, const P& b) {
return real(a)*real(b) + imag(a)*imag(b);
}
inline R det(const P& a, const P& b) {
return real(a)*imag(b) - imag(a)*real(b);
}
inline P vec(const L& l) {return l.second - l.first;}
// P,L,Sについて入力
inline istream& operator>>(istream& is, P& p) {
R x, y;
is >> x >> y;
p = P(x, y);
return is;
}
inline istream& operator>>(istream& is, L& l) {
P a, b;
is >> a >> b;
l = L(a, b);
return is;
}
inline istream& operator>>(istream& is, S& s) {
P a, b;
is >> a >> b;
s = S(a, b);
return is;
}
// 射影
P projection(const L &l, const P &p) {
R t = dot(p-l.first, l.first-l.second) / norm(l.first-l.second);
return l.first + t*(l.first-l.second);
}
// 反射
P reflection(const L &l, const P &p) {
return p + (R)2 * (projection(l, p) - p);
}
// 線分abから見たcの位置
enum CCW{LEFT=1, RIGHT=2, BACK=4, FRONT=8, ON=16};
int ccw(P a, P b, P c) {
P p = (c-a)/(b-a);
if(sgn(imag(p)) > 0) return LEFT;
if(sgn(imag(p)) < 0) return RIGHT;
if(sgn(real(p)) < 0) return BACK;
if(sgn(real(p)-1) > 0) return FRONT;
return ON;
}
// 垂直,平行
inline bool vertical(L a, L b) {return sgn(dot(vec(a), vec(b))) == 0;}
inline bool parallel(L a, L b) {return sgn(det(vec(a), vec(b))) == 0;}
inline bool eal(L a, L b) {return vertical(a,b) && parallel(a,b);}
// 交差判定
template<bool strict=false> inline bool intersect(const L&l1, const L&l2) {
if(strict) return sgn(det(vec(l1),vec(l2))) != 0;
return sgn(det(vec(l1),vec(l2))) != 0 || l1 == l2;
}
template<bool strict=false> inline bool intersect(const L&l, const S&s) {
if(strict) det(s.first, vec(l)) * det(s.second, vec(l)) < 0;
return det(s.first, vec(l)) * det(s.second, vec(l)) <= 0;
}
template<bool strict=false> inline bool intersect(const S&s1, const S&s2) {
int ccw1 = ccw(s1.first, s1.second, s2.first) | ccw(s1.first, s1.second, s2.second);
int ccw2 = ccw(s2.first, s2.second, s1.first) | ccw(s2.first, s2.second, s1.second);
if(strict) return (ccw1 & ccw2) == (LEFT | RIGHT);
return (ccw1 & ccw2) == (LEFT | RIGHT) || ((ccw1 | ccw2) & ON);
}
template<bool strict=false> inline bool intersect(const S&s, const P&p) {
return ccw(s.first, s.second, p) == ON;
}
template<bool strict=false> inline bool intersect(const L&l, const P&p) {
return ccw(l.first, l.second, p) == ON ||
ccw(l.first, l.second, p) == FRONT ||
ccw(l.first, l.second, p) == BACK;
}
// 交点 交差判定を先にすること!!!
inline P crosspoint(const L& l1, const L& l2) {
R ratio = det(vec(l2), l2.first-l1.first)/det(vec(l2),vec(l1));
return l1.first + vec(l1)*ratio;
}
// 距離
R dist(const S& s, const P& p) {
P q = projection(s, p);
if(sgn(dot(s.second-s.first, p-s.first)) <= 0) q = s.first;
if(sgn(dot(s.first-s.second, p-s.second)) <= 0) q = s.second;
return abs(p-q);
}
R dist(const S& a, const S& b) {
if(intersect(a, b)) return 0;
return min({dist(a, b.first), dist(a, b.second), dist(b, a.first), dist(b, a.second)});
}
// 面積 頂点が反時計回りに並んでいること
R area(const G& pol) {
R ret = 0.0;
REP(i, pol.size()) ret += det(pol[i], pol[(i+1)%pol.size()]);
return (ret/2.0);
}
// 凸性の判定 頂点が並んでいること
bool isconvex(const G& pol) {
REP(i, pol.size()) {
if(sgn(det(pol[(i+1)%pol.size()]-pol[i], pol[(i+2)%pol.size()]-pol[i])) < 0) {
return false;
}
}
return true;
}
signed main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
G pol(n);
REP(i, n) cin >> pol[i];
if(isconvex(pol)) cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
|
#include <complex>
#include <cmath>
#include <algorithm>
#include <vector>
template <class T>
inline T sq(T x) { return x * x; }
using Real = long double;
using Point = std::complex<Real>;
using Segment = std::pair<Point, Point>;
using Polygon = std::vector<Point>;
const Real EPS = 1e-10;
const Point origin(0, 0);
// 宇宙船演算子
inline Real compare(Real a, Real b) {
if (std::abs(a - b) < EPS) return 0;
return a - b > 0 ? 1 : -1;
}
inline Real dist(Point a, Point b) {
return std::abs(a - b);
}
inline Real length(Segment s) {
return dist(s.first, s.second);
}
// inner product
inline Real dot(Point x, Point y) {
return std::real(std::conj(x) * (y));
}
// outer product
inline Real cross(Point x, Point y) {
return std::imag(std::conj(x) * (y));
}
// lに対するxの正射影
Point proj(Segment s, Point p) {
Real ratio = dot(s.second - s.first, p - s.first) / sq(length(s));
return s.first + (s.second - s.first) * ratio;
}
// lを軸としてxに対称な点
Point refl(Segment s, Point p) {
Point t = proj(s, p);
return t + (t - p);
}
// lに対するpの位置
// counter clockwise -> 1, clockwise -> -1
// online front -> 2, online back -> -2
// on segment -> 0
int side(Segment s, Point p) {
Real c = cross(s.second - s.first, p - s.first);
if (compare(c, 0) != 0) return compare(c, 0);
Real d = dot(s.second - s.first, p - s.first);
if (compare(d, 0) < 0) return -2;
return (compare(length(Segment(s.first, p)), length(s)) > 0 ? 2 : 0);
}
inline bool isorthogonal(Segment s1, Segment s2) {
return compare(dot(s1.second - s1.first, s2.second - s2.first), 0) == 0;
}
inline bool isparallel(Segment s1, Segment s2) {
return compare(cross(s1.second - s1.first, s2.second - s2.first), 0) == 0;
}
inline bool intersect(Segment s1, Segment s2) {
return (side(s1, s2.first) * side(s1, s2.second) <= 0) &&
(side(s2, s1.first) * side(s2, s1.second) <= 0);
}
Point intersection(Segment s1, Segment s2) {
Real c1 = cross(s2.second - s2.first, s1.second - s1.first);
Real c2 = cross(s2.second - s2.first, s1.first - s2.first);
return s1.first + (s1.second - s1.first) * (-c2 / c1);
}
// distanceのオーバーロード
Real dist(Segment s, Point p) {
Point t = proj(s, p);
if (side(s, t) == 0) return dist(p, t);
return std::min(dist(p, s.first), dist(p, s.second));
}
Real dist(Segment s1, Segment s2) {
if (intersect(s1, s2)) return 0;
return std::min({dist(s1, s2.first),
dist(s1, s2.second),
dist(s2, s1.first),
dist(s2, s1.second)});
}
// 符号付き面積
inline Real triangle(Point a, Point b, Point c) {
return cross(b - a, c - a) / 2;
}
// 時計回りか反時計回りであることを要請
Real area(const Polygon& g) {
Real S = 0;
for (int i = 0; i < g.size(); ++i) {
S += triangle(origin, g[i], g[(i + 1) % g.size()]);
}
return S;
}
bool isconvex(const Polygon& g) {
for (int i = 0; i < g.size(); ++i) {
if (side(Segment(g[i], g[(i + 1) % g.size()]),
g[(i + 2) % g.size()]) == -1) return false;
}
return true;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int N;
cin >> N;
Polygon g(N);
for (auto& p : g) {
Real x, y;
cin >> x >> y;
p = Point(x, y);
}
cout << isconvex(g) << endl;
return 0;
}
|
#include <stdio.h>
#include <stack>
#include <math.h>
using namespace std;
int main(){
int n;
scanf("%d",&n);
double x[n+1];
double y[n+1];
int i;
for(i=0; i<n; i++){
scanf("%lf %lf",&x[i],&y[i]);
}
x[n]=x[0];
y[n]=y[0];
double v_x[n+1];
double v_y[n+1];
for(i=0; i<n; i++){
v_x[i]=x[i+1]-x[i];
v_y[i]=y[i+1]-y[i];
}
v_x[n]=v_x[0];
v_y[n]=v_y[0];
int S=1;
int T;
for(i=0; i<n; i++){
if(v_x[i]*v_y[i+1]-v_x[i+1]*v_y[i] >= 0){
T=1;
} else {
T=0;
}
S = S*T;
}
if(S != 0){
printf("1\n");
} else {
printf("0\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
os<<'[';
rep(i,a.size()) os<<(i?" ":"")<<a[i];
return os<<']';
}
int Signum(double x){
return x<-EPS?-1:x>EPS?1:0;
}
struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
Point& operator+=(Point p){
x+=p.x,y+=p.y;
return *this;
}
Point& operator-=(Point p){
x-=p.x,y-=p.y;
return *this;
}
Point& operator*=(double c){
x*=c,y*=c;
return *this;
}
Point& operator/=(double c){
x/=c,y/=c;
return *this;
}
};
Point operator+(Point a,Point b){
return a+=b;
}
Point operator-(Point a,Point b){
return a-=b;
}
Point operator*(Point a,double c){
return a*=c;
}
Point operator*(double c,Point a){
return a*=c;
}
Point operator/(Point a,double c){
return a/=c;
}
bool operator==(Point a,Point b){
return abs(a.x-b.x)<EPS && abs(a.y-b.y)<EPS;
}
bool operator!=(Point a,Point b){
return !(a==b);
}
double Abs(Point p){
return sqrt(p.x*p.x+p.y*p.y);
}
double Abs2(Point p){
return p.x*p.x+p.y*p.y;
}
double Arg(Point p){
return atan2(p.y,p.x);
}
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;
}
Point Rot(Point p,double t){
return Point(cos(t)*p.x-sin(t)*p.y,sin(t)*p.x+cos(t)*p.y);
}
int CCW(Point a,Point b,Point c){
b-=a,c-=a;
if(int sign=Signum(Cross(b,c)))
return sign; // 1:ccw,-1:cw
if(Dot(b,c)<-EPS)
return -2; // c-a-b
if(Abs2(b)<Abs2(c)-EPS)
return 2; // a-b-c
return 0; // a-c-b (inclusive)
}
bool IsConvex(const vector<Point>& ps)
{
int n=ps.size(),res=true;
rep(i,n) res&=CCW(ps[i],ps[(i+1)%n],ps[(i+2)%n])!=-1;
return res;
}
int main()
{
for(int n;cin>>n && n;){
vector<Point> ps(n);
for(auto& p:ps) cin>>p.x>>p.y;
cout<<IsConvex(ps)<<endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define mkp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
typedef long long ll;
const ll MOD=1e9+7;
const double EPS=1e-9;
const double PI=acos(-1.0);
//const long double PI=acos(-1.0L);
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);}
Point operator *(const double b) const {return Point(x*b,y*b);}
Point operator /(const double b) const {return Point(x/b,y/b);}
};
struct Line{
Point a,b;
Line(){}
Line(Point a,Point b):a(a),b(b){}
};
int sgn(double a) {return (a>EPS)-(a<-EPS);}
int sgn(double a,double b) {return sgn(a-b);}
double dot(const Point &a,const Point &b) {return (a.x*b.x+a.y*b.y);}
double cross(const Point &a,const Point &b) {return (a.x*b.y-a.y*b.x);}
double norm(const Point &p) {return dot(p,p);}
double abs(const Point &p) {return sqrt(norm(p));}
//1:Counter_Clockwise -1:Clockwise
//2:a-b-c -2:c-a-b 0:a-c-b
int ccw(const Point &p,const Point &q){
int s=sgn(cross(p,q));
if(s!=0) return s;
if(dot(p,q)<0) return -2;
if(norm(p)<norm(q)) return 2;
return 0;
}
int ccw(const Point &a,const Point &b,const Point &c){return ccw(b-a,c-a);}
bool isConvex(vector<Point> &g){
int n=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;
vector<Point> g(N);
rep(i,N) cin>>g[i].x>>g[i].y;
if(isConvex(g)) cout<<"1"<<endl;
else cout<<"0"<<endl;
return 0;
}
|
#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
using namespace std;
typedef int32_t i32;typedef int64_t i64;typedef i64 ll;typedef uint32_t uint;typedef uint64_t ull;
#define ALL(c) (begin(c)),(end(c))
#define REP(i,n) FOR(i,0,n)
#define REPr(i,n) FORr(i,0,n)
#define FOR(i,l,r) for(int i=(int)(l);i<(int)(r);++i)
#define FORr(i,l,r) for(int i=(int)(r)-1;i>=(int)(l);--i)
#define EACH(it,o) for(__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))
//debug
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) <<endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x)<<endl
#define LINE() cerr<< " (L" << __LINE__ << ")"
#define LINELN() LINE()<<endl
#define CHECK(exp,act) if(exp!=act){DUMPLN(exp);DEBUGLN(act);}
#define STOP(e) CHECK(e,true);if(!(e)) exit(1);
class range {
private:
struct Iter{
int v;
int operator*(){return v;}
bool operator!=(Iter& itr) {return v < itr.v;}
void operator++() {++v;}
};
Iter i, n;
public:
range(int n) : i({0}), n({n}) {}
range(int i, int n) : i({i}), n({n}) {}
Iter& begin() {return i;}
Iter& end() {return n;}
};
//output
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){REP(i,as.size()){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T> >& as){REP(i,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){EACH(a,ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){os<<p.first<<" "<<p.second;return os;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;EACH(p,m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
//input
char tmp[1000];
#define nextInt(n) scanf("%d",&n)
#define nextLong(n) scanf("%lld",&n) //I64d
#define nextDouble(n) scanf("%lf",&n)
#define nextChar(n) scanf("%c",&n)
#define nextString(n) scanf("%s",tmp);n=tmp
// values
template<class T> T INF(){assert(false);};
template<> int INF<int>(){return 1<<28;};
template<> ll INF<ll>(){return 1LL<<58;};
template<> double INF<double>(){return 1e16;};
template<typename T> T pmod(T v,T M){return (v%M+M)%M;}
namespace _double_tmpl{
typedef long double D;const D EPS = 1e-8;
static constexpr D Ae=0;
D A(D a,D b){return a+b;}D Ainv(D a){return -a;}
D S(D a,D b){return A(a,Ainv(b));}
static constexpr D Me=1;
D M(D a,D b){return a*b;}D Minv(D a){return 1.0/a;};
int sig(D a,D b=0){return a<b-EPS?-1:a>b+EPS?1:0;}
bool eq(D a,D b){ return sig(abs(a-b))==0;}
}
using namespace _double_tmpl;
namespace Ps{
// using namespace _double_tmpl;
typedef complex<D> P,Vec;
#define X real()
#define Y imag()
istream& operator >> (istream& is,complex<D>& p){
D x,y;is >> x >> y;p=P(x,y);return is;
}
bool eq(P a,P b){return eq(a.X,b.X) and eq(a.Y,b.Y);}
// a×b
D cross(const Vec& a,const Vec& b){return imag(conj(a)*b);}
// a・b
D dot(const Vec&a,const Vec& b) {return real(conj(a)*b);}
int ccw(const P& a,P b,P c){
b -= a; c -= a;
if (cross(b,c) > EPS) return +1; // counter clockwise
if (cross(b,c) < -EPS) 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;
}
}
using namespace Ps;
namespace Polys{
typedef vector<P> Poly,ConvexPoly;
bool is_convex(const Poly& ps){
int n=ps.size();
REP(i,n)
if(ccw(ps[pmod(i,n)],ps[pmod(i+1,n)],ps[pmod(i+2,n)])==-1)return false;
return true;
}
}
using namespace Polys;
stringstream ss;
class Main{
public:
void run(){
int n;cin >> n;
Poly poly;
REP(i,n) {
P p;cin >> p;
poly.push_back(p);
}
cout <<is_convex(poly)<<endl;
}
};
int main(){
cout <<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
Main().run();
return 0;
} |
//
// 与えられた多角形が凸かどうか
//
// verified:
// AOJ Course CGL_3_B Polygon - Is-Convex
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A&lang=jp
//
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
using DD = double;
const DD INF = 1LL<<60; // to be set appropriately
const DD EPS = 1e-10; // to be set appropriately
const DD PI = acos(-1.0);
DD torad(int deg) {return (DD)(deg) * PI / 180;}
DD todeg(DD ang) {return ang * 180 / PI;}
/* Point */
struct Point {
DD x, y;
Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}
friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';}
};
inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}
inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}
inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}
inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}
inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}
inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}
inline Point conj(const Point &p) {return Point(p.x, -p.y);}
inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}
inline Point rot90(const Point &p) {return Point(-p.y, p.x);}
inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}
inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}
inline DD norm(const Point &p) {return dot(p, p);}
inline DD abs(const Point &p) {return sqrt(dot(p, p));}
inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}
inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}
inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}
inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}
inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}
/* Line */
struct Line : vector<Point> {
Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {
this->push_back(a);
this->push_back(b);
}
friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << ", " << l[1] << '}';}
};
/* Circle */
struct Circle : Point {
DD r;
Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}
friend ostream& operator << (ostream &s, const Circle &c) {return s << '(' << c.x << ", " << c.y << ", " << c.r << ')';}
};
///////////////////////
// 凸性判定
///////////////////////
int ccw_for_isconvex(const Point &a, const Point &b, const Point &c) {
if (cross(b-a, c-a) > EPS) return 1;
if (cross(b-a, c-a) < -EPS) return -1;
return 0;
}
bool isConvex(vector<Point> &ps) {
int n = (int)ps.size();
for (int i = 0; i < n; ++i) {
if (ccw_for_isconvex(ps[i], ps[(i+1)%n], ps[(i+2)%n]) == -1) return false;
}
return true;
}
int main() {
int n; cin >> n;
vector<Point> ps(n);
for (int i = 0; i < n; ++i) cin >> ps[i].x >> ps[i].y;
if (isConvex(ps)) cout << 1 << endl;
else cout << 0 << endl;
}
|
#include <bits/stdc++.h>
#define REP(i,n) for (long long i=0;i<(n);i++)
#define FOR(i,a,b) for (long long i=(a);i<(b);i++)
#define RREP(i,n) for(long long i=n;i>=0;i--)
#define RFOR(i,a,b) for(long long i=(a);i>(b);i--)
#define dump1d_arr(array) REP(i,array.size()) cerr << #array << "[" << (i) << "] ==> " << (array[i]) << endl;
#define dump2d_arr(array) REP(i,array.size()) REP(j,array[i].size()) cerr << #array << "[" << (i) << "]" << "[" << (j) << "] ==> " << (array[i][j]) << endl;
#define dump(x) cerr << #x << " => " << (x) << endl;
#define dumpP(p) cerr << "( " << p.first << " , " << p.second << " )" << ends;
#define CLR(vec) { REP(i,vec.size()) vec[i] = 0; }
#define SORT(c) sort((c).begin(),(c).end())
#define MIN(vec) *min_element(vec.begin(), vec.end());
#define MAX(vec) *max_element(vec.begin(), vec.end());
#define UNIQ(vec) vec.erase(unique(vec.begin(), vec.end()),vec.end());
#define IN(n,m) (!(m.find(n) == m.end()))
#define ENUM(m) for (auto itr = m.begin(); itr != m.end(); ++itr)
#define dump_MAP(m) for(auto itr = m.begin(); itr != m.end(); ++itr) { cerr << itr->first << " --> " << itr->second << endl; }
#define FINDL(vec,x) (lower_bound(vec.begin(),vec.end(),x) - vec.begin())
#define FINDU(vec,x) (upper_bound(vec.begin(),vec.end(),x) - vec.begin())
#define ROUND(N) setprecision(N)
using namespace std;
constexpr double eps = 1e-10;
constexpr long mod = 1000000007;
constexpr short shINF = 32767;
constexpr long loINF = 2147483647;
constexpr long long llINF = 9223372036854775807;
typedef long long LL;
typedef vector<LL> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<LL,LL> pr;
typedef vector<pr> VP;
struct Order {
bool operator() (pr const& a,pr const& b) const {
return a.first > b.first || ((a.first == b.first) && (a.second > b.second));
}
};
typedef struct _PT {
long double x, y;
_PT() {}
_PT(long double x,long double y) : x(x), y(y) {}
_PT operator + (_PT p){
return _PT(x + p.x, y + p.y);
}
_PT operator - (_PT p){
return _PT(x - p.x, y - p.y);
}
_PT operator * (long double d){
return _PT(d*x, d*y);
}
bool operator <(const struct _PT &e) const{
return x == e.x? (y < e.y) : x < e.x;
}
bool operator >(const struct _PT &e) const{
return x == e.x? (y > e.y) : x > e.x;
}
// --オプジェクトは位置ベクトル--
long double dist(_PT p){ //点pとの距離
return sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y));
}
// --オブジェクトは幾何ベクトル--
long double dot(_PT v){ //ベクトルvとの内積
return x * v.x + y * v.y;
}
long double det(_PT v){ // ベクトルvとの外積
return x * v.y - v.x * y;
}
long double norm(void){ // ベクトルのノルム
return sqrt(x*x + y*y);
}
long double twiceNorm(void){ // ノルムの2乗
return x*x + y*y;
}
bool is_parallel(_PT v) { //ベクトルvと平行か(外積 = 0か)
return (abs(x * v.y - v.x * y) < eps);
}
}P;
class Plane{
private:
static bool cmp_x(const P &p1,const P &p2){
if (p1.x != p2.x) return p1.x < p2.x;
return p1.y < p2.y;
}
public:
P intersection(P p1,P p2,P q1,P q2) { // 直線p1-p2と直線q1-q2の交点
return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
bool on_seq(P p1,P p2,P q) { // 線分p1-p2上に点qがあるか
return (abs((p1 - q).det(p2 - q)) < eps) && ((p1 - q).dot(p2 - q) < (eps));
}
bool have_intersection(P p1,P p2,P q1,P q2) { // 線分p1-p2と線分q1-q2がが交点を持つか
if (abs((p1-p2).det(q1-q2)) > eps) {
P r = intersection(p1,p2,q1,q2);
return (on_seq(p1,p2,r) && on_seq(q1,q2,r));
}
else return (on_seq(p1,p2,q1) || on_seq(p1,p2,q2) || on_seq(q1,q2,p1) || on_seq(q1,q2,p2));
}
P projection(P v1,P v2) { // v1へのv2の正射影ベクトル
return (v1)*((v1).dot(v2) / v1.twiceNorm());
}
long double disPointToLine(P p1,P p2,P q) { // 点と直線の距離
return (p1 + (projection((p2-p1),(q-p1)))).dist(q);
}
long double disPointToLineSeg(P p1,P p2,P q) { //線分p1-p2と点qの距離
if ((p1 - p2).dot(q - p2) < eps) return q.dist(p2);
if ((p2 - p1).dot(q - p1) < eps) return q.dist(p1);
else return (p1 + projection(p2-p1,q-p1)).dist(q);
}
long double disLineSeg(P p1,P p2,P q1,P q2) { //線分p1-p2と線分q1-q2の距離
if (have_intersection(p1,p2,q1,q2)) return 0.0;
else return min({disPointToLineSeg(p1,p2,q1),disPointToLineSeg(p1,p2,q2),disPointToLineSeg(q1,q2,p1),disPointToLineSeg(q1,q2,p2)});
}
vector<P> convexHull(vector<P> &point){
LL n = point.size();
vector<P> qs(n*2);
sort(point.begin(),point.end(),cmp_x);
long k = 0;
REP(i,n){ //下側凸包の作成
while(k > 1 && (qs[k-1] - qs[k-2]).det(point[i] - qs[k-1]) <= 0) k--;
qs[k++] = point[i];
}
for(long i = n - 2, t = k; i >= 0; i--){ //上側凸包の作成
while(k > t && (qs[k-1] - qs[k-2]).det(point[i] - qs[k-1]) <= 0) k--;
qs[k++] = point[i];
}
qs.resize(k-1);
return qs;
}
long double getArea(vector<P> &point){
long double s = 0;
LL n = point.size();
REP(i,n-1) s += point[i].det(point[i+1]);
s += point[n-1].det(point[0]);
return abs(s) * 0.5;
}
long double getAngle(P v1,P v2){ //2ベクトルのなす角を出す。
return acos(v1.dot(v2)/sqrt(v1.twiceNorm() * v2.twiceNorm()));
}
};
int main(void) {
Plane pl;
int N;
cin >> N;
int p = N;
vector<P> point(N);
REP(i,N) cin >> point[i].x >> point[i].y;
P prev = point[N-1] - point[0];
FOR(i,1,N-1) {
if ((point[i-1]-point[i]).is_parallel(point[i+1]-point[i])) p--;
}
if ((point[1]-point[0]).is_parallel(point[N-1]-point[0])) p--;
if((point[N-2]-point[N-1]).is_parallel(point[0]-point[N-1])) p--;
int ret = (pl.convexHull(point)).size();
if (p == ret || p == 0) cout << 1 << endl;
else cout << 0 << endl;
}
|
#include <algorithm>
#include <complex>
#include <vector>
using namespace std;
const double eps = 1e-10;
#define EQ(a, b) (abs((a) - (b)) < eps)
using P = complex<double>; // position
struct L : vector<P> {
L(const P &a = P(), const P &b = P()) {
emplace_back(a); emplace_back(b);
}
};
using Polygon = vector<P>;
ostream &operator<<(ostream &os, P p) { return os << real(p) << " " << imag(p); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
enum CCW_RESULT { CCW = +1, CW = -1, BEHIND = +2, FRONT = -2, ON = 0 };
int ccw(P a, P b, P c) {
b -= a; c -= a;
if (cross(b, c) > eps) return CCW; // counter clockwise
if (cross(b, c) < -eps) return CW; // clockwise
if (dot(b, c) < 0) return BEHIND; // c--a--b on line
if (norm(b) < norm(c)) return FRONT; // a--b--c on line | a=b
return ON; // on segment | a=c | b=c
}
#define curr(ps, i) ps[(i) % ps.size()]
#define next(ps, i) ps[(i + 1) % ps.size()]
#define prev(ps, i) ps[(i + ps.size() - 1) % ps.size()]
bool is_convex(const Polygon &ps) {
for (int i = 0; i < ps.size(); i++) {
if (ccw(prev(ps, i), curr(ps, i), next(ps, i)) == -1) return false;
}
return true;
}
#include <iostream>
#include <iomanip>
int main() {
cout << fixed << setprecision(1);
int n; cin >> n;
Polygon 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>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
typedef complex<double> Point;
typedef vector<Point> VP;
#define X real()
#define Y imag()
const double EPS = 1e-4;
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;
}
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross(b,c) > EPS) return +1; // counter clockwise
if (cross(b,c) < -EPS) return -1; // clockwise
if (dot(b,c) < -EPS) return +2; // c--a--b on line
if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b
return 0; // a--c--b on line or a==c or b==c
}
// ???????????????????????????????????????ccw???????????¨?????? != 1 ??¨??????
bool isCcwConvex(const VP& ps) {
int n = ps.size();
rep (i, n) if (ccw(ps[i], ps[(i+1) % n], ps[(i+2) % n]) == -1) return false;
return true;
}
int main(void){
int n;
cin>>n;
VP p(n);
rep(i,n){
double x,y;
cin>>x>>y;
p[i] = {x,y};
}
cout<<isCcwConvex(p)<<endl;
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include <map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
#include <complex>
using namespace std;
#define LONG_INF 10000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353LL
#define seg_size 65536*4
#define REP(i,n) for(long long i = 0;i < n;++i)
class Point{
public:
long double x;
long double y;
const Point operator*(const long double &a) {
Point ans;
ans.x = x * a;
ans.y = y * a;
return ans;
}
const Point operator=(const Point &a){
x = a.x;
y = a.y;
return *this;
}
const Point operator+(const Point &a) const{
Point ans;
ans.x = a.x + x;
ans.y = a.y + y;
return ans;
}
const Point operator+=(const Point &a){
x += a.x;
y += a.y;
return *this;
}
const Point operator-(const Point &a) const{
Point ans;
ans.x = x - a.x;
ans.y = y - a.y;
return ans;
}
const Point operator-=(const Point &a) {
x -= a.x;
y -= a.y;
return *this;
}
static long double inner(const Point &a, const Point &b) {
return a.x * b.x + a.y * b.y;
}
static long double cross(const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
}
static Point norms(const Point &a) {
long double hoge = abs(a);
Point ans;
ans.x = a.x / hoge;
ans.y = a.y / hoge;
return ans;
}
static long double abs(const Point &a) {
return sqrtl(a.x * a.x + a.y * a.y);
}
};
int main(){
int n;
cin >> n;
vector<Point> input(n + 2);
REP(i, n) {
cin >> input[i].x >> input[i].y;
}
input[n] = input[0];
input[n + 1] = input[1];
for (int i = 1; i < n; ++i) {
long double a = Point::cross(input[i] - input[i - 1], input[i + 1] - input[i]);
long double b = Point::cross(input[i + 1] - input[i], input[i + 2] - input[i + 1]);
if (a*b < 0) {
cout << 0 << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<climits>
#include<cstring>
#include<queue>
using namespace std;
const int INF = INT_MAX / 3;
#define REP(i,n) for(int i=0;i<(int)n;i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
double EPS = 1e-10;
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(d*x, d*y);
}
double dot(P p) {
return add(x*p.x, y*p.y);
}
double det(P p) {
return add(x*p.y, -y*p.x);
}
void print() {
printf("%.10lf %.10lf \n", x, y);
return;
}
};
int IsConvex(vector<P> &polygon){
int n = polygon.size();
int d = 1;
P p1, p2;
for(int i=0;i<n-2;i++){
p1 = polygon[i+1]-polygon[i];
p2 = polygon[i+2]-polygon[i+1];
if(p1.det(p2)<0){
d = 0;
break;
}
}
p1 = polygon[n-1]-polygon[n-2];
p2 = polygon[0]-polygon[n-1];
if(p1.det(p2)<0){
d = 0;
}
p1 = polygon[0]-polygon[n-1];
p2 = polygon[1]-polygon[0];
if(p1.det(p2)<0){
d = 0;
}
return d;
}
int main(){
int n;
cin >> n;
vector<P> polygon;
REP(i,n){
P p;
cin >> p.x >> p.y;
polygon.push_back(p);
}
cout << IsConvex(polygon) <<endl;
return 0;
} |
#include<iostream>
#include<complex>
#include<vector>
#include<iomanip>
using namespace std;
typedef complex<double> P;
typedef vector<P> G;
#define EPS 1e-9
#define shosu(x) fixed<<setprecision(x)
struct L : public vector<P> {
L(const P &a, const P &b) {
push_back(a); push_back(b);
}
L(){;}
};
struct C{
P c;double r;
C(const P &c,double r):c(c),r(r){}
};
namespace std{//演算子の定義
bool operator < (const P& a,const P& b){
return real(a)!= real(b) ? real(a) < real(b) : imag(a) < imag(b);
//return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b)
}
bool operator == (const P& a,const P& b){
return a.real()==b.real() && a.imag()==b.imag();
}
}
double dot(P a,P b){
return real(conj(a)*b);
}
double cross(P a,P b){
return imag(conj(a)*b);
}
int ccw(P a, P b, P c) {//3点の関係性
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
}
P projection(const L &l, const P &p){//pの直線l上の射影の点
double t=dot(p-l[0],l[0]-l[1])/norm(l[0]-l[1]);
return l[0]+t*(l[0]-l[1]);
}
P reflection(const L &l, const P &p){//点pの直線lに関して対称な点
return p+2.0*(projection(l,p)-p);
}
bool isOrthogonal(const L &l, const L &m){//2直線の直交判定
return fabs(dot(l[1]-l[0], m[1]-m[0])) < EPS;
}
bool isParallel(const L &l, const L &m){//2直線の平行判定
return fabs(cross(l[1]-l[0],m[1]-m[0])) < EPS;
}
bool intersectLL(const L &l, const L &m){//2直線の交差判定
return !isParallel(l,m);
}
bool intersectSS(const L &s, const L &t) { //2線分の交差判定(完全に交差してないとだめ)
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
bool intersectSP(const L &s, const P &p) { //直線と点の交差判定
return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
P crosspointSS(const L &a, const L &b){ //2線分の交点
double t1=abs(cross(a[1]-a[0],b[0]-a[0]));
double t2=abs(cross(a[1]-a[0],b[1]-a[0]));
return b[0]+(b[1]-b[0])*t1/(t1+t2);
}
double distanceSP(const L &s, const P &p) {//直線と点との距離
const P r = projection(s, p);
if (intersectSP(s, r)) return abs(r - p);
return min(abs(s[0] - p), abs(s[1] - p));
}
double distanceSS(const L &s, const L &t) {//2線分の距離
if (intersectSS(s, t)) return 0;
return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}
double area(const G &g){ //多角形の面積
double S =0;
for(int i = 0; i < g.size(); i++){
S +=(cross(g[i],g[(i+1)%g.size()]));
}
return abs(S/2.0);
}
bool isconvex(const G &g){ //凸かどうか(全ての内角の大きさが180度以下)
int n=g.size();
for(int i = 0; i < n; i++)
if(ccw(g[(i+n-1)%n],g[i%n],g[(i+1)%n])==-1)return false;
return true;
}
P inP(){
double x,y;
cin >> x >> y;
P p(x,y);
return p;
}
L inL(){
P p1=inP();
P p2=inP();
L l(p1,p2);
return l;
}
int main(){
int n;
cin>>n;
G g;
for(int i=0;i<n;i++){
P p = inP();
g.push_back(p);
}
if(isconvex(g)){
cout << 1 << endl;
}else{
cout << 0 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;
#define eps (1e-10)
struct Point{
double x,y;
Point(){}
Point(double x,double y):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 k){return Point(x*k,y*k);}
double norm(){return x*x+y*y;}
double abs(){return sqrt(norm());}
bool operator == (const Point &p) const{
return fabs(x-p.x)<eps && fabs(y-p.y)<eps;
}
double dot(Point p){return x*p.x+y*p.y;}
double det(Point p){return x*p.y-y*p.x;}
};
int ccw(Point a,Point b,Point c){
Point t1=b-a,t2=c-a;
if(t1.det(t2)> eps) return 1;//counter clockwise
if(t1.det(t2)< -eps) return -1;//clockwise
if(t1.dot(t2)< -eps) return 2;//c-a-b online
if(t1.norm()<t2.norm()) return -2;//a-b-c online
return 0;//a-c-b online
}
Point a[101];
int main(){
int n;
cin>>n;
double x,y;
rep(i,n){
cin>>x>>y;
a[i]=Point(x,y);
}
bool flag=true;
rep(i,n){
if(ccw(a[(i+1)%n],a[i],a[(i+2)%n])==1) flag=false;
}
cout<<flag<<endl;
return 0;
}
|
//{{{
#include <bits/stdc++.h>
using namespace std;
//types
typedef long long ll;
typedef pair<int,int> pii;
//input
bool SR(int &_x){return scanf("%d",&_x)==1;}bool SR(ll &_x){return scanf("%lld",&_x)==1;}
bool SR(double &_x){return scanf("%lf",&_x)==1;}bool SR(char *_s){return scanf("%s",_s)==1;}
bool RI(){return true;}
template<typename I,typename... T>bool RI(I &_x,T&... _tail){return SR(_x) && RI(_tail...);}
//output
void SP(const int _x){printf("%d",_x);}void SP(const ll _x){printf("%lld",_x);}
void SP(const double _x){printf("%.16lf",_x);}void SP(const char *s){printf("%s",s);}
void PL(){puts("");}
template<typename I,typename... T>void PL(const I _x,const T... _tail)
{SP(_x);if(sizeof...(_tail)) putchar(' ');PL(_tail...);}
//macro
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<int(n);i++)
#define REP1(i,a,b) for(int i=(a);i<=int(b);i++)
#define PER1(i,a,b) for(int i=(a);i>=int(b);i--)
#define pb push_back
#define mkp make_pair
#define F first
#define S second
//debug
#ifdef darry140
template<typename A,typename B>
ostream& operator <<(ostream&_s, const pair<A,B> &_p){return _s<<"("<<_p.F<<","<<_p.S<<")";}
template<typename It>
ostream& _OUTC(ostream &_s,It _b,It _e)//container
{
_s<<"{";
for(auto _it=_b;_it!=_e;_it++) _s<<(_it==_b?"":" ")<<*_it;
_s<<"}";
return _s;
}
template<typename A,typename B>
ostream& operator <<(ostream&_s, const map<A,B> &_c){return _OUTC(_s,ALL(_c));}
template<typename T>
ostream& operator <<(ostream&_s, const set<T> &_c){return _OUTC(_s,ALL(_c));}
template<typename T>
ostream& operator <<(ostream&_s, const vector<T> &_c){return _OUTC(_s,ALL(_c));}
template<typename I>
void _DOING(const char *_s,I&& _x){cerr<<_s<<"="<<_x<<endl;}//without ','
template<typename I,typename... T>
void _DOING(const char *_s,I&& _x,T&&... _tail)//with ','
{
int _c=0;
static const char _bra[]="({[";
static const char _ket[]=")}]";
while(*_s!=',' || _c!=0)//eg. mkp(a,b)
{
if(strchr(_bra,*_s)) _c++;
if(strchr(_ket,*_s)) _c--;
cerr<<*_s++;
}
cerr<<"="<<_x<<", ";
_DOING(_s+1,_tail...);
}
#define debug(...) do{\
fprintf(stderr,"%s:%d - ",__PRETTY_FUNCTION__,__LINE__);\
_DOING(#__VA_ARGS__,__VA_ARGS__);\
}while(0)
#else
#define debug(...)
#endif
//}}}
struct Point
{
ll x,y;
Point():x(0),y(0){}
Point(ll _x,ll _y):x(_x),y(_y){}
Point operator +(const Point &p) const
{return Point(x+p.x,y+p.y);}
Point operator -(const Point &p) const
{return Point(x-p.x,y-p.y);}
ll operator *(const Point &p) const
{return x*p.x+y*p.y;}
ll operator %(const Point &p) const
{return x*p.y-y*p.x;}
bool operator <(const Point &p) const
{return tie(y,x) < tie(p.y,p.x);}
bool operator ==(const Point &p) const
{return tie(y,x) == tie(p.y,p.x);}
Point operator /(const ll &l) const
{return Point(x/l,y/l);}
Point operator *(const ll &l) const
{return Point(x*l,y*l);}
};
int main()
{
int n;RI(n);
vector<Point> p(n);
REP(i,n) RI(p[i].x,p[i].y);
REP(i,n)
{
auto a=p[i]-p[(i+n-1)%n],b=p[i]-p[(i+1)%n];
if(a%b>0){ PL(0);return 0;}
}
PL(1);
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.