Part of the question in the 2014 Anshan field competition.

Source: Internet
Author: User

First, 1004 is a difficult question.

The position of N stars is given. All the mass values are 1. In one-dimensional coordinates, k stars can be moved, W indicates the mass, and di indicates the distance from the center of the center, the minimum I value is required.


First, let's talk about the definition of the center of mass: Quality and position score and divide by quality and.


So we can think like this: if we move K, then FF = n-k will be left, and the adjacent ones will be the best. We can use O (N) the complexity of enumerate all adjacent FF intervals, and then calculate a minimum value.

If we know I, we need to know di ^ 2. Assume that the center of the image is X and the coordinate is Xi. (x-xi) ^ 2 = x ^ 2 + Xi ^ 2-2 * x * XI. We can pre-process a prefix and sum of the squares of the prefix,


The point that many people have never done is that it is better not to move, and it is better to consider not moving, to a minimum value.

AC code:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define Del(a,b) memset(a,b,sizeof(a))const int N = 70000;double a[N],sum[N];double seq[N];int main(){    //freopen("Input.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        int n,k;        scanf("%d%d",&n,&k);        int ff = n-k;        for(int i=1;i<=n;i++)            scanf("%lf",&a[i]);        if(ff== 0 || ff==1)        {            puts("0");            continue;        }        sort(a+1,a+n+1);        sum[0] = 0;        seq[0] = 0;        for(int i=1;i<=n;i++)        {            sum[i]=sum[i-1]+a[i];            seq[i] = seq[i-1] + (a[i]*a[i]);        }//        for(int i=0;i<n;i++)//            printf("%.2lf ",sum[i]);        double tmp = sum[n] / ff;        double ans = seq[n] + ff *tmp *tmp - 2*tmp*sum[n];        for(int  i = ff;i<=n;i++)        {            //printf("YES%d\n",ff);            tmp = (sum[i] - sum[i-ff])/ff;            double pps = 0;            pps = (seq[i] - seq[i-ff]) + ff*(tmp*tmp) - 2 * tmp *(sum[i]-sum[i-ff]);//            for(int j = i-ff+1;j<=i;j++)//                pps+=((a[i]-tmp)*(a[i]-tmp));            //printf("tmp:%.5lf \n",pps);            ans = min(pps,ans);        }        printf("%.10lf\n",ans);    }    return 0;}

Then 1005 water question DP

Directly enumerate all cases DP [I] [J] the result of the first I after the merge is J.

Transfer equation:

 dp[i][k] = max(dp[i][k],dp[i-1][j]+a[j][k]);

Directly run the Code:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define Del(a,b) memset(a,b,sizeof(a))const int N = 120;int dp[N][60];int a[60][60];int s[N];int main(){    //freopen("Input.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++)            for(int j = 1;j<=m;j++)                scanf("%d",&a[i][j]);        memset(dp,-1,sizeof(dp));        for(int i=1;i<=n;i++)        {            scanf("%d",&s[i]);        }        if(s[1]!=-1)            dp[1][s[1]] = 0;        else            for(int i=1;i<=m;i++)                dp[1][i] = 0;        for(int i=2;i<=n;i++)        {            for(int j = 1;j<=m;j++)            {                if(dp[i-1][j] == -1)                    continue;                if(s[i]!=-1)                {                    //printf("NO\n");                    dp[i][s[i]] = max(dp[i][s[i]],dp[i-1][j]+a[j][s[i]]);                    continue;                }                for(int k = 1;k<=m;k++)                {                    //printf("YES\n");                    dp[i][k] = max(dp[i][k],dp[i-1][j]+a[j][k]);                }            }        }        int ans = 0;        for(int i=1;i<=m;i++){            //printf("%d %d\n",i,dp[n][i]);            ans = max(ans,dp[n][i]);        }        printf("%d\n",ans);    }    return 0;}

1009: sign-in question

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int N = 1005;struct POINT{    double x, y;    double t;}p[N];double dis(POINT a, POINT b){    return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));}int main(){    int T;    scanf("%d", &T);    while(T --){        int n;        scanf("%d", &n);        for(int i = 0; i < n; i ++){            scanf("%lf %lf %lf", &p[i].t, &p[i].x, &p[i].y);        }        double ans = 0.0;        for(int i = 1; i < n; i ++){            ans = max(ans, dis(p[i], p[i - 1]) / (p[i].t - p[i -1].t));        }        printf("%.10lf\n", ans);    }    return 0;}


Part of the question in the 2014 Anshan field competition.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.