Topcoder 370 Div1 500

Problem

A and B are two cities distance units away from each other. Several transmitters have been placed along the straight road connecting them. The transmission range can be set to any positive integer value, but it must be the same for all transmitters. Any two transmitters can communicate directly if the distance between them is not greater than the transmission range. Each transmitter can communicate with city A or city B if the distance between the transmitter and the city is not greater than the transmission range.

You have been assigned to set up a connection between the cities. You are allowed to move any number of transmitters, but moving a transmitter for k units costs you k dollars and the budget does not allow you to spend more than funds dollars in total. You can move the transmitters into points with integer coordinates only.

You will be given a vector position, with the i-th element of position representing the initial distance between the i-th transmitter and city A. You will be also given funds, the maximal total cost you are allowed to spend when moving transmitters. Return the minimal transmission range which still allows you to establish a connection between the cities. See notes for the formal definition of the connection.

Note

Cities A and B are connected if there exists a sequence of transmitters t1, t2, …, tn such that transmitter t1 can communicate directly with city A, transmitter tn can communicate directly with city B and, for every i between 1 and n-1, inclusive, transmitters ti and ti+1 can communicate directly.

Constraints

distance will be between 1 and 100, inclusive.

position will contain between 1 and 50 elements, inclusive.

Each element of position will be between 0 and distance, inclusive.

funds will be between 0 and 100000, inclusive.

Solution

dp。设dp[i][j][k]表示第 i 个点,位置在 j ,此时传送范围为k时的最小花费。有个显然的猜想,得到最优结果的情况下,所有点的相对位置不会变。我们把positon从小到大排序,Note里的那句话告诉我们,此时只需从 i 推到 i+1 即可。dp[i][j][k]=min(dp[i][j][k],dp[i-1][j1][k]+abs(j-p[i]) ),但前提是 abs(j-j1)<=k,且dp[i-1][j1][k]+abs(j-p[i])<=funds。

最后在dp[n][j][k]中找到最小的 j 即可,但前提是 abs(j-distance)<=k。

猜想

上述做法复杂度是O(N^4)的,topcoder上可以过,因为topcder快,且由于限制条件,很多情况不会跑到。但有没有优化到O(N^3)的方法?… 待想,如果有,再更新…

My code

//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
//const double pi=acos(-1.0);
//const double eps=1e-9;
#define MAXN 1000100
#define N
#define M
int dp[100][200][200];
int p[100];
class ConnectTheCities{
public:
    int minimalRange(int distance, int funds, vector <int> position){
        clr_INT(dp);
        sort(position.begin(),position.end());
        int len=gsize(position);
        p[0]=0;
        rep(i,0,len){
            p[i+1]=position[i];
        }
        p[len+1]=distance;
        repin(k,1,distance){
            dp[0][0][k]=0;
        }
        repin(i,1,len){

        }
        int res=INT;
        repin(j,0,distance){
            repin(k,1,distance){
                if(dp[len][j][k]>funds) continue;
                if(abs(j-p[len+1])>k) continue;
                res=min(res,k);
            }
        }
        return res;
    }
};

你可能感兴趣的:(dp,topcoder)