zoukankan      html  css  js  c++  java
  • URAL

    Two students are playing the following game. There are 2· n points on the plane, given with their coordinates ( xiyi). Each move player paints the point with his own color (first with white, second with black). The first student makes odd moves, second student makes even moves. When all points are painted (each student made n moves), the game finishes. Each student gets amount of points (real number) that equals to the sum of all distances among pairs of points, colored with his color. Student who get more points becomes a winner. The students play optimally. Find and print the difference between points amount of winner and looser.
    Input
    Contains multiple test cases. The first line of each case contains positive integer number n ( n ≤ 500). Next 2· n lines contain points' coordinates ( x 1, y 1), ( x 2, y 2), …, ( x 2n, y 2n).
    Output
    For each test case output the difference between the points of winner and looser. Output the difference with three digits after decimal point.
    Example
    inputoutput
    2
    0 0
    0 1
    1 0
    1 1
    2
    0 0
    1 0
    0 3
    1 5
    
    0.000
    1.937
    

    思路:之前遇到了类似的题。由于这样的题比较抽象,当时我是陷入了矛盾的,A既要让自己越大越好,又要使B越小越好(B同理),这两个标准会不会又矛盾呢,该遵循哪一个呢。

    此题可以推出来,二则在使自己大的时候,也约束 了对方更小。

    val A= Sum( distant(pi,pj) ) { i<j && i,j belong to A}   - Sum( distant(pi,pj) ) {i<j && i,j belong to B}

            = Sum( distant(pi,pj) ) {i,j belong to Q}  - Sum( distant(pi,pj) ) { i belong to B && j belong to Q}

    (其中Q是全集。

    我们令dis是点到其他所有点的距离之和,那么A和B都按照dis从大到小选,既能要自己更大,也能让对方更小。

    (下次再遇到这样的题,就用公式,把他们的标准统一就好了(如果可以的话)。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    double x[maxn],y[maxn],dis[maxn],ans;int p[maxn];
    bool cmp(int w,int v){ return dis[w]>dis[v]; }
    double dist(int u,int v){
        return sqrt((x[u]-x[v])*(x[u]-x[v])+(y[u]-y[v])*(y[u]-y[v]));
    }
    int main()
    {
        int N;
        while(scanf("%d",&N)==1){ N<<=1;
            for(int i=1;i<=N;i++) scanf("%lf%lf",&x[i],&y[i]);
            for(int i=1;i<=N;i++) p[i]=i;
            for(int i=1;i<=N;i++){
                dis[i]=0.;
                for(int j=1;j<=N;j++) dis[i]+=dist(i,j);
            }
            sort(p+1,p+N+1,cmp); ans=0;
            for(int i=1;i<=N;i++){
                double tmp=0.0;
                for(int j=i+2;j<=N;j+=2) tmp+=dist(p[i],p[j]);
                if(i&1) ans+=tmp; else ans-=tmp;
            }
            printf("%.3lf
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    内蒙古草原之行
    【iOS开发笔记25/50】:正则表达式
    读书笔记:《写给大家看的设计书》
    【iOS开发笔记22/50】:恼人的a valid provisioning profile for this executable was not found错误
    【搞定GTD】打造高效的OmniFocus系统
    【iOS开发笔记24/50】调整UIImage的大小
    【iOS开发笔记26/50】我自己写的苹果应用程序XQViewer终于上架了,解决了一系列的问题,终于挺过来了
    桥牌笔记:双挤
    养成一个习惯需要几年,而毁掉一下习惯只需要一天
    使用SuperMemo背单词2000天,抓图纪念一下!
  • 原文地址:https://www.cnblogs.com/hua-dong/p/10064829.html
Copyright © 2011-2022 走看看