AtCoder Beginner Contest 145

Portal

A - Circle

Sign.

B - Echo

To sign.

C - Average Length

To accuracy in the card, using a bipartite carry out his or long double.


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/16 20:04:44
 */
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 10;
const double eps = 1e-6;
int n;
struct Point{
    int x, y;   
}p[N];
int a[N];
double dis(Point A, Point B) {
    double tmp = 1.0 * (A.x - B.x) * (A.x - B.x) + 1.0 * (A.y - B.y) * (A.y - B.y);   
    double l = 0, r = tmp, mid;
    for(int i = 1; i <= 500; i++) {
        mid = (l + r) / 2;
        if(mid * mid < tmp) l = mid;
        else r = mid;   
    }
    return r;
}
void run(){
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;
    for(int i = 1; i <= n; i++) a[i] = i;
    double ans = 0;
    int tot = 0;
    do {
        ++tot;
        for(int i = 2; i <= n; i++) {
            ans += dis(p[a[i]], p[a[i - 1]]);
        }
    } while(next_permutation(a + 1, a + n + 1));
    ans = ans / tot;
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

D - Knight

Meaning of the questions:
There are a pawn located in \ ((0,0) \) point, when a stone is located in \ ((i, j) \ ) , you can jump to the \ ((i + 1, j + 2), (i + 2, j + 1) \ ) these two cells.
Q. How many different ways can the point \ ((the X-, the y-) \) .

Ideas:
Since \ (the X-, the y-\ Leq 10 ^ 6 \) , apparently directly \ (dp \) can not.
Then find the law, we jumped back from the end to simulate it and found eventually forming a plurality of slope \ (1 \) straight answer every relevant point jump straight line above a number of combinations.
Then found that the final \ ((0,0) \) layers point as \ (\ FRAC {X} + {Y}. 3 \) , the final answer is \ (\ frac {x + y } {3} \ choose t \) .
Details on paper what came out.


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/16 20:26:13
 */
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 15, MOD = 1e9 + 7;

int fac[N];

ll qpow(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;   
    }
    return ans;   
}

int C(int n, int m) {
    return 1ll * fac[n] * qpow(fac[n - m], MOD - 2) % MOD * qpow(fac[m], MOD - 2) % MOD;
}

void run(){
    fac[0] = 1;
    for(int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
    int x, y;
    cin >> x >> y;
    if((x + y) % 3 != 0) {
        cout << 0;
        return;
    }
    int t = (x + y) / 3;
    if(y - t >= 0 && y - t <= t) {
        cout << C(t, y - t);
    } else cout << 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

E - All-you-can-eat

Meaning of the questions:
Now there \ (n \) dish, each dish needs \ (a_i \) time to eat, there \ (b_i \) of deliciousness.
There are the following rules:

  • After the point of a dish, you can only be to point to the next dish after eating.
  • \ (T \) after the time you can not take your order, but can still bite.
  • Vegetables only once every point.

Finally asked finally be able to get the maximum deliciousness is.

Ideas:

  • Note that the more important thing, no matter what the final order is a la carte, we can be certain of a dish arrange \ (T \) time to the point.
  • So after the problem becomes very simple: a total of \ (T-1 \) time, then the equivalent of a backpack, which directly \ (dp \) need \ (O (the n-^ 2) \) .
  • But we need to enumerate what dish at the last point, so the total complexity is \ (O (n ^ 3) \) apparently can not afford the time complexity.
  • Since we only concern is one dish not vote, then we may be pretreated before a suffix \ (dp \) , \ (DP1 [i] [J] \) represents \ (1 \) ~ \ (i \) the article, the total time does not exceed \ (J \) is the maximum degree of taste, \ (DP2 is [I] [J] \) Similarly.
  • The final time complexity is \ (O (^ n-2) \) .

Direct questions to do this because you can exceed the time limit, so the definition of a bad state, but after the last Imperial location problem has been converted.
Prefix and suffix \ (dp \) pretreatment is quite clever QAQ.


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/18 15:30:25
 */
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 3005;

int n, t;
int a[N], b[N];
int dp1[N][N], dp2[N][N];

void run(){
    for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= t - 1; j++) {
            dp1[i][j] = dp1[i - 1][j];
            if(j >= a[i]) 
                dp1[i][j] = max(dp1[i][j], dp1[i - 1][j - a[i]] + b[i]);
        }   
    }
    for(int i = n; i >= 1; i--) {
        for(int j = 1; j <= t - 1; j++) {
            dp2[i][j] = dp2[i + 1][j];
            if(j >= a[i])
                dp2[i][j] = max(dp2[i][j], dp2[i + 1][j - a[i]] + b[i]);   
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= t - 1; j++) {
            ans = max(ans, dp1[i - 1][j] + dp2[i + 1][t - 1 - j] + b[i]);
        }   
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n >> t) run();
    return 0;
}

 
The problem there is a more subtle solution: Suppose we know what the final point of the dish, then obviously we always put the most time-consuming arrangements \ (T \) time to the point.
Then converted to a further question: Suppose handpicked the last one, the previous select only takes no more than it.
Therefore, the above process directly \ (DP1 [I] [J] \) , while maintaining a maximum value for the suffix.


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/18 15:30:25
 */
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 3005;

