Codeforces, codeforce

Source: Internet
Author: User
Tags array sort

Codeforces, codeforce


These matches have been cool. In general, they are still too bad !!

I am slow at the same time, reading questions and reading questions, and I still have to spend a lot of effort to understand it !!



A. New Year Transportationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

New Year is coming in Line World! In this world, there areNCells numbered by integers from 1N, As a 1 digit × hourNBoard. 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 thoughtNAccept-rule 1 positive integersA1, bytes,A2, middle..., middle ,...,ANVertex-marker 1. For every integerIWhere 1 limit ≤ limitILimit ≤ limitNCondition-condition 1 the condition 1 condition ≤ conditionAILimit ≤ limitNAccept-Encoding-IHolds. Next, he madeNRole-increment 1 portals, numbered by integers from 1NUpload-Snapshot 1.I-Th (1 digit ≤ secondILimit ≤ limitNConnector-connector 1) portal connects cellIAnd cell (IRegion + RegionAI), And one can travel from cellITo cell (IRegion + RegionAI) UsingI-Th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (IRegion + RegionAI) To cellIUsingI-Th portal. It is easy to see that because of condition 1 limit ≤ limitAILimit ≤ limitNAccept-Encoding-IOne can't leave the Line World using portals.

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

Input

The first line contains two space-separated integersN(3 cores ≤ CoresNCost ≤ cost 3 × cost 104) andT(2 cores ≤ CoresTLimit ≤ limitN)-The number of cells, and the index of the cell which I want to go.

The second line containsNAccept-rule 1 space-separated integersA1, bytes,A2, middle..., middle ,...,ANPartition-limit 1 (1 partition ≤ limitAILimit ≤ limitNAccept-Encoding-I). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

Output

If I can go to cellTUsing the transportation system, print "YES". Otherwise, print "NO ".

Sample test (s) input
8 41 2 1 2 1 2 1
Output
YES
Input
8 51 2 1 2 1 1 1
Output
NO
Note

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

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




Idea: directly simulate scanning.


AC code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[30005];int main(){    int n, t;    while(scanf("%d %d", &n, &t) != EOF)    {        for(int i=0; i<n-1; i++)        {            scanf("%d", &a[i]);        }        int tmp = 0, flag = 0;        for(int i=0; i<n-1; i++)        {            tmp += a[i];            if(tmp == t)            {                flag = 1;                break;            }        }        if(flag) printf("YES\n");        else printf("NO\n");    }    return 0;} 



B. New Year Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

User ainta has a permutationP1, bytes,P2, middle..., middle ,...,PN. As the New Year is coming, he wants to make his permutation as pretty as possible.

PermutationA1, bytes,A2, middle..., middle ,...,ANIs prettier than permutationB1, bytes,B2, middle..., middle ,...,BN, If and only if there exists an integerK(1 digit ≤ DigitKLimit ≤ limitN) WhereA1 bytes = bytesB1, bytes,A2 bytes = bytesB2, middle..., middle ,...,AKExecutor-cores 1 cores = CoresBKBetween-between 1 andAKLatency <latencyBKAll holds.

As known, permutationPIs so sensitive that it cocould be only modified by swapping two distinct elements. But swapping two elements is harder than you think. GivenNLimit × limitNBinary matrixA, User ainta can swap the valuesPIAndPJ(1 digit ≤ DigitI, Bytes,JLimit ≤ limitN,I  =J) If and only ifAI, Bytes,JBytes = bytes 1.

Given the permutationPAnd the matrixA, User ainta wants to know the prettiest permutation that he can obtain.

Input

The first line contains an integerN(1 digit ≤ DigitNLimit ≤ limit 300)-the size of the permutationP.

The second line containsNSpace-separated integersP1, bytes,P2, middle..., middle ,...,PN-The permutationPThat user ainta has. Each integer between 1andNOccurs exactly once in the given permutation.

NextNLines describe the matrixA.I-Th line containsNCharacters '0' or '1' and describesI-Th rowA.J-Th character ofI-Th lineAI, Bytes,JIs the element on the intersection ofI-Th row andJ-Th column of A. It is guaranteed that, for all integersI, Bytes,JWhere 1 limit ≤ limitILatency <latencyJLimit ≤ limitN,AI, Bytes,JSignature = SignatureAJ, Bytes,IHolds. Also, for all integersIWhere 1 limit ≤ limitILimit ≤ limitN,AI, Bytes,IRows = rows 0 holds.

Output

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

