zoukankan      html  css  js  c++  java
  • xtu summer individual 5 F

    Post Office

    Time Limit: 1000ms
    Memory Limit: 10000KB
    This problem will be judged on PKU. Original ID: 1160
    64-bit integer IO format: %lld      Java class name: Main
     
     
    There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates. 

    Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum. 

    You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office. 
     

    Input

    Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.
     

    Output

    The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.
     

    Sample Input

    10 5
    1 2 3 6 7 9 11 22 44 50

    Sample Output

    9

    Source

     
    解题:dp,dp[i][j]表示i个邮局负责j个村子时的最短距离和。距离和最少?肯定是选取的点如果在第i个与第n-i 个村子的中点(1 <= 1 <= n/2),距离和会最小啊!
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f3f
    11 using namespace std;
    12 int dp[35][301],x[301],n,m;
    13 int dis(int i,int j){
    14     int sum = 0;
    15     while(i < j) sum += x[j--]-x[i++];
    16     return sum;
    17 }
    18 int main(){
    19     int i,j,k;
    20     while(~scanf("%d%d",&m,&n)){
    21         memset(dp,0,sizeof(dp));
    22         for(i = 1; i <= m; i++){
    23             scanf("%d",x+i);
    24             dp[1][i] = dis(1,i);
    25         }
    26         for(i = 2; i <= n; i++){
    27             for(j = i; j <= m; j++){
    28                 dp[i][j] = INF;
    29                 for(k = i-1; k < j; k++)
    30                     dp[i][j] = min(dp[i][j],dp[i-1][k]+dis(k+1,j));
    31             }
    32         }
    33         cout<<dp[n][m]<<endl;
    34     }
    35     return 0;
    36 }
    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int INF = 0x3f3f3f3f;
     6 const int maxn = 310;
     7 int w[maxn][maxn],dp[maxn][maxn],a[maxn],n,m;
     8 int main() {
     9     while(~scanf("%d%d",&n,&m)) {
    10         for(int i = 1; i <= n; ++i) scanf("%d",a + i);
    11         memset(w,0,sizeof w);
    12         for(int i = 1; i <= n; ++i) {
    13             for(int j = i + 1; j <= n; ++j)
    14                 w[i][j] = w[i][j-1] + a[j] - a[(i+j)>>1];
    15         }
    16         for(int i = 1; i <= n; ++i) {
    17             dp[i][i] = 0;
    18             dp[i][1] = w[1][i];
    19         }
    20         for(int j = 2; j <= m; ++j) {
    21             for(int i = j + 1; i <= n; ++i) {
    22                 dp[i][j] = INF;
    23                 for(int k = j-1; k < i; ++k)
    24                     dp[i][j] = min(dp[i][j],dp[k][j-1] + w[k+1][i]);
    25             }
    26         }
    27         printf("%d
    ",dp[n][m]);
    28     }
    29     return 0;
    30 }
    View Code

    还是太年轻,蓝桥杯决赛有题就是这个,当时做的时候,没有理解这题的精髓,太傻了

    平行四边形优化

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int INF = 0x3f3f3f3f;
     6 const int maxn = 310;
     7 int w[maxn][maxn],dp[maxn][maxn],a[maxn],s[maxn][maxn],n,m;
     8 int main() {
     9     while(~scanf("%d%d",&n,&m)) {
    10         for(int i = 1; i <= n; ++i) scanf("%d",a + i);
    11         memset(w,0,sizeof w);
    12         for(int i = 1; i <= n; ++i) {
    13             for(int j = i + 1; j <= n; ++j)
    14                 w[i][j] = w[i][j-1] + a[j] - a[(i+j)>>1];
    15         }
    16         for(int i = 1; i <= n; ++i) {
    17             dp[i][i] = 0;
    18             dp[i][1] = w[1][i];
    19             s[i][1] = 0;
    20         }
    21         for(int j = 2; j <= m; ++j) {
    22             s[n+1][j] = n;
    23             for(int i = n; i > j; --i) {
    24                 dp[i][j] = INF;
    25                 for(int k = s[i][j-1]; k <= s[i+1][j]; ++k){
    26                     int tmp = dp[k][j-1] + w[k+1][i];
    27                     if(tmp < dp[i][j]){
    28                         dp[i][j] = tmp;
    29                         s[i][j] = k;
    30                     }
    31                 }
    32             }
    33         }
    34         printf("%d
    ",dp[n][m]);
    35     }
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    8 网站用户密码保存
    10 XSRF和XSS
    评分预测
    社会化推荐
    借助上下文信息
    UGC
    冷启动
    Git秘籍:在 Git 中进行版本回退
    Google在三大系统上停止对Chrome Apps的支持
    Windows 的 AD 域寄生于 Linux 机器
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3898285.html
Copyright © 2011-2022 走看看