USACO 之 Section 1.1 Ad Hoc Problems

来源:互联网 发布:狙击模拟软件ios 编辑:程序博客网 时间:2024/06/10 22:06

Your Ride Is Here:模拟一下题意过程,就可以了。。。

/*ID: JmingPROG: rideLANG: C++*/#include <iostream>#include <cstdlib>#include <cstdio>#include <string>using namespace std;int Solve(string str){    int sum = 1;    for (int i = 0; i < str.size(); i++) {        sum *= (str[i] - 'A' + 1);        sum %= 47;    }    return sum;}int main(){    freopen("ride.in", "r", stdin);    freopen("ride.out", "w", stdout);    string str;    string str1;    cin >> str >> str1;    int tmp1, tmp2;    tmp1 = Solve(str);    tmp2 = Solve(str1);    if (tmp1 == tmp2) {        printf("GO\n");    }else {        printf("STAY\n");    }    return 0;}

Greedy Gift Givers:用map即可解决。。。

/*ID: JmingPROG: gift1LANG: C++*/#include <iostream>#include <cstdlib>#include <cstdio>#include <string>#include <map>using namespace std;const int MAX_N = 15;string arr_str[MAX_N];int N;struct node {    int rec, giv;    node() {        rec = 0;        giv = 0;    }};node myrecord[MAX_N];map<string, node> str_no_map;void Solve() {    string str, t_str;    int amount, number;    while (cin >> str) {        scanf("%d %d", &amount, &number);        if (0 == number) {            continue;        }        str_no_map[str].giv += (amount / number) * number;        for (int i = 0; i < number; ++i) {            cin >> t_str;            str_no_map[t_str].rec += (amount / number);        }    }    for (int i = 0; i < N; ++i) {        cout << arr_str[i] << " " << str_no_map[arr_str[i]].rec - str_no_map[arr_str[i]].giv << endl;    }}int main(){    freopen("gift1.in", "r", stdin);    freopen("gift1.out", "w", stdout);    scanf("%d", &N);    for (int i = 0; i < N; ++i) {        cin >> arr_str[i];        str_no_map[arr_str[i]] = myrecord[i];    }    Solve();return 0;}
Friday the Thirteenth:数学知识就可解决。。。

/*ID: JmingPROG: fridayLANG: C++*/#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;int N, cnt[13] = { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int ans[7];void Solve() {    int end_year = 1900+N;    int days, week = 1;    for (int i = 1900; i < end_year; ++i) {        for (int j = 1; j <= 12; ++j) {            if (2 == j) {                if (!(i%100)) {                    if (!(i%400)) { days = 29; }                    else { days = 28; }                }else {                    if (!(i%4)) { days = 29; }                    else { days = 28; }                }            }else { days = cnt[j]; }            ans[(week+5)%7]++;            week = (week+days%7)%7;        }    }    printf("%d ", ans[6]);    for (int i = 0; i < 6; ++i) {        printf("%d", ans[i]);        if (i != 5) {            printf(" ");        }    }    printf("\n");}int main(){    freopen("friday.in", "r", stdin);    freopen("friday.out", "w", stdout);    scanf("%d", &N);    Solve();    return 0;}
Broken Necklace:

由于(3<=N<=350),故以O(n^2)复杂度解决也可AC。。。不过,AC后看到给的分析里有一种O(n)的方法也学了一下。

O(n^2):

/*ID: JmingPROG: beadsLANG: C++*/#include <iostream>#include <cstdlib>#include <cstdio>#include <string>#include <algorithm>using namespace std;int N;string str;void Solve() {    int sum1, sum2;    int ans = -1;    for (int i = 0; i < N; ++i) {        char ch;        sum1 = 1;        if ('w' == str[i]) {            for (int j = i+1; j != i; j = (j+1)%N) {                if ('w' != str[j]) {                    ch = str[j];                    break;                }            }        }else { ch = str[i]; }        int r = (i + 1)%N;        while (r != i) {            if (('w' == str[r]) || (str[i] == str[r])) {                ++sum1;                r = (r+1)%N;            }            else break;        }        r = (r-1 + N)%N;        int l = (i-1+N)%N;        int sum2 = 0;        if (l != r) {            if ('w' == str[l]) {                for (int j = l; j != r; j = (j-1+N)%N) {                    if ('w' != str[j]) {                        ch = str[j];                        break;                    }                }            }else { ch = str[l]; }        }        while (l != r) {            if (('w' == str[l]) || (ch == str[l])) {                ++sum2;                l = (l-1+N)%N;            }            else break;        }        ans = max(ans, sum1+sum2);    }    printf("%d\n", ans);}int main(){    freopen("beads.in", "r", stdin);    freopen("beads.out", "w", stdout);    scanf("%d", &N);    cin >> str;    Solve();    return 0;}

O(n):

关键:

(1)将环转换为所给串重复的两个(即str+str)

(2)作预处理,myleft[pos][ ],表示在字符串pos位置之前(不包括pos位置的字符),连续的相同颜色的珠子数

                            myright[pos][ ],表示从字符串pos位置向右(包括pos位置的字符),连续的相同颜色的珠子数

/*ID: JmingPROG: beadsLANG: C++*/#include <iostream>#include <cstdlib>#include <cstdio>#include <string>using namespace std;#define MAX(a, b) ((a)>(b)?(a):(b))#define MIN(a, b) ((a)<(b)?(a):(b))int N;string str;const int MAX_N = 705;int myleft[MAX_N][2], myright[MAX_N][2];void Solve() {    // 0: b 1:r    myleft[0][0] = myleft[0][1] = 0;    for (int i = 1; i <= N; ++i) {        if ('b' == str[i-1]) {            myleft[i][0] = myleft[i-1][0] + 1;            myleft[i][1] = 0;        }        else if ('r' == str[i-1]) {            myleft[i][1] = myleft[i-1][1] + 1;            myleft[i][0] = 0;        }        else {            myleft[i][0] = myleft[i-1][0] + 1;            myleft[i][1] = myleft[i-1][1] + 1;        }    }    myright[N][0] = myright[N][1] = 0;    for (int i = N-1; i >= 0; --i) {        if ('b' == str[i]) {            myright[i][0] = myright[i+1][0] + 1;            myright[i][1] = 0;        }else if ('r' == str[i]) {            myright[i][1] = myright[i+1][1] + 1;            myright[i][0] = 0;        }else {            myright[i][0] = myright[i+1][0] + 1;            myright[i][1] = myright[i+1][1] + 1;        }    }    int ans = 0;    for (int i = 0; i < N; ++i) {        ans = MAX(ans, MAX(myleft[i][0], myleft[i][1])+ MAX(myright[i][0], myright[i][1]));    }    ans = MIN(ans, N>>1);    printf("%d\n", ans);}int main(){    freopen("beads.in", "r", stdin);    freopen("beads.out", "w", stdout);    scanf("%d", &N);    cin >> str;    str = str+str;    N = N<<1;    Solve();    return 0;}
0 0
原创粉丝点击