程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Codeforces Round #303 (Div. 2)

Codeforces Round #303 (Div. 2)

編輯:關於C++

A.簡單題

/*************************************************************************
    > File Name: A.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月20日 星期三 09時51分15秒
 ************************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair  PLL;

int mat[110][110];
bool ok[110];

int main() {
    int n;
    while (cin >> n) {
        for (int i = 1; i <= n; ++i) {
            ok[i] = 1;
            for (int j = 1; j <= n; ++j) {
                cin >> mat[i][j];
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (mat[i][j] == -1 || mat[i][j] == 0) {
                    continue;
                }
                if (mat[i][j] == 1) {
                    ok[i] = 0;
                }
                else if (mat[i][j] == 2) {
                    ok[j] = 0;
                }
                else {
                    ok[i] = ok[j] = 0;
                }
            }
        }
        vector  ans;
        ans.clear();
        for (int i = 1; i <= n; ++i) {
            if (ok[i]) {
                ans.push_back(i);
            }
        }
        int size = ans.size();
        printf("%d\n", size);
        if (size == 0) {
            continue;
        }
        printf("%d", ans[0]);
        for (int i = 1; i < size; ++i) {
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}

B.簡單題

/*************************************************************************
    > File Name: B.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月20日 星期三 09時57分00秒
 ************************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair  PLL;

string s, t;

int main() {
    while (cin >> s >> t) {
        int len = s.length();
        int cnt = 0;
        for (int i = 0; i < len; ++i) {
            if (s[i] != t[i]) {
                ++cnt;
            }
        }
        if (cnt & 1) {
            printf("impossible\n");
        }
        else {
            bool flag = 0;
            for (int i = 0; i < len; ++i) {
                if (s[i] == t[i]) {
                    printf("%c", s[i]);
                }
                else {
                    if (!flag) {
                        printf("%c", s[i]);
                    }
                    else {
                        printf("%c", t[i]);
                    }
                    flag ^= 1;
                }
            }
            printf("\n");
        }
    }
    return 0;
}

C.簡單dp, dp[i][0/1/2]表示不砍第i棵樹;砍,往左邊倒;砍,往右倒

/*************************************************************************
    > File Name: C.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月20日 星期三 10時03分06秒
 ************************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair  PLL;

PLL sta[100100];
int dp[100100][3];
int main() {
    int n;
    while (~scanf("%d", &n)) {
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= n; ++i) {
            scanf("%d%d", &sta[i].first, &sta[i].second);
        }
        dp[1][1] = 1;
        if (sta[1].first + sta[1].second < sta[2].first) {
            dp[1][2] = 1;
        }
        for (int i = 2; i <= n; ++i) {
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]);
            dp[i][0] = max(dp[i - 1][0], dp[i][0]);
            if (sta[i].first - sta[i].second > sta[i - 1].first) {
                dp[i][1] = max(dp[i][1], max(dp[i - 1][1], dp[i - 1][0]) + 1);
            }
            if (sta[i].first - sta[i].second > sta[i - 1].first + sta[i - 1].second) {
                dp[i][1] = max(dp[i][1], dp[i - 1][2] + 1);
            }
            if (i < n && sta[i].first + sta[i].second < sta[i + 1].first || i == n) {
                dp[i][2] = max(dp[i][2], max(dp[i - 1][1], dp[i - 1][0]) + 1);
                if (sta[i - 1].first + sta[i - 1].second < sta[i].first) {
                    dp[i][2] = max(dp[i][2], dp[i - 1][2] + 1);
                }
            }
        }
        printf("%d\n", max(dp[n][0], max(dp[n][1], dp[n][2])));
    }
    return 0;
}

D.排序貪心就行

/*************************************************************************
    > File Name: D.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月20日 星期三 10時16分38秒
 ************************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair  PLL;

int arr[100010];
int main() {
    int n;
    while (~scanf("%d", &n)) {
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &arr[i]);
        }
        sort(arr + 1, arr + n + 1);
        int sum = 0;
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            if (sum <= arr[i]) {
                ++ans;
                sum += arr[i];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

E.dijkstra算法的實現過程就是最小路徑樹的尋找過程
每次加入一個點放到當前求出最短路的集合裡
考慮給每個點求一個最短的可以達到它的邊,最短路相同時去邊權小的邊, 求的時候注意用long long ,一個地方沒改wa了好多次也是哔了狗了

/*************************************************************************
    > File Name: E.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月20日 星期三 10時54分55秒
 ************************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair  PLL;

static const int N = 300100;
LL d[N];
bool flag[N];
bool vis[N];
int head[N];
int cost[N];
int tot;
int use[N];
vector  ans;
priority_queue < PLL, vector , greater  > qu;


struct node {
    int w;
    int id;
    int nxt;
    int to;
}edge[N << 1];

void addedge(int u, int v, int w, int id) {
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    edge[tot].id = id;
    head[u] = tot++;
}

void Dijkstra(int s) {
    d[s] = 0;
    cost[s] = 0;
    memset(flag, 0, sizeof(flag));
    memset(vis, 0, sizeof(vis));
    while (!qu.empty()) {
        qu.pop();
    }
    qu.push(make_pair(d[s], s));
    while (!qu.empty()) {
        PLL tmp = qu.top();
        qu.pop();
        int u = tmp.second;
        LL dist = tmp.first;
        if (flag[u]) {
            continue;
        }
        flag[u] = 1;
        vis[use[u]] = 1;
        for (int i = head[u]; ~i; i = edge[i].nxt) {
            int v = edge[i].to;
            int w = edge[i].w;
            int id = edge[i].id;
            if (d[v] > dist + w) {
                use[v] = id;
                d[v] = dist + w;
                cost[v] = w;
                qu.push(make_pair(d[v], v));
            }
            else if (d[v] == dist + w && cost[v] > w) {
                cost[v] = w;
                use[v] = id;
            }
        }
    }
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) {
        d[i] = (1LL << 60);
        cost[i] = d[i];
    }
    tot = 0;
    int u, v, w, s;
    for (int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        addedge(u, v, w, i);
        addedge(v, u, w, i);
    }
    scanf("%d", &s);
    Dijkstra(s);
    LL sum = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = head[i]; ~j; j = edge[j].nxt) {
            if (vis[edge[j].id]) {
                sum += edge[j].w;
            }
        }
    }
    cout << sum / 2 << endl;
    if (!sum) {
        return 0;
    }
    for (int i = 1; i <= m; ++i) {
        if (vis[i]) {
            printf("%d ", i);
        }
    }
    printf("\n");
    return 0;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved