2020 Multi-University Training Contest 4

1001 Anti-AK Problem


Blow up the Enemy

#include 
using namespace std;
typedef long long ll;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e3 + 10;
struct wapon {
     
   ll a, d;
   ll val;
} s[N];

int main() {
     

   int t;
   scanf("%d", &t);
   while (t--) {
     
       int n;
       scanf("%d", &n);
       double res = 0.0;
       ll maxx = 0.0;
       ll max0 = INF;
       for (int i = 1; i <= n; i++) {
     
           scanf("%lld%lld", &s[i].a, &s[i].d);
           ll tmp = 100 / s[i].a;

           if (100 > s[i].a && 100 % s[i].a) tmp++;
           
           s[i].val = (tmp - 1) * s[i].d;
           if (s[i].a >= 100) {
     
               maxx = max(maxx, s[i].a);
           }
           max0 = min(max0, s[i].val);
       }
       if (maxx >= 100) {
     
           for (int i = 1; i <= n; i++) {
     
               if (s[i].a >= 100) res += 0.5;
               else res = res + 1.0;
           }
       } else {
     
           for (int i = 1; i <= n; i++) {
     
               if (max0 == s[i].val) res += 0.5;
               else res = res + 1.0;
           }
       }
       printf("%.7f\n", res / (n * 1.0));
   }
   return 0;
}

Contest of Rope Pulling

2020 Multi-University Training Contest 4_第1张图片
以后5秒内 n = 1 0 3 n=10^3 n=103 V = 1 0 3 V=10^3 V=103 的背包dpTLE了,我就随机一遍数组

#include 
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll INF = 1e15;
const int base = 5e4;
const int N = 1e6 + 10;
const int M = 2e4 + 10; // 加起来总共就2e3的大小 偏偏 M=2e3+10 就是RE 
int n, m, k;

ll dp[N], tmp[N];
ll w[M], v[M];
pll a[M];

int main() {
     
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    ll lim = 1e5; // 限制大小真的是靠猜 开1e6T orz
    srand((int) time(0));

    int T;
    cin >> T;
    for (int cs = 1; cs <= T; cs++) {
     
        cin >> n >> m;
        //init
        fill(dp, dp + N, -INF);
        fill(tmp, tmp + N, -INF);
        dp[base] = tmp[base] = 0;

        for (int i = 1; i <= n + m; i++) {
     
            cin >> a[i].first >> a[i].second;
            if (i > n) {
     
                a[i].first = -a[i].first;
            }
        }
        //随机化
        random_shuffle(a + 1, a + 1 + n + m);
        for (int i = 1; i <= n + m; i++) {
     
            w[i] = a[i].first;
            v[i] = a[i].second;
        }
        for (int i = 1; i <= n + m; i++) {
     
            for (int j = min(lim, lim + w[i]); j >= w[i]; j--) {
     
                if (dp[j - w[i]] != INF) {
     
                    tmp[j] = max(dp[j], dp[j - w[i]] + v[i]);
                }
            }
            for (int j = 0; j <= lim ; j++) {
     
                dp[j] = tmp[j];
            }
        }
        cout << dp[base] << endl;
    }
    return 0;
}

Deliver the Cake

#include 
using namespace std;
typedef long long ll;
const ll inf = (1ll << 60);//inf=(1ll<<60)
const int N = 1e6 + 10;
int n, m;

string str;
int s, t;
ll x;

namespace Dijkstra {
     

   vector<pair<int, ll> > E[N];// to dis

   ll d[N][3];
   int inq[N][3];

   void init(int n) {
     
       for (int i = 1; i <= n; i++) {
     
           E[i].clear();
           inq[i][0] = inq[i][1] = inq[i][2] = 0;
           d[i][0] = d[i][1] = d[i][2] = inf;
       }
   }
   
   void add(int x, int y, ll z) {
     
       E[x].push_back(make_pair(y, z));
       E[y].push_back(make_pair(x, z));
   }
   
   struct node {
     
       int to, left;
       ll dis;

       bool operator<(const node &b) const {
     
           return dis > b.dis;
       }
   };

   priority_queue<node> q;
   int left;

   void dijkstra() {
     

       if (str[s] == 'L') {
     
           q.push({
     s, 1, 0});
           d[s][1] = 0;
       } else if (str[s] == 'R') {
     
           q.push({
     s, 0, 0});
           d[s][0] = 0;

       } else {
     
           q.push({
     s, 2, 0});// 2 可以左手也可以右手
           d[s][2] = 0;
       }

       while (!q.empty()) {
     
           node u = q.top();
           q.pop();

           if (inq[u.to][u.left])continue;

           int now = u.to;
           inq[now][u.left] = 1;

           for (int i = 0, sz = E[now].size(); i < sz; i++) {
     
               int v = E[now][i].first;
               ll c = E[now][i].second;

               if (str[v] == 'L') {
     
                   if (u.left == 0) {
     
                       c += x;
                   }
                   left = 1;
               } else if (str[v] == 'R') {
     
                   if (u.left == 1) {
     
                       c += x;
                   }
                   left = 0;
               } else {
     
                   left = u.left;
               }

               if (d[v][left] > u.dis + c) {
     
                   d[v][left] = u.dis + c;
                   q.push({
     v, left, d[v][left]});
               }
           }
       }
       cout << min(d[t][2], min(d[t][0], d[t][1])) << endl;
   }
}
using namespace Dijkstra;


