Codefores Good Bye 2014 - A,B,C

A. New Year Transportation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.

So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.

Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.

Input

The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.

The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

Output

If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".

Sample test(s)
Input
8 4
1 2 1 2 1 2 1
Output
YES
Input
8 5
1 2 1 2 1 1 1
Output
NO
Note

In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.

In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.


                                                                       

题意:从0点出发,所以一开始 i= 0;下一步会跳到 i+ ai 的位置,求出是否可以跳到m位置。

思路:暴力求解,但是比赛的时候忘了是可以到达终点的即 m = n. (粗心 打脸 >.<)

CODE:

#include 
#include 
using namespace std;

int num[30005];

int main()
{
//freopen("in", "r", stdin);
    int n, t;

    while(~scanf("%d %d", &n, &t)) {
        for(int i = 1; i < n; ++i) {
            scanf("%d", &num[i]);
        }
        int x = 1, ok = 1;
        while(x <= n) {
            if(x == t) {
                ok = 0;
                printf("YES\n");
                break;
            }
            if(x > t) break;
            x = x + num[x];
        }
        if(ok) printf("NO\n");
    }
    return 0;
}

B. New Year Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.

Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.

As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.

Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

Input

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p.

The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation.

Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Sample test(s)
Input
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000
Output
1 2 4 3 6 7 5
Input
5
4 2 1 5 3
00100
00011
10010
01101
01010
Output
1 2 3 4 5
Note

In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).

In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).

Codefores Good Bye 2014 - A,B,C_第1张图片

A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

                                                                

题意:给出一组数,要求输出字典序最小的序列,但是数字的交换规则需按照矩阵规律。

思路:最重要的一点是 当 a[i][k] = 1, a[k][j] = 1, 则a[i][j] = 1. 据说是有一点Floyd 的思想。。。好像有那么点吧。。

CODE :

#include 
#include 
#include 

using namespace std;
int num[305];
char a[305][305];

int main()
{
//freopen("in", "r", stdin);
    int n;
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &num[i]);
        }
        for(int i = 1; i <= n; ++i) {
            scanf("%s", a[i] + 1);
        }
        for(int i = 1; i <= n; ++i) {
            for(int k = 1; k <= n; ++k) {
                for(int j = 1; j <= n; ++j) {
                    if(a[k][i] == '1' && a[i][j] == '1')
                    a[k][j] = '1';
                }
            }
        }
        int ok = 1;
        for(int i = 1; i <= n; ++i) {
            for(int j = i + 1; j <= n; ++j) {
                if(a[i][j] == '1' && num[i] > num[j]) {
                    ok = 1;
                    swap(num[i], num[j]);
                }
            }
        }
        printf("%d", num[1]);
        for(int i = 2; i <= n; ++i) {
            printf(" %d", num[i]);
        }
        printf("\n");
    }
    return 0;
}


C. New Year Book Reading
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.

As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.

  1. He lifts all the books above book x.
  2. He pushes book x out of the stack.
  3. He puts down the lifted books without changing their order.
  4. After reading book x, he puts book x on the top of the stack.

Codefores Good Bye 2014 - A,B,C_第2张图片

He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.

After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?

Input

The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books.

The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.

Output

Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.

Sample test(s)
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note

Here's a picture depicting the example. Each vertical column presents the stacked books.

Codefores Good Bye 2014 - A,B,C_第3张图片

                                                                    

题意:

要求求出最少每次拿到所需的那本书,最少帮动的书的总重量。需要注意的一点是一开始的初始状态是可以自己设定的。

思路:

比赛的时候写的思路是完全正确的,但是没有注意到可以自己设定初始状态这一点。Wa 了之后就心虚了。。。

使用栈,和贪心的思想。

CODE :

#include 
#include 
#include 
#include 

using namespace std;
stack s, a;
int num[505], ord[1005], vis[505], temp[505];


int main()
{
//freopen("in", "r", stdin);
    int n, m;
    while(~scanf("%d %d", &n, &m)) {

        while(!s.empty()) s.pop();
        while(!a.empty()) a.pop();
        memset(ord, 0, sizeof(ord));

        for(int i = 1; i <= n; ++i) {
            scanf("%d", &num[i]);
            s.push(num[i]);
        }

        int t = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= m; ++i) {
            scanf("%d", &ord[i]);
            if(!vis[ord[i]]) {
                temp[t++] = ord[i];
                vis[ord[i]] = 1;
            }
        }

        for(int i = 1; i <= n; ++i) {
            if(!vis[i]) {
                s.push(i);
            }
        }
        for(int i = t - 1; i >= 0; --i) {
            s.push(temp[i]);
        }

        int ans = 0, i = 2;
        int ans1 = 0;

        while(!s.empty()) {
            int f = s.top(); s.pop();
            if(f == ord[i]) {
                while(!a.empty()) {
                    int r = a.top(); a.pop();
                    ans += num[r];
                    s.push(r);
                }
                if(i == 1) ans1 = ans;
                s.push(f);
                i++;
            }
            else {
                a.push(f);
            }
        }
        printf("%d\n", ans - ans1);
    }
    return 0;
}

。。。(未完待续)

你可能感兴趣的:(CF)