Description
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 - 1positive 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 - 1portals, 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 Input
8 4 1 2 1 2 1 2 1
YES
8 5 1 2 1 2 1 1 1
NO
Hint
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.
题意:给出一个序列,每次只能从i到(i+a[i]),问能否从1到t。
题解:水题,循环就可以。
#include<cstdio>
#include<iostream>
using namespace std;
long long a[30030];
int main(){
long long n,t,i,s;
int flag=0;
scanf ("%lld %lld",&n,&t);
s=1;
for (i=1;i<n;i++){
scanf ("%lld",&a[i]);
if (flag==1)
continue;
s+=a[s];
if (s==t){
flag=1;
}
}
if (flag) printf ("YES\n");
else
printf ("NO\n");
return 0;
}
Description
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.
Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.
Input
The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.
Next q lines contain the descriptions of the requests, one per line.
Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.
The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handlenew is not used and has not been used by anyone.
Output
In the first line output the integer n — the number of users that changed their handles at least once.
In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old andnew, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.
Each user who changes the handle must occur exactly once in this description.
Sample Input
5 Misha ILoveCodeforces Vasya Petrov Petrov VasyaPetrov123 ILoveCodeforces MikeMirzayanov Petya Ivanov
3 Petya Ivanov Misha MikeMirzayanov Vasya VasyaPetrov123
#include<cstdio>
#include<cstring>
char a[1010][22],b[1010][22];
int main(){
int n,m,i,j;
scanf ("%d",&n);
for (i=1;i<=n;i++){
scanf ("%s %s",a[i],b[i]);
for (j=i-1;j>=1;j--){
if (!strcmp(a[i],b[j])){
strcpy(b[j],b[i]);
i--;
n--;
break;
}
}
}
m=i-1;
printf ("%d\n",m);
for (i=1;i<=m;i++)
printf ("%s %s\n",a[i],b[i]);
return 0;
}
Description
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.
- He lifts all the books above book x.
- He pushes book x out of the stack.
- He puts down the lifted books without changing their order.
- After reading book x, he puts book x on the top of the stack.
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 Input
3 5 1 2 3 1 3 2 3 1
12
Hint
Here's a picture depicting the example. Each vertical column presents the stacked books.
#include<cstdio>
#include<cstring>
using namespace std;
int w[550],b[1010],vis[550];
int main(){
int n, m, i, s, j;
scanf ("%d %d",&n, &m);
for (i=1; i<=n; i++){
scanf ("%d",&w[i]);
}
for (i=1; i<=m; i++)
scanf ("%d",&b[i]);
s=0;
for (i=2; i<=m; i++){
memset (vis,0,sizeof(vis));
for (j=i-1; j>0; j--){
if (b[i]== b[j])
break;
if (!vis[b[j]]){
s+=w[b[j]];
vis[b[j]]=1;
}
}
}
printf ("%d\n",s);
return 0;
}