Asia Regional Contest, Tsukuba, 2016–10–16

A Rearranging a Sequence

#include<bits/stdc++.h>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))
#define fi first
#define se second
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int maxn=200005;
int n,m;
int a[maxn];
bool vis[maxn];

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)scanf("%d",&a[i]);
    int cnt=0;
    for(int i=m;i>=1;i--)
    {
        if(vis[a[i]])continue;
        printf("%d\n",a[i]);
        vis[a[i]]=true;
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i])continue;
        cnt++;
        printf("%d\n",i);
    }
    return 0;
}

B Quality of Check Digits

#include<bits/stdc++.h>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))
#define fi first
#define se second
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int a[10][10];
bool vis[10];
int x[4],v[4],u[4],cnt=0;

int check(int q,int w,int e,int r)
{
    u[0]=q;u[1]=w;u[2]=e;u[3]=r;
    int ret=0;
    for(int i=0;i<4;i++)
        ret=a[ret][u[i]];
    return ret;
}

int main()
{
    for(int i=0;i<=9;i++)
        for(int j=0;j<=9;j++)
            scanf("%d",&a[i][j]);
    for(int i=0;i<=9;i++)
        for(int j=0;j<=9;j++)
            if(i!=j && a[i][j]==0)vis[i]=true;
    for(x[0]=0;x[0]<=9;x[0]++)
        for(x[1]=0;x[1]<=9;x[1]++)
            for(x[2]=0;x[2]<=9;x[2]++)
                for(x[3]=0;x[3]<=9;x[3]++)
                {
                    int now=check(x[0],x[1],x[2],x[3]);
                    if(vis[now]){cnt++;continue;}
                    bool f=true;
                    for(int p=0;p<4 && f;p++)
                    {
                        for(int t=0;t<4;t++)v[t]=x[t];
                        for(int s=0;s<=9;s++)
                            if(s!=x[p])
                            {
                                v[p]=s;
                                if(a[check(v[0],v[1],v[2],v[3])][now]==0)
                                {
                                    f=false;break;
                                }
                            }
                    }
                    if(!f){cnt++;continue;}
                    if(x[0]!=x[1] && a[check(x[1],x[0],x[2],x[3])][now]==0){cnt++;continue;}
                    if(x[1]!=x[2] && a[check(x[0],x[2],x[1],x[3])][now]==0){cnt++;continue;}
                    if(x[2]!=x[3] && a[check(x[0],x[1],x[3],x[2])][now]==0){cnt++;continue;}
                    if(x[3]!=now && a[check(x[0],x[1],x[2],now)][x[3]]==0){cnt++;continue;}
                }
    printf("%d\n",cnt);
    return 0;
}

C Distribution Center

可以发现到达某个点的点一定是一段区间,只要求上下界。
那么可以考虑相邻2 Lanes的转移,每个Lane存它能到达的上、下界。

#include<bits/stdc++.h>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))
#define fi first
#define se second
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int n,m;
vector<int> g[200005];
int a[2][100005];
int up[200005],dw[200005];

int main()
{
    //IN;
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++)g[i].pb(0),g[i].pb(100005);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[y].pb(x);
    }
    for(int i=1;i<=n;i++)up[i]=dw[i]=i;
    for(int i=1;i<n;i++)sort(g[i].begin(),g[i].end());
    up[1]=1;
    int now=0;
    for(int i=1;i<n;i++)
    {
        now^=1;
        a[now^1][0]=i;
        for(int t : g[i])
        {
            if(t<=0 || t>=100000)continue;
            int x=lower_bound(g[i-1].begin(),g[i-1].end(),t)-g[i-1].begin();
            x--;
            a[now][t]=a[now^1][g[i-1][x]];
            up[i+1]=min(up[i+1],a[now][t]);
        }
    }
    now=0;dw[n]=n;a[0][0]=n;
    for(int i=n-1;i>=1;i--)
    {
        now^=1;
        a[now][0]=i;
        for(int t : g[i])
        {
            if(t<=0 || t>=100000)continue;
            int x=lower_bound(g[i+1].begin(),g[i+1].end(),t)-g[i+1].begin();
            x--;
            a[now][t]=a[now^1][g[i+1][x]];
            dw[i]=max(dw[i],a[now][t]);
        }
    }
    for(int i=1;i<=n;i++)printf("%d%c",dw[i]-up[i]+1,i==n?'\n':' ');
    return 0;
}

D Hidden Anagrams

双hash

#include<bits/stdc++.h>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))
#define fi first
#define se second
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int mod1=1e9+7;
const int seed1=5003;
const int mod2=1e9+9;
const int seed2=5009;
char s1[4005],s2[4005];
int a1[4005][26],a2[4005][26];
int l1,l2;
pii h1[4005],h2[4005];
int c1,c2;

