srm 538

http://mlz000.github.io/2015/07/14/srm-538/

題外話

之前準備期末預習加上剛放假浪了一會= =沒有補題,今天開始恢復補題。。

250


Description:

平面上給出N 個點,整數座標。從(0,0) 出發,每次往上下左右四個垂直方向走,要求遍歷所有點至少一次,最後回到給出的N個點中的某個點(不包括原點)。然後走過的距離是曼哈頓距離。給出一個數字代表要求走過的距離爲奇還是爲偶。問能否滿足要求。

Solution

顯然如果要求是偶的話只有全奇時無解,若有一個偶數,則可通過往返的方式訪問其他點最後停留在偶數點,要求爲奇數時同理

Code

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
class EvenRoute {
        public:
        string isItPossible(vector <int> x, vector <int> y, int wantedParity) {
            int n = x.size();
            bool even = 0, odd = 0;
            for (int i = 0; i < n; ++i) {
                if ((abs(x[i]) + abs(y[i])) & 1)    odd = 1;
                else even = 1;
            }
            return (odd && even) || (even && !wantedParity) || (odd && wantedParity) ? "CAN" : "CANNOT";
        }
};

450


Description

給出四種命令。左轉多少度,右轉多少度,前進多遠,後退多遠。先在順序是打亂的。要求給出一種排列使得最終總的位移和最遠。輸出最遠距離即可。

Solution

一個很明顯的直觀感受是儘量走直線會使得答案更大一些。這樣就很簡單了,不妨全向前,然後把調整角度放到後面,最後全向後即可。調整的角度揹包求即可

Code

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
vector<int> a, b;//ang back
int dp[60][360];
const double Pi = acos(-1.0);
class TurtleSpy {
    public:
        double maxDistance(vector <string> commands) {
            int n = commands.size();
            double X = 0.0, Y = 0.0;
            for (int i = 0; i < n; ++i) {
                char s[10];
                int x;
                sscanf(commands[i].c_str(), "%s%d", s, &x);
                if (s[0] == 'f')    X += x;
                else if (s[0] == 'b')   Y += x;
                else if (s[0] == 'l')   a.pb(x);
                else    a.pb(360 - x);
            }
            dp[0][0] = 1;
            for (int i = 0; i < a.size(); ++i) {
                for (int j = 0; j < 360; ++j)
                    if (dp[i][j])   dp[i + 1][(j + a[i]) % 360] = dp[i + 1][j] = 1;
            }
            double t = 0.0;
            for (int i = 0; i < 360; ++i) {
                if (dp[a.size()][i])    t = max(t, X * X + Y * Y - 2.0 * X * Y * cos(Pi * i / 180.0));
            }
            return sqrt(t);
        }
};
發佈了86 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章