2020-2021 ACM-ICPC, Asia Nanjing Regional Contest (XXI Open Cup, Grand Prix of Nanjing) | JorbanS

E - Evil Coordinate

#include 
#include 
#include 
using namespace std;
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
#define aa first
#define bb second
const string no = "Impossible";

const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
int n, mx, my;
string s;
string op = "UDLR";

void solve() {
    cin >> mx >> my >> s;
    n = s.size();
    int cnt[4] = {0};
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < 4; j ++)
            if (s[i] == op[j]) cnt[j] ++;
    vector<int> v{0, 1, 2, 3};
    for (int _ = 0; _ < 24; _ ++) {
        int x = 0, y = 0;
        for (int i = 0; i < 4; i ++) {
            int xx = x + dx[v[i]] * cnt[v[i]], yy = y + dy[v[i]] * cnt[v[i]];
            int minx = min(xx, x), maxx = max(xx, x);
            int miny = min(yy, y), maxy = max(yy, y);
            if (xx == x && x == mx) {
                if (miny <= my && my <= maxy) break;
            } else if (yy == y && y == my) {
                if (minx <= mx && mx <= maxx) break;
            }
            if (i == 3) {
                for (int j = 0; j < 4; j ++)
                    while (cnt[v[j]] --) cout << op[v[j]];
                return;
            }
            x = xx, y = yy;
        }
        next_permutation(v.begin(), v.end());
    }
    cout << no;
}

int main() {
    Cases
    {
        solve();
        cout << endl;
    }
    return 0;
}

F - Fireworks

#include 
#include 
using namespace std;
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)

const double eps = 1e-6;
double n, m, p;

double cal(double k) {
    return (m + k * n) / (1 - pow(1 - p, k));
}

double solve() {
    cin >> n >> m >> p;
    p /= 1e4;
    double l = 1, r = 2e9;
    while (abs(l - r) >= eps) {
        double mid = (l + r) / 2;
        if (cal(mid - eps) < cal(mid + eps)) r = mid;
        else l = mid;
    }
    return min(cal(floor(l)), cal(ceil(l)));
}

int main() {
    Cases
    printf("%.6lf\n", solve());
    return 0;
}

K - K Co-prime Permutation

#include 

using namespace std;
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
int n, m;

void solve() {
    cin >> n >> m;
    if (!m) {
        cout << -1 << endl;
        return;
    }
    if (n == 1) {
        cout << (m == 1 ? 1 : -1) << endl;
        return;
    }
    if (m & 1) {
        cout << 1 << ' ';
        m --;
        for (int i = 2; i <= n; i ++) {
            if (m) {
                cout << i + 1 << ' ' << i << ' ';
                m -= 2;
                i ++;
            } else cout << i << ' ';
        }
    } else {
        for (int i = 1; i <= n; i ++) {
            if (m) {
                cout << i + 1 << ' ' << i << ' ';
                m -= 2;
                i ++;
            } else cout << i << ' ';
        }
    }
}

int main() {
    solve();
    return 0;
}

L - Let’s Play Curling

#include 
#include 
#include 
#include 
using namespace std;
#define Cases int __; cin >> __; for (int _ = 1; _ <= __; _ ++)
const string no = "Impossible";
#define aa first
#define bb second

typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 2;
int n, m;

int a[N], b[N];

void solve() {
    cin >> n >> m;
    vector<pair<int, char>> v;
    map<int, bool> mp;
    for (int i = 0; i < n; i ++) {
        int x; cin >> x;
        v.emplace_back(x, 'a');
    }
    for (int i = 0; i < m; i ++) {
        int x; cin >> x;
        mp[x] = true;
        v.emplace_back(x, 'b');
    }
    sort(v.begin(), v.end());
    int res = 0, cnt = 0;
    for (auto it : v) {
        if (it.bb == 'a' && !mp[it.aa]) cnt ++;
        else {
            res = max(res, cnt);
            cnt = 0;
        }
    }
    res = max(res, cnt);
    if (res) cout << res << endl;
    else cout << no << endl;
}

int main() {
    Cases
    solve();
    return 0;
}

你可能感兴趣的:(OI,题解,算法,c++,开发语言)