int main()
{
    scanf("%s",s1+1);l1=strlen(s1+1);
    scanf("%s",s2+1);l2=strlen(s2+1);
    for(int i=1;i<=l1;i++)
    {
        for(int j=0;j<26;j++)
            a1[i][j]=a1[i-1][j];
        a1[i][s1[i]-97]++;
    }
    for(int i=1;i<=l2;i++)
    {
        for(int j=0;j<26;j++)
            a2[i][j]=a2[i-1][j];
        a2[i][s2[i]-97]++;
    }
    for(int ans=min(l1,l2);ans>=1;ans--)
    {
        c1=c2=0;
        for(int i=1;i+ans-1<=l1;i++)
        {
            int now1=0,now2=0;
            for(int j=0;j<26;j++)
            {
                int p=a1[i+ans-1][j]-a1[i-1][j];
                now1=1ll*now1*seed1%mod1;
                now1+=p;
                now1%=mod1;
                now2+=p;now2%=mod2;
                now2=1ll*now2*seed2%mod2;
            }
            h1[++c1]=mp(now1,now2);
        }
        for(int i=1;i+ans-1<=l2;i++)
        {
            int now1=0,now2=0;
            for(int j=0;j<26;j++)
            {
                int p=a2[i+ans-1][j]-a2[i-1][j];
                now1=1ll*now1*seed1%mod1;
                now1+=p;
                now1%=mod1;
                now2+=p;now2%=mod2;
                now2=1ll*now2*seed2%mod2;
            }
            h2[++c2]=mp(now1,now2);
        }
        sort(h1+1,h1+c1+1);
        sort(h2+1,h2+c2+1);
        int l=1;
        for(int i=1;i<=c1;i++)
        {
            while(l<=c2 && h2[l]<h1[i])l++;
            if(h1[i]==h2[l])
            {
                printf("%d\n",ans);
                return 0;
            }
        }
    }
    printf("0\n");
    return 0;
}

E Infallibly Crack Perplexing Cryptarithm

逆波兰表达式,递归下降

F Three Kingdoms of Bourdelot

假设条目可以全是negative,任意两人没有血缘关系。
维护一个队列,表示一定成立的条目。先将第一条目入队,之后找出可以确定的条目,继续入队……反复执行此操作,如果存在传递闭包则无解。

G Placing Medals on a Binary Tree

我们考虑对第i层的一个节点占用了 (1/2)i 的总节点。
于是我们实际只要判断之前用的节点+当前节点占总节点的比值是否 >1
注意这题精度要求较高,我们可以用线段树,树状数组维护大量 (1/2)k 相加,相当于手动模拟高精加运算。注意每次如果不进位则单点+1,进位则若干为变为0,单点+1,容易发现变为0这个操作最多发生O(n)次,所以可以手动模拟。
考虑最劣情况1/2,1/4,1/8,..., \frac 1 {2^{n-1}} , \frac 1 {2^{n-1}
或者 1/2k,1/2k,...,1/2k
发现最多会影响的位置是第n+log(n)层
所以超过这层的放在这层就行

#include<bits/stdc++.h>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))
#define fi first
#define se second
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int a[600005],s[600005];
int n,x,cs=555555;

void add(int p,int nu)
{
    while(p<=cs)
    {
        s[p]+=nu;
        p+=lowbit(p);
    }
}

int cal(int p)
{
    int ret=0;
    while(p>0)
    {
        ret+=s[p];
        p-=lowbit(p);
    }
    return ret;
}

int main()
{
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&x);
        x=min(x,cs);
        if(a[0]>0 || (cal(x)==x && cal(cs)>cal(x)))
        {
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        while(a[x]==1)
        {
            a[x]=0;
            add(x,-1);
            x--;
        }
        a[x]=1;
        if(x>0)add(x,1);
    }
    return 0;
}

H Animal Companion in Maze

