2021寒假训练(五)

题号 A B C D E F G H
AC    √      Ο    √   Ο

 

 

 

B.Silver Cow Party

#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ll long long
#define int long long
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const int Base = 131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9 + 7;
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
#define speed { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);}

// inline int gcd(int a, int b) {
//     while (b ^= a ^= b ^= a %= b);
//     return a;
// }

inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

long long fastPower(long long base, long long power)
{
    long long result = 1;
    while (power > 0)
    {
        if (power & 1)
            result = result * base % mod;
        power >>= 1;
        base = (base * base) % mod;
    }
    return result;
}

inline ll rd()
{
    ll s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}


int n,m,x;
int res[maxn] = {0};
int head[maxn],cnt;
int ex_head[maxn],ex_cnt;

struct Edge{
    int to, next, w;
}e[maxn],ex[maxn];

struct Node{
    int dist, id;
    Node(int id, int dist):id(id),dist(dist){ }
    bool operator<(const Node &tmp)const{
        return dist > tmp.dist;
    }
};

void init(){
    cnt = 0;
    ex_cnt = 0;
    for(int i = 0; i < m; i++){
        e[i].next = -1;
        head[i] = -1;
        ex[i].next = -1;
        ex_head[i] = -1;
    }
}

void addEdge(int u, int v, int w){
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}

void ex_addEdge(int u, int v, int w){
    ex[ex_cnt].to = v;
    ex[ex_cnt].w = w;
    ex[ex_cnt].next = ex_head[u];
    ex_head[u] = ex_cnt++;
}

void Dijkstra(int st){
    int dis[maxn];
    int vis[maxn];
    for(int i = 1; i <= n; i++)dis[i] = inf, vis[i] = 0;
    dis[st] = 0;
    priority_queue<Node>Q;
    Q.push(Node(st, 0));
    while(!Q.empty()){
        Node u = Q.top();
        Q.pop();
        if(vis[u.id])continue;
        vis[u.id] = 1;
        for(int i = head[u.id]; ~i; i = e[i].next){
            int v = e[i].to;
            if(vis[v])continue;
            if(dis[v] > e[i].w + u.dist){
                dis[v] = e[i].w + u.dist;
                Q.push(Node(v, dis[v]));
            }
        }    
    }
    for(int i = 1; i <= n; i++){
        res[i] += dis[i];
    }
}

void EX_Dijkstra(int st){
    int dis[maxn];
    int vis[maxn];
    for(int i = 1; i <= n; i++)dis[i] = inf, vis[i] = 0;
    dis[st] = 0;
    priority_queue<Node>Q;
    Q.push(Node(st, 0));
    while(!Q.empty()){
        Node u = Q.top();
        Q.pop();
        if(vis[u.id])continue;
        vis[u.id] = 1;
        for(int i = ex_head[u.id]; ~i; i = ex[i].next){
            int v = ex[i].to;
            if(vis[v])continue;
            if(dis[v] > ex[i].w + u.dist){
                dis[v] = ex[i].w + u.dist;
                Q.push(Node(v, dis[v]));
            }
        }    
    }
    for(int i = 1; i <= n; i++){
        res[i] += dis[i];
    }
}

signed main(){

    n = rd();
    m = rd();
    x = rd();

    init();
    for(int i = 0; i < m; i++){
        int u, v, w;
        u = rd();
        v = rd();
        w = rd();
        addEdge(u, v, w);
        ex_addEdge(v, u, w);
    }

    // for(int i = 1; i <= n; i++){
    //     for(int j = head[i]; ~j; j = e[j].next){
    //         printf("%lld %lld %lld\n", i, e[j].to, e[j].w);
    //     }
    // }

    Dijkstra(x);
    EX_Dijkstra(x);
    sort(res + 1, res + n + 1, greater<int>());

    printf("%lld\n",res[1]);

    //system("pause");
    return 0;
}
View Code

G.The Party and Sweets

#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ll long long
#define int long long
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const int Base = 131;
const ll INF = 1ll << 62;
const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9 + 7;
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
#define speed { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);}

// inline int gcd(int a, int b) {
//     while (b ^= a ^= b ^= a %= b);
//     return a;
// }

inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

long long fastPower(long long base, long long power)
{
    long long result = 1;
    while (power > 0)
    {
        if (power & 1)
            result = result * base % mod;
        power >>= 1;
        base = (base * base) % mod;
    }
    return result;
}

inline ll rd()
{
    ll s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}


signed main(){
    int n = rd();
    int m = rd();
    int b[maxn],g[maxn];
    for(int i = 0; i < n; i++)b[i] = rd();
    for(int i = 0; i < m; i++)g[i] = rd();
    sort(b, b + n, greater<int>());
    sort(g, g + m, greater<int>());
    // printf("Bug\n");
    if(b[0] > g[m-1])printf("-1\n");
    else{
        int p = 0, res = 0;
        for(int i = 0; i < n; i++){
            int f = 0, ff = 0;
            while(p<m){
                f = 1;
                if(g[p] > b[i]){
                    res += g[p];
                    p++;
                }else if(g[p] == b[i]){
                    ff = 1;
                    res += g[p];
                    p++;
                }
            }
            if(ff == 0 && i == 0){
                p--;
                res -= g[m-1];
                res += b[i];
            }
            if(i != 0 && f == 0){
                res += m * b[i];
            }else if(i != 0 && f == 1){
                res += ((m - 1) * b[i]);
            }
        }
        printf("%lld\n",res);
    }
    //system("pause");
    return 0;
}
View Code

E.Tian Ji -- The Horse Racing

#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ll long long
#define int long long
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const int Base = 131;
const ll INF = 1ll << 62;
const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9 + 7;
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
#define speed { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);}

// inline int gcd(int a, int b) {
//     while (b ^= a ^= b ^= a %= b);
//     return a;
// }

inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

long long fastPower(long long base, long long power)
{
    long long result = 1;
    while (power > 0)
    {
        if (power & 1)
            result = result * base % mod;
        power >>= 1;
        base = (base * base) % mod;
    }
    return result;
}

inline ll rd()
{
    ll s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}


signed main(){
    int n;
    while(~scanf("%lld",&n), n){
        int a[maxn], b[maxn], res = 0;
        for(int i = 0; i < n; i++)a[i] = rd();
        for(int i = 0; i < n; i++)b[i] = rd();
        sort(a, a + n, greater<int>());
        sort(b, b + n, greater<int>());
        int x1 = 0, x2 = n-1;//田忌最快、最慢
        int y1 = 0, y2 = n-1;//齐王最快、最慢
        // 总的思想就是不断将齐王最快的马消耗掉即便当前亏钱
        while(y1 <= y2){
            if(a[x1] > b[y1]){
                res += 200;
                x1++;
                y1++;
            }else if(a[x1] < b[y1]){
                res -= 200;
                y1++;
                x2--;
            }else{
                if(a[x2] > b[y2]){
                    res += 200;
                    x2--;
                    y2--;
                }else{
                    if(b[y1] > a[x2])res -= 200;
                    y1++;
                    x2--;
                }
            }
        }

        printf("%lld\n",res);
    }

    //system("pause");
    return 0;
}
View Code

H.Nastya Is Playing Computer Games

#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ll long long
#define int long long
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const int Base = 131;
const ll INF = 1ll << 62;
const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9 + 7;
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
#define speed { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);}

// inline int gcd(int a, int b) {
//     while (b ^= a ^= b ^= a %= b);
//     return a;
// }

inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

long long fastPower(long long base, long long power)
{
    long long result = 1;
    while (power > 0)
    {
        if (power & 1)
            result = result * base % mod;
        power >>= 1;
        base = (base * base) % mod;
    }
    return result;
}

inline ll rd()
{
    ll s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}


signed main(){
    int n, k;
    n = rd();
    k = rd();
    int minn = min(k-1, n-k);
    int res = 0;
    res = 2 + 4 + (minn - 1) * 3 + minn;
    res += 3 * (n - minn - 1);
    printf("%lld\n",res);

    //system("pause");
    return 0;
}
View Code
posted @ 2021-01-24 10:15  濡苏  阅读(44)  评论(0)    收藏  举报