HDU 5531(Rebuild- 三分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nike0good/article/details/51857568

题意:已知a1+a2,a2+a3,...,an+a1,求min(a21+a22+...+a2n)
只要知道a_1,就能求出整个序列(头尾需要特判)
奇数时可以直接求出来,偶数时不行
容易写出ak=kiai+ci的形式,保证每个数都非负就行
然后三分

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip> 
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
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())
typedef long long ll;
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;
} 
#define MAXN (10000+10)
pair<double,double> p[MAXN];
double c[MAXN];
double a[MAXN];
const double eps=1e-10;
int dcmp(double x) {
    if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1; 
}
int n;
typedef long double ld;
ld PI = 3.141592653589793238462643383;
double R[MAXN];
bool calc(double r0,double &S) {
    R[0]=r0;
    S=0;
    For(i,n-1) {
        R[i]=a[i];
        if (i%2==0) R[i]+=r0;
        else R[i]-=r0;
        if (dcmp(R[i])<0) return 0; 
    }
    Rep(i,n) S+=R[i]*R[i];
    return 1;
}
int main()
{
//  freopen("C.in","r",stdin);
//  freopen(".out","w",stdout);
    char s[]="IMPOSSIBLE";
    int T=read();
    while(T--) {
        n=read();
        Rep(i,n) scanf("%lf%lf",&p[i].fi,&p[i].se);
        Rep(i,n) c[i]=hypot(p[i].fi-p[(i+1)%n].fi,p[i].se-p[(i+1)%n].se);
        a[0]=0;
        For(i,n) {
            a[i]=c[i-1]-a[i-1];
        }
        double l=0,r=1e9;
        Rep(i,n) {
            if (i%2==0) l=max(l,-a[i]);
            else r=min(r,a[i]);
        }
        if (dcmp(r-l)<0) puts(s);
        else {
            if (n%2==0 && dcmp(a[n])!=0) puts(s);
            else if (n&1) {
                double r0=a[n]/2;
                if (dcmp(r0-l)<0||dcmp(r-r0)<0) puts(s);
                else {
                    double S;
                    if (!calc(r0,S) ) {
                        puts(s); continue;
                    } 
                    S*=PI;
                    printf("%.2lf\n",S);
                    Rep(i,n) printf("%.2lf\n",R[i]);
                }
            } else {
                double S1,S2;
                For(tft,250) {
                    double m1=l+(r-l)/3;
                    double m2=r-(r-l)/3;
                    calc(m1,S1);
                    calc(m2,S2);
                    if (S1<S2) r=m2; else l=m1;
                } 
                S1*=PI;
                printf("%.2lf\n",S1);
                Rep(i,n) printf("%.2lf\n",R[i]);

            }
        } 


    }

    return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页