把问题转化成给一颗带点权的树(2 ≤ n ≤ 10000),对每个点A求 maxB(dis(A,B)+valueB)

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 1000001
int dfn[MAXN], low[MAXN], stack[MAXN];
int res[MAXN];
bool flag[MAXN];
vector<int> e[MAXN];
int cur, top, cnt;//cur为访问标号,top为栈顶,cnt为强连通分量个数
//使用前清空dfn,flag数组,cur,cnt置0;返回res存储每个点所在强连通分量集合
void tarjan(int i)
{
    low[i] = dfn[i] = ++cur;
    stack[++top] = i; flag[i] = true;
    for (unsigned j = 0; j < e[i].size(); j++){
        int id = e[i][j];
        if (!dfn[id]){
            tarjan(id);
            low[i] = min(low[i], low[id]);
        }
        else if (flag[id])
            low[i] = min(low[i], dfn[id]);//此处可写low
    }
    if (low[i] == dfn[i]){
        cnt++;
        int t;
        do{
            t = stack[top--];
            res[t] = cnt;
            flag[t] = false;
        } while (t != i);
    }
}

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int> v[1000001];
int degree[1000001];
int ans[1000001], layer[1000001];//ans为拓扑序列,layer为每个结点层数
//返回值为true表示成功,false表示有环
bool toposort(int n)
{
    int k = 0;
    memset(degree + 1, 0, sizeof(int)*n);
    memset(layer + 1, 0, sizeof(int)*n);
    for (int i = 1; i <= n; i++){
        for (unsigned int t = 0; t < v[i].size(); t++)
            degree[v[i][t]]++;
    }
    for (int i = 1; i <= n; i++){
        if (!degree[i])ans[k++] = i;
    }
    for (int j = 0; j < k; j++){
        for (unsigned int t = 0; t < v[ans[j]].size(); t++){
            int i = v[ans[j]][t];
            if (!--degree[i]){
                ans[k++] = i;
                layer[i] = layer[ans[j]] + 1;
            }
        }
    }
    return k == n;
}

vector<int> vv[200001];
vector<int> s[200001];
int f[200001];
bool vis[200001];
int getFather(int i){
    if(!f[i])return i;
    return f[i]=getFather(f[i]);
}
struct Edge{
    int x,y,w;
}ee[200001];
int out[200001];
int dp[200001],dp2[200001],dp3[200001],son[200001];
void dfs1(int i,int fa)
{
    dp2[i]=dp[i];
    for(int j:vv[i]){
        if(j!=fa){
            dfs1(j,i);
            int t=dp[j]+1;
            if(t>dp[i]){dp2[i]=dp[i];dp[i]=t;son[i]=j;}
            else if(t>dp2[i]){dp2[i]=t;}
        }
    }
}
void dfs2(int i,int fa)
{
    for(int j:vv[i]){
        if(j!=fa){
            if(j==son[i])dp3[j]=dp2[i]+1;
            else dp3[j]=dp[i]+1;
            dp3[j]=max(dp3[j],dp3[i]+1);
            dfs2(j,i);
        }
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    bool flag=false;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&ee[i].x,&ee[i].y,&ee[i].w);
        if(ee[i].w==2){
            int x=getFather(ee[i].x);
            int y=getFather(ee[i].y);
            if(x==y){flag=true;break;}
            f[x]=y;
            e[ee[i].x].push_back(ee[i].y);
            e[ee[i].y].push_back(ee[i].x);
        }
        else e[ee[i].x].push_back(ee[i].y);
    }
    if(!flag){
        for(int i=1;i<=n;i++){
            if(!dfn[i])tarjan(i);
        }
        for(int i=0;i<m;i++){
            if(res[ee[i].x]==res[ee[i].y]&&ee[i].w==1){
                flag=true;break;
            }
        }
    }
    if(flag)printf("Infinite");
    else{
        int finally=0;
        for(int i=1;i<=n;i++){
            for(int j:e[i]){
                if(res[i]!=res[j])
                    v[res[i]].push_back(res[j]);
                else
                    vv[i].push_back(j);
            }
        }
        toposort(cnt);
        for(int i=1;i<=n;i++)
            s[res[i]].push_back(i);
        for(int i=cnt-1;i>=0;i--){
            int ii=ans[i];
            for(int j:s[ii]){
                for(int k:e[j]){
                    if(res[k]!=ii)
                        dp[j]=max(dp[j],dp[k]+1);
                }
            }
            dfs1(s[ii][0],0);
            dfs2(s[ii][0],0);
            for(int j:s[ii]){
                dp[j]=max(dp[j],dp3[j]);
                finally=max(finally,max(dp[j],dp3[j]));
            }
        }
        printf("%d",finally);
    }
}

I Skinny Polygon

数论

J Cover the Polygon with Your Di

求一个多边形和圆相交的最大值