Sample test (s) input
75 2 4 3 6 7 10001001000000000000101000001000000000100001001000
Output
1 2 4 3 6 7 5
Input
54 2 1 5 30010000011100100110101010
Output
1 2 3 4 5
Note

In the first sample, the swap needed to obtain the prettiest permutation is :(P1, bytes,P7 ).

In the second sample, the swaps needed to obtain the prettiest permutation is (P1, bytes,P3), then ),(P4, middle,P5), then ),(P3, bytes,P4 ).

A permutationPIs a sequence of integersP1, bytes,P2, middle..., middle ,...,PN, ConsistingNDistinct positive integers, each of them doesn't exceedN.I-Th element of the permutationPIs denotedPI. The size of the permutationPIs denotedN.




Idea: At the beginning, I didn't know what to do with this question. Then I started to tease me...

Is to sort the items in the connected component ~~


AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; int n; char str [305] [305]; // used to save the 01 two-dimensional array int a [305]; // input the data to be sorted int vis [305]; // record the data int tmp [305] on a connected component; // sort the data in the connected component out of void dfs (int x) // deep search for connected components {if (vis [x]) return; vis [x] = 1; for (int I = 0; I <n; I ++) if (str [x] [I] = '1') dfs (I);} int main () {while (scanf ("% d", & n )! = EOF) {for (int I = 0; I <n; I ++) scanf ("% d", & a [I]); for (int I = 0; I <n; I ++) scanf ("% s", str [I]); for (int I = 0; I <n; I ++) if (! Vis [I]) {dfs (I); int k = 0; for (int j = 0; j <n; j ++) if (vis [j]) tmp [k ++] = a [j]; // Save the connected component in the tmp array sort (tmp, tmp + k ); // sort the tmp array k = 0; for (int j = 0; j <n; j ++) if (vis [j]) a [j] = tmp [k ++]; // Save the sorted data back to memset (vis, 0, sizeof (vis )); // set vis to 0 so that the connected component can be found again next time.} for (int I = 0; I <n-1; I ++) printf ("% d ", a [I]); printf ("% d \ n", a [n-1]);} return 0 ;}




C. New Year Book Readingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

New Year is coming, and Jaehyun decided to read every books during 2015, unlike this year. He hasNBooks numbered by integers from 1N. The weight ofI-Th (1 digit ≤ secondILimit ≤ limitN) Book isWI.

As Jaehyun's house is not large enough to have a bookshelf, he keepsNBooks by stacking them vertically. When he wants to read a certain bookX, He follows the steps described below.

  1. He lifts all the books above bookX.
  2. He pushes bookXOut of the stack.
  3. He puts down the lifted books without changing their order.
  4. After reading bookX, He puts bookXOn the top of the stack.

He decided to read booksMDays. InJ-Th (1 digit ≤ secondJLimit ≤ limitM) Day, he will read the book that is numbered with integerBJ(1 digit ≤ DigitBJLimit ≤ limitN). 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 shoshould liftMDays wocould 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 integersN(2 cores ≤ CoresNLimit ≤ limit 500) andM(1 digit ≤ DigitMLimit ≤ limit 1000)-the number of books, and the number of days for which Jaehyun wocould read books.

The second line containsNSpace-separated integersW1, bytes,W2, middle..., middle ,...,WN(1 digit ≤ DigitWILimit ≤ limit 100)-the weight of each book.

The third line containsMSpace separated integersB1, bytes,B2, middle..., middle ,...,BM(1 digit ≤ DigitBJLimit ≤ limitN)-The order of books that he wocould read. Note that he can read the same book more than once.

Output

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

Sample test (s) input
3 51 2 31 3 2 3 1
Output
12
Note

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




Idea: It's a fun question. Although I don't know how it works

Do not care about the order of books. For each book to be read, scan forward to the beginning or scan to the same book to see how many books are in the middle. Just add the weight of these books.

Note: If you scan the same book, you can break it. If you scan the duplicate book, you can continue.


AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; int w [505]; int main () {int n, m; while (scanf ("% d", & n, & m )! = EOF) {for (int I = 1; I <= n; I ++) {scanf ("% d", & w [I]);} int t, ans = 0; int vis [505], B [1005]; for (int j = 1; j <= m; j ++) scanf ("% d ", & B [j]); for (int I = 2; I <= m; I ++) {memset (vis, 0, sizeof (vis )); for (int j = I-1; j> = 1; j --) // sweep from the beginning of the book {if (B [j] = B [I]) break; // scan to the same book to obtain the break if (vis [B [j]) continue; // scan the duplicate to continue ans + = w [B [j]; vis [B [j] = 1 ;}} printf ("% d \ n ", ans);} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.