int main() {
     
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   
   ll c;
   int T;
   cin >> T;
   while (T--) {
     
       cin >> n >> m >> s >> t >> x;
       cin >> str;
       str = " " + str;
       init(n);
       for (int i = 1, a, b; i <= m; i++) {
     
           cin >> a >> b >> c;
           add(a, b, c);
       }
       dijkstra();
   }
   return 0;
}

Equal Sentences

#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ull base = 233;
const int N = 1e6 + 10;
int n, m, k;

int a[N];
ll dp[N];

int Hash(string s) {
     
   int len = s.length();
   ull res = 0;
   for (int i = 0; i < len; i++) {
     
       res = res * base + (s[i] - 'a' + 1);
   }
   return res;
}

string s;

int main() {
     
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);

   a[0] = 0;
   int T;
   cin >> T;
   while (T--) {
     
       cin >> n;
       for (int i = 1; i <= n; i++) {
     
           cin >> s;
           a[i] = Hash(s);
       }

       memset(dp, 0, sizeof dp);
       dp[0] = 1;

       for (int i = 1; i <= n; i++) {
     
           if (a[i] == a[i - 1]) {
     
               dp[i] = dp[i - 1];
           } else {
     
               dp[i] = (dp[i - 1] + dp[i - 2]) % mod;
           }
       }
       cout << dp[n] << endl;
   }
   return 0;
}

1006 Fake Photo

Go Running

#include 
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
int n, m, k;

template<typename T>
struct Discretization {
     
    vector<T> a;

    void discrete() {
     
        sort(a.begin(), a.end());
        a.erase(unique(a.begin(), a.end()), a.end());
    }

    int getId(T x) {
     
        return lower_bound(a.begin(), a.end(), x) - a.begin() + 1;
    }

    void init() {
     
        a.clear();
    }

    void push(T x) {
     
        a.push_back(x);
    }

    int size() {
     
        return a.size();
    }
};

ll x[N], y[N];
namespace Network_flows {
      //网络流板子
    //设定起点和终点
    int st;//起点-源点
    int ed;//终点-汇点

    struct egde {
     
        int to, next;
        int flow;//剩余流量
        //int capacity;//容量
    } e[N * 2];

    int head[N], tot = 1;

    void add(int u, int v, int w) {
     
        e[++tot] = {
     v, head[u], w};
        head[u] = tot;
        e[++tot] = {
     u, head[v], 0};
        head[v] = tot;//网络流反相边流量为0
    }

    int dep[N];//dep[]=-1时为炸点
    queue<int> q;

    bool bfs() {
     
        memset(dep, 0, sizeof(dep));//顺便起到vis的功能
        q.push(st);
        dep[st] = 1;
        while (!q.empty()) {
     
            int u = q.front();
            q.pop();
            for (int i = head[u]; i; i = e[i].next) {
     
                int v = e[i].to;
                if (!dep[v] && e[i].flow) {
     
                    dep[v] = dep[u] + 1;
                    q.push(v);
                }
            }
        }
        return dep[ed];
    }

    int dfs(int u, int Flow) {
     
        if (u == ed) return Flow;
        int now_flow = 0;//跑残流
        for (int i = head[u]; i; i = e[i].next) {
     
            int v = e[i].to;
            if (dep[v] == dep[u] + 1 && e[i].flow) {
     
                int f = dfs(v, min(Flow - now_flow, e[i].flow));
                e[i].flow -= f;
                e[i ^ 1].flow += f;
                now_flow += f;
                if (now_flow == Flow) return Flow;
            }
        }
        if (now_flow == 0)dep[u] = -1;
        return now_flow;
    }

#define max_flow dinic

    int dinic() {
     //最大流
        int res = 0;
        while (bfs()) {
     
            res += dfs(st, INF);
        }
        return res;
    }

    void init() {
     
        tot = 1;
        memset(head, 0, sizeof(head));
        while (!q.empty()) q.pop();
    }
}
using namespace Network_flows;

int main() {
     
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    Discretization<ll> l, r;
    int T;
    ll a, b;
    cin >> T;
    for (int cs = 1; cs <= T; cs++) {
     
        cin >> n;
        for (int i = 1; i <= n; i++) {
     
            cin >> a >> b;
            x[i] = a + b;
            y[i] = a - b;
            l.push(x[i]);
            r.push(y[i]);
        }

        l.discrete();
        r.discrete();

        st = 0;
        int lsz = l.size();
        int rsz = r.size();
        for (int i = 1; i <= n; i++) {
     
            x[i] = l.getId(x[i]);
            y[i] = r.getId(y[i]) + lsz;
        }
        ed = lsz + rsz + 1;
        for (int i = 1; i <= lsz; i++) {
     
            add(st, i, 1);
        }
        for (int i = 1; i <= rsz; i++) {
     
            add(i + lsz, ed, 1);
        }
        for (int i = 1; i <= n; i++) {
     
            add(x[i], y[i], 1);
        }
        cout << dinic() << endl;

        l.init();
        r.init();
        init();
    }
    return 0;
}

1008 Head Maker
1009 Imperative Meeting
1010 Joyful Party


Kindergarten Physics

#include 
using namespace std;
int a, b, t;
double d;

int main() {
     

   int T;
   scanf("%d", &T);
   while (T--) {
     
       scanf("%d%d%lf%d", &a, &b, &d, &t);
       printf("%.20f\n", d);
   }

   return 0;
}

1012 Last Problem

你可能感兴趣的:(多校)