爬山

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
const double eps=1e-10;
int dcmp(double x) {
    if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1; 
}
ld PI = 3.141592653589793238462643383;
class P{
public:
    double x,y;
    P(double x=0,double y=0):x(x),y(y){}
    friend ld dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y);   }
    friend ld Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
    friend ld Length(P A) {return sqrt(Dot(A,A)); }
    friend ld Angle(P A,P B) {
        if (dcmp(Dot(A,A))==0||dcmp(Dot(B,B))==0||dcmp(Dot(A-B,A-B))==0) return 0;
        return acos(max((ld)-1.0, min((ld)1.0, Dot(A,B) / Length(A) / Length(B) )) ); 
    }

    friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
    friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
    friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
    friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
    friend bool operator< (const P& a,const P& b) {return dcmp(a.x-b.x)<0 ||(dcmp(a.x-b.x)==0&& dcmp(a.y-b.y)<0 );}

}; 
P read_point() {
    P a;
    scanf("%lf%lf",&a.x,&a.y);
    return a;   
} 
bool operator==(const P& a,const P& b) {
    return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
} 
typedef P V;

double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
    return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
} 
// A 不是 0向量 
V Normal(V A) { 
    double L = Length(A);
    return V(-A.y/L , A.x/L); 
}

namespace complex_G{
    typedef complex<double> Point;
    //real(p):实部 imag(p):虚部 conj(p):共轭 
    typedef Point Vector;
    double Dot(Vector A,Vector B) {return real(conj(A)*B); }
    double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
    Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(平行)时,不能调这个函数 
P GetLineIntersection(P p,V v,P Q,V w){
    V u = p-Q;
    double t = Cross(w,u)/Cross(v,w);
    return p+v*t;
}
P GetLineIntersectionB(P p,V v,P Q,V w){
    return GetLineIntersection(p,v-p,Q,w-Q);
}

double DistanceToLine(P p,P A,P B) {
    V v1 = B-A, v2 = p-A;
    return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
    if (A==B) return Length(p-A);
    V v1 = B-A, v2 = p-A, v3 = p - B;
    if (dcmp(Dot(v1,v2))<0) return Length(v2);
    else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
    else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
    V v=B-A;
    return A+v*(Dot(v,p-A)/Dot(v,v));
}
//规范相交-线段相交且交点不在端点 
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) { 
    double  c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
            c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//点在线段上(不包含端点) 
bool OnSegment(P p,P a1,P a2) {
    return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double PolygonArea(P *p,int n) {
    double area=0;
    For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2;
} 
/*欧拉公式: V+F-E=2 
V-点数 F面数 E边数 */
struct C{
    P c;
    double r,x,y;
    C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
    P point(double a) {
        return P(c.x+cos(a)*r,c.y+sin(a)*r);
    }
};

struct Line{
    P p;
    V v;
    double ang;
    Line(){}
    Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
    bool operator<(const Line & L) const {
        return ang<L.ang;
    }
    P point(double a) {
        return p+v*a;
    }
};
int getLineCircleIntersection(Line L,C cir,double &t1,double &t2,vector<P> & sol) {
    if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
        P A=GetLineProjection(cir.c,L.p,L.p+L.v);
        sol.pb(A); 
        t1 = (A-L.p).x / L.v.x;  
        return 1;
    }

    double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
    double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
    double delta = f*f - 4*e*g;
    if (dcmp(delta)<0) return 0;
    else if (dcmp(delta)==0) {
        t1 = t2 = -f / (2*e); sol.pb(L.point(t1));
        return 1;
    } 
    t1 = (-f - sqrt(delta)) / (2*e); sol.pb(L.point(t1));
    t2 = (-f + sqrt(delta)) / (2*e); sol.pb(L.point(t2));
    return 2;
}
double angle(V v) {return atan2(v.y,v.x);}
int getCircleCircleIntersection(C C1,C C2,vector<P>& sol) {
    double d = Length(C1.c-C2.c);
    if (dcmp(d)==0) {
        if (dcmp(C1.r - C2.r)==0) return -1; //2圆重合 
        return 0;
    }
    if (dcmp(C1.r+C2.r-d)<0) return 0;
    if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0;

    double a = angle(C2.c-C1.c);
    double da = acos((C1.r*C1.r+d*d - C2.r*C2.r)/ (2*C1.r*d));
    P p1 = C1.point(a-da), p2 = C1.point(a+da);
    sol.pb(p1);
    if (p1==p2) return 1;
    sol.pb(p2);
    return 2; 
}
// Tangents-切线 
int getTangents(P p,C c,V* v) {
    V u= c.c-p;
    double dist = Length(u);
    if (dist<c.r) return 0;
    else if (dcmp(dist-c.r)==0) {
        v[0]=Rotate(u,PI/2);
        return 1;
    } else {
        double ang = asin(c.r / dist);
        v[0]=Rotate(u,-ang);
        v[1]=Rotate(u,ang);
        return 2;
    }       
}
int getTangentsPoint(P p,C c,P *point) {
    V u= c.c-p;
    double dist = Length(u);
    if (dist<c.r) return 0;
    else if (dcmp(dist-c.r)==0) {
        point[0]=p;
        return 1;
    } else {
        V v[2];
        double ang = asin(c.r / dist);
        v[0]=Rotate(u,-ang);point[0]=GetLineProjection(c.c,p,p+v[0]);
        v[1]=Rotate(u,ang); point[1]=GetLineProjection(c.c,p,p+v[1]);
        return 2;
    }
}
//这个函数假设整数坐标和整数半径
//double时要把int改成double 
int getTangents(C A,C B,P* a,P* b) {
    int cnt=0;
    if (A.r<B.r) {swap(A,B),swap(a,b);}
    int d2 = (A.c.x-B.c.x)*(A.c.x-B.c.x) + (A.c.y-B.c.y)*(A.c.y-B.c.y);
    int rdiff = A.r-B.r;
    int rsum = A.r+B.r;
    if (d2<rdiff*rdiff) return 0;
    double base = atan2(B.y-A.y,B.x-A.x);
    if (d2==0 && A.r == B.r) return -1;
    if (d2 == rdiff*rdiff) {
        a[cnt] = A.point(base); b[cnt] = B.point(base); ++cnt;
        return 1;
    }
    double ang = acos((A.r-B.r)/sqrt(d2));
    a[cnt] = A.point(base+ang); b[cnt] = B.point(base+ang); ++cnt;
    a[cnt] = A.point(base-ang); b[cnt] = B.point(base-ang); ++cnt;
    if (d2==rsum*rsum) {
        a[cnt] = A.point(base); b[cnt] = B.point(PI+base); ++cnt;
    }
    else if (d2>rsum*rsum) {
        double ang = acos((A.r+B.r)/sqrt(d2));
        a[cnt] = A.point(base+ang); b[cnt] = B.point(PI+base+ang); ++cnt;
        a[cnt] = A.point(base-ang); b[cnt] = B.point(PI+base-ang); ++cnt;
    }
    return cnt; 
}
//Circumscribed-外接 
C CircumscribedCircle(P p1,P p2,P p3) {
    double Bx = p2.x-p1.x, By= p2.y-p1.y;
    double Cx = p3.x-p1.x, Cy= p3.y-p1.y;
    double D = 2*(Bx*Cy-By*Cx);
    double cx = (Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D + p1.x;
    double cy = (Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D + p1.y;
    P p =P(cx,cy);
    return C(p,Length(p1-p));
}
//Inscribed-内接 
C InscribedCircle(P p1,P p2,P p3) {
    double a = Length(p2-p3);
    double b = Length(p3-p1);
    double c = Length(p1-p2);
    P p = (p1*a+p2*b+p3*c)/(a+b+c);
    return C(p,DistanceToLine(p,p1,p2));
}
double torad(double deg) {
    return deg/180*acos(-1);
}
//把 角度+-pi 转换到 [0,pi) 上 
double radToPositive(double rad) {
    if (dcmp(rad)<0) rad=ceil(-rad/PI)*PI+rad;
    if (dcmp(rad-PI)>=0) rad-=floor(rad/PI)*PI; 
    return rad;
} 
double todeg(double rad) {
    return rad*180/acos(-1);
}

//(R,lat,lng)->(x,y,z)
void get_coord(double R,double lat,double lng,double &x,double &y,double &z) {
    lat=torad(lat);
    lng=torad(lng);
    x=R*cos(lat)*cos(lng);
    y=R*cos(lat)*sin(lng);
    z=R*sin(lat);
}
void print(double a) {
    printf("%.6lf",a); 
}
void print(P p) {
    printf("(%.6lf,%.6lf)",p.x,p.y);
}
template<class T>
void print(vector<T> v) {
    sort(v.begin(),v.end());
    putchar('[');
    int n=v.size();
    Rep(i,n) {
        print(v[i]);
        if (i<n-1) putchar(','); 
    }
    puts("]");
}
// 把直线沿v平移
// Translation-平移 
Line LineTranslation(Line l,V v) {
    l.p=l.p+v;
    return l;
}
void CircleThroughAPointAndTangentToALineWithRadius(P p,Line l,double r,vector<P>& sol) {
    V e=Normal(l.v);
    Line l1=LineTranslation(l,e*r),l2=LineTranslation(l,e*(-r));
    double t1,t2;
    getLineCircleIntersection(l1,C(p,r),t1,t2,sol);
    getLineCircleIntersection(l2,C(p,r),t1,t2,sol);
}
void CircleTangentToTwoLinesWithRadius(Line l1,Line l2,double r,vector<P>& sol) {
    V e1=Normal(l1.v),e2=Normal(l2.v);
    Line L1[2]={LineTranslation(l1,e1*r),LineTranslation(l1,e1*(-r))},
         L2[2]={LineTranslation(l2,e2*r),LineTranslation(l2,e2*(-r))};
    Rep(i,2) Rep(j,2) sol.pb(GetLineIntersection(L1[i].p,L1[i].v,L2[j].p,L2[j].v));
}
void CircleTangentToTwoDisjointCirclesWithRadius(C c1,C c2,double r,vector<P>& sol) {
    c1.r+=r; c2.r+=r;
    getCircleCircleIntersection(c1,c2,sol);
}
//确定4个点能否组成凸多边形,并按顺序(不一定是逆时针)返回 
bool ConvexPolygon(P &A,P &B,P &C,P &D) {
    if (SegmentProperIntersection(A,C,B,D)) return 1;
    swap(B,C);
    if (SegmentProperIntersection(A,C,B,D)) return 1;
    swap(D,C);
    if (SegmentProperIntersection(A,C,B,D)) return 1;
    return 0;
}
bool IsParallel(P A,P B,P C,P D) {
    return dcmp(Cross(B-A,D-C))==0;
}
bool IsPerpendicular(V A,V B) {
    return dcmp(Dot(A,B))==0;
}
//先调用ConvexPolygon 求凸包并确认是否是四边形
// Trapezium-梯形 Rhombus-菱形  
bool IsTrapezium(P A,P B,P C,P D){
    return IsParallel(A,B,C,D)^IsParallel(B,C,A,D);
}
bool IsParallelogram(P A,P B,P C,P D) {
    return IsParallel(A,B,C,D)&&IsParallel(B,C,A,D);
}
bool IsRhombus(P A,P B,P C,P D) {
    return IsParallelogram(A,B,C,D)&&dcmp(Length(B-A)-Length(C-B))==0;
}
bool IsRectangle(P A,P B,P C,P D) {
    return IsParallelogram(A,B,C,D)&&IsPerpendicular(B-A,D-A);
}
bool IsSquare(P A,P B,P C,P D) {
    return IsParallelogram(A,B,C,D)&&IsPerpendicular(B-A,D-A)&&dcmp(Length(B-A)-Length(C-B))==0;
}

bool IsCollinear(P A,P B,P C,P D) { //共线 
    return !dcmp(Cross(A-B,C-D)) && !dcmp(Cross(A-B,C-B)) && !dcmp(DistanceToLine(A,C,D));
} 
//chord-弦 arc-弧 
double ArcDis(double chord,double r) {
    return 2*asin(chord/2/r)*r;
} 
typedef vector<P> Polygon ;
int isPointInPolygon(P p,Polygon poly) {
    int wn=0;
    int n=poly.size();
    Rep(i,n) {
        if (OnSegment(p,poly[i],poly[(i+1)%n])) return -1; //edge
        int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
        int d1 = dcmp(poly[i].y-p.y);
        int d2 = dcmp(poly[(i+1)%n].y-p.y);
        if ( k > 0 && d1 <= 0 && d2 > 0 ) wn++;
        if ( k < 0 && d2 <= 0 && d1 > 0 ) wn--;
    }
    if (wn!=0) return 1; //inside
    return 0; //outside
}

int ConvexHull(P *p,int n,P *ch) {
    sort(p,p+n);
    int m=0;
    Rep(i,n) {
        while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    int k=m;
    RepD(i,n-2) {
        while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    if ( n > 1 ) m--;
    return m;
}
//把两点式转为一般式 ax+by+c=0 
void Two_pointFormToGeneralForm(P A,P B,double &a,double &b,double &c){
    a = A.y - B.y;
    b = B.x - A.x;
    c = Cross(A,B);
}
//有向直线A->B 切割多边行poly 可能返回单点线段 O(n)
Polygon CutPolygon(Polygon poly,P A,P B){
    Polygon newpoly;
    int n=poly.size();
    Rep(i,n) {
        P C = poly[i];
        P D = poly[(i+1)%n];
        if (dcmp(Cross(B-A,C-A))>=0)  newpoly.pb(C);
        if (dcmp(Cross(B-A,C-D))) {
            P ip = GetLineIntersection(A,B-A,C,D-C);
            if (OnSegment(ip,C,D)) newpoly.pb(ip);
        }
    }
    return newpoly;
}
double PolygonArea(Polygon &p) {
    double area=0;
    int n=p.size();
    For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2;
} 

//线上不算 
bool OnLeft(Line L,P p) {
    return Cross(L.v,p-L.p)>0;
} 
P GetIntersection(Line a,Line b) {
    V u=a.p-b.p;
    double t = Cross(b.v,u) / Cross(a.v, b.v);
    return a.p + a.v*t;
}
int HalfplaneIntersection(Line *L, int n, P* poly) {
    sort(L,L+n);
    int fi,la;
    P *p = new P[n];
    Line *q = new Line[n];
    q[fi=la=0 ] = L[0];
    For(i,n-1) {
        while (fi < la && !OnLeft(L[i],p[la-1])) la--;
        while (fi < la && !OnLeft(L[i],p[fi])) fi++;
        q[++la] = L[i];
        if (fabs(Cross(q[la].v, q[la-1].v))<eps) {
            la--;
            if (OnLeft(q[la],L[i].p)) q[la] = L[i];
        }
        if (fi<la) p[la-1] = GetIntersection(q[la-1],q[la]);
    }
    while(fi < la && !OnLeft(q[fi],p[la-1])) la--;
    if (la-fi<=1) return 0; 
    p[la] = GetIntersection(q[la],q[fi]);

    int m=0;
    Fork(i,fi,la) poly[m++]=p[i];
    return m;
} 

// hypot(double x,double y); return sqrt(x*x+y*y)

double rotating_calipers(P *p,int n) {
    int q=1;
    double ans=0;
    Rep(i,n) {
        while(fabs(Area2(p[i],p[(i+1)%n],p[(q+1)%n]))> fabs(Area2(p[i],p[(i+1)%n],p[q] )) ) 
            q=(q+1)%n;
        ans = max( ans, (double)max(Length(p[i]- p[q]), Length(p[(i+1)%n] -p[q] ) ) );          
    }
    return ans;
}
// rotating for rect
double rec_rotating_calipers(P *p,int n) {
    int q=1;
    double ans1=1e15,ans2=1e15;
    int l=0,r=0;
    Rep(i,n) {
        while(dcmp(fabs(Area2(p[i],p[(i+1)%n],p[(q+1)%n])) - fabs(Area2(p[i],p[(i+1)%n],p[q] ))) >0  ) 
            q=(q+1)%n;
        while (dcmp(Dot(p[(i+1)%n]-p[i],p[(r+1)%n]-p[r]))>0 )  r=(r+1)%n;
        if (!i) l=q;
        while (dcmp(Dot(p[(i+1)%n]-p[i],p[(l+1)%n]-p[l]))<0 )  l=(l+1)%n;
        double d = Length(p[(i+1)%n]-p[i]);
        double h = fabs(Area2(p[i],p[(i+1)%n],p[q] ))/d;
        double w=(Dot(p[(i+1)%n]-p[i],p[r]-p[i]) - Dot(p[(i+1)%n]-p[i],p[l]-p[i]) ) /d;
        ans1=min(ans1,2*(h+w)),ans2=min(ans2,h*w);
    }
    printf("%.2lf %.2lf\n",ans2,ans1);
}


//去共线化 
void simplify(Polygon& poly) {
    Polygon ans;
    int n=SI(poly);
    Rep(i,n) {
        if (dcmp(Cross(poly[i]-poly[(i+1)%n],poly[(i+1)%n]-poly[(i+2)%n]))!=0)
            ans.pb(poly[(i+1)%n]);
    }
    n=SI(ans);
    cout<<n<<endl;
    Rep(i,n) printf("%.4lf %.4lf\n",ans[i].x,ans[i].y);
}

int getSegCircleIntersection(Line L,C cir,vector<P> & sol) {
    if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
        P A= GetLineProjection(cir.c,L.p,L.p+L.v);
        if (OnSegment(A,L.p,L.p+L.v) || L.p==A || L.p+L.v==A  ) 
            sol.pb(A);
        return sol.size();
    }
    double t1,t2;
    double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
    double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
    double delta = f*f - 4*e*g;
    if (dcmp(delta)<0) return 0;
    else if (dcmp(delta)==0) {
        t1 = -f / (2*e); 
        if (dcmp(t1)>=0&&dcmp(t1-1)<=0) {
            sol.pb(L.point(t1));
        }
        return sol.size();
    } 
    t1 = (-f - sqrt(delta)) / (2*e); if (dcmp(t1)>=0&&dcmp(t1-1)<=0) sol.pb(L.point(t1));
    t2 = (-f + sqrt(delta)) / (2*e); if (dcmp(t2)>=0&&dcmp(t2-1)<=0) sol.pb(L.point(t2));
    if(SI(sol)==2 && t1>t2) swap(sol[1],sol[0]);
    return sol.size();
}
int isPointInOrOnCircle(P p,C c) {
    return dcmp(Length(p-c.c)-c.r)<=0;
}

// Triangle(O,A,B) and Circle(O,m)
double CircleTriangleArea(P A,P B,double m) {
    double ans=0;
    C c=C(P(),m); P O=c.c;
    if (A==O||B==O) return 0;
    bool b = isPointInOrOnCircle(A,c);
    bool b2 = isPointInOrOnCircle(B,c);
    double opr;
    if (dcmp(Area2(O,A,B))>=0) opr=1; else opr=-1;
    if (b&&b2) {
        ans+=opr*fabs(Area2(A,B,O))/2; 
    } else if (!b&&!b2){
        Line l=Line(A,B-A);
        vector<P> sol;
        getSegCircleIntersection(l,c,sol);
        if (SI(sol)==2) {
            ans+=opr*fabs(Area2(sol[0],sol[1],O))/2;
            ans+=opr*m*m/2*(Angle(A,sol[0])+Angle(sol[1],B));
        } else {
            ans+=opr*m*m/2*(Angle(A,B));
        }
    } else {
        Line l=Line(A,B-A);
        vector<P> sol;
        getSegCircleIntersection(l,c,sol);
        if (SI(sol)==2) {
            ans+=opr*fabs(Area2(sol[0],sol[1],O))/2;
            ans+=opr*m*m/2*(Angle(A,sol[0])+Angle(sol[1],B));
        } else if(b) { 
            ans+=opr*fabs(Area2(sol[0],A,O))/2;
            ans+=opr*m*m/2*(Angle(sol[0],B));
        } else {
            ans+=opr*fabs(Area2(sol[0],B,O))/2;
            ans+=opr*m*m/2*(Angle(sol[0],A));
        }
    }
    return ans;
}
double max_polygon_inside_segment(Polygon p) {
    int n=SI(p);
    double ans=0;
    Rep(i,n) Fork(j,i+1,n-1) {
        P A=p[i],B=p[j];
        if (A==B) continue;
        Polygon poly;
        poly.pb(A); poly.pb(B);
        Rep(k,n) {
            P C=p[k],D=p[(k+1)%n];
            if (IsParallel(A,B,C,D)) {
                if (IsCollinear(A,B,C,D)) {
                    poly.pb(C);
                    poly.pb(D);
                }
            } else {
                P t=GetLineIntersectionB(A,B,C,D);
                poly.pb(t);
            }
        }
        sort(ALL(poly));
        poly.erase(unique(ALL(poly)),poly.end());
        int sz=SI(poly);
        double l=0; bool fl=0;
        For(i,sz-1) {
            P m=(poly[i]+poly[i-1])/2;
            if (isPointInPolygon(m,p)) {
                if(!fl) l=Length(poly[i]-poly[i-1]);
                else l+=Length(poly[i]-poly[i-1]);
                ans=max(ans,l);
                fl=1;
            }else fl=0;
        }
    }
    return ans;
}
double Jinggai_problems(P *p,int n) {
    int q=1;
    double ans=INF;
    Rep(i,n) {
        while(fabs(Area2(p[i],p[(i+1)%n],p[(q+1)%n]))> fabs(Area2(p[i],p[(i+1)%n],p[q] )) ) 
            q=(q+1)%n;
        ans = min( ans, (double)DistanceToSegment(p[q],p[i],p[(i+1)%n]) );          
    }
    return ans;
}
int n;
#define MAXN 912345
P a[MAXN];
double r;
double calc(P t) {
    double s=0;
    Rep(i,n) {
        P A=a[i],B=a[(i+1)%n];
        s+=CircleTriangleArea(A-t,B-t,r);
    } 
    return s;
}
P d[4]={P(0,1),P(1,0),P(0,-1),P(-1,0)};
double solve() {
    double T=100;
    P o=P();
    Rep(i,n) o=o+a[i]; o=o/n;
    double ans=calc(o);
    double eps=0.000001;
    T=0;
    Rep(i,n) Rep(j,n) gmax(T,(double)Length(a[i]-a[j]) );
    while(T>eps)
    {
        double pre=ans;
        double f=calc(o);
        double fx=calc(o+P(eps,0));
        double fy=calc(o+P(0,eps));
        double fx2=(fx-f) / eps;
        double fy2=(fy-f) /eps;
        P v(fx2,fy2);
        v=v/Length(v)*T;
        P o2=o+v;
        if (calc(o2)>calc(o)) {
            o=o2;
        }else T*=0.9;

    }
    return calc(o);
}

int main()   
{
    freopen("J.in","r",stdin);
    n=read();
    r;cin>>r;
    Rep(i,n) a[i]=read_point();
    cout<<setiosflags(ios::fixed)<<setprecision(10);
    double ans=calc(a[1]);
    P o=P();
    Rep(i,n) o=o+a[i]; o=o/n;
    printf("%.10lf\n",solve());

    return 0;
}

K Black and White Boxes

超实数博弈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值