int n, t;
struct node {
    int a, b;   
    bool operator < (const node &A) const {
        return a < A.a;   
    }
}p[N];
int maxv[N];
int dp[N][N];

void run(){
    for(int i = 1; i <= n; i++) cin >> p[i].a >> p[i].b;
    sort(p + 1, p + n + 1);
    for(int i = n; i >= 1; i--) maxv[i] = max(maxv[i + 1], p[i].b);
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= t - 1; j++) {
            dp[i][j] = dp[i - 1][j];
            if(j >= p[i].a) dp[i][j] = max(dp[i][j], dp[i - 1][j - p[i].a] + p[i].b);
        }
        ans = max(ans, dp[i][t - 1] + maxv[i + 1]);
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n >> t) run();
    return 0;
}

F - Laminate

The meaning of problems:
there are \ (n-\) th row with a column, each column has a height \ (H_i, 0 \ Leq H_i \ Leq. 9 ^ 10 \) .
There are at most \ (k \) chance of some arbitrarily modify the height of the column.
After performs operations: erasing each laterally for a continuous column. Q. final minimum number of operations is.

Ideas:
This title feels nothing directly to do too much food ideas ... 555.
Feel very abstract issue, considering many cases, the general feeling visualize the problem, concrete is the key to solving problems, such as the title, we consider \ (k = 0 \) case, the final answer is:
\ [\ sum_ {i = 1}
^ {n} max (0, h_i-h_ {i-1}) \] then there conclusion: If you modify a column, its position is assumed \ (I \) , then the final its height \ ([h_ {i-1 }, h_ {i + 1}] \) between optimal.

This conclusion is more clearly, we can directly Imperial: modifying operation and even if \ (H_i = H_-I. 1} {\) .
Further observation can be found, the operation is equivalent to deleting a column.

In contrast to the solution at this time, you can make \ (dp [i] [j ] \) represents the former \ (I \) pillars Reserved \ (J \) a minimum number of operations root.
Then metastasis: \ (DP [I] [J] = min \ {DP [K] [-J. 1] + max (0, H_i-h_k), J <I \} \) .
Direct \ (O (n ^ 3) \) carry out his gone.

PS: This may be later \ (max \) operating to open and maintain two values in a tree to optimize the array, the complexity can be achieved \ (O (n ^ 2logn) \)


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/18 18:16:10
 */
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 300 + 5;

int n, K;

int h[N];
ll dp[N][N];

void run(){
    for(int i = 1; i <= n; i++) cin >> h[i];
    memset(dp, INF, sizeof(dp));
    dp[0][0] = 0;
    for(int i = 1; i <= n - K; i++) {
        for(int j = i; j <= n; j++) {
            for(int k = 0; k < j; k++) {
                dp[j][i] = min(dp[j][i], dp[k][i - 1] + max(0, h[j] - h[k]));   
            }
        }   
    }
    ll ans = INF;
    for(int i = 1; i <= n; i++) ans = min(ans, dp[i][n - K]);
    if(ans == INF) ans = 0;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n >> K) run();
    return 0;
}

Guess you like

Origin www.cnblogs.com/heyuhhh/p/11887825.html