Good Bye 2014 A B C
来源:互联网 发布:天涯明月刀网络延迟高 编辑:程序博客网 时间:2024/04/29 04:34
#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<vector>#include<string.h>#include<queue>#include<stack>#include<set>#include<map>#include<sstream>#include<time.h>#include<utility>#include<malloc.h>using namespace std;int n, t;int p[30010];int main(){ while (cin >> n >> t) { for (int i = 1; i <= n-1; i++) cin >> p[i]; int pos = 1; for (int i = 1; i <= n-1;i++) { if (pos >= t) break; pos += p[pos]; } if (pos == t) cout << "YES" << endl; else cout << "NO" << endl; } return 0;}
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.
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 1and 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 thei-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.
In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.
75 2 4 3 6 7 10001001000000000000101000001000000000100001001000
1 2 4 3 6 7 5
54 2 1 5 30010000011100100110101010
1 2 3 4 5
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).
A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Thei-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
B 弗洛伊德算法 ,并查集也可以做 ,有时间敲一下。
#include <iostream> #include <stdio.h> #include <math.h> #include <algorithm> #include <string.h> using namespace std;int p[305];int b[305][305];int n;int main(){ while (cin >> n) { for (int i = 1; i <= n; i++) cin >> p[i]; getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { char c = getchar(); b[i][j] = (c == '1'); } getchar(); } for (int k = 1; k <= n;k++) for (int i= 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (b[i][k] & b[k][j]) { b[i][j] = b[j][i] = 1; } } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { if (b[i][j] ) { if (p[i] > p[j]) { swap(p[i],p[j]); } } } for (int i = 1; i <= n; i++) cout << p[i] << " "; } return 0;}
贪心后模拟。
#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<vector>#include<string.h>#include<queue>#include<stack>#include<set>#include<map>#include<sstream>#include<time.h>#include<utility>#include<malloc.h>using namespace std;int n, m;int p[510];int q[1010], qq[1010];int vis[1010];int main(){ while (cin >> n >> m) { for (int i = 1; i <= n; i++) { cin >> p[i]; } memset(vis,0,sizeof(vis)); int pos = 1; for (int i = 1; i <= m; i++) { int jjq; cin >> jjq; q[i] = jjq; if (!vis[jjq]) { qq[pos++] = jjq; vis[jjq] = 1; } } long long ans = 0; for (int i = 1; i <= m; i++) { int tt = q[i]; for (int j = 1; j <= n; j++) { if (qq[j] != tt) { ans += p[qq[j]]; } else { for (int k = j; k > 1 ; k--) { qq[k] = qq[k-1]; } qq[1] = tt; break; } } } cout << ans << endl; } return 0;}
- Good Bye 2014 A B C
- Codefores Good Bye 2014 - A,B,C
- Codeforces Good Bye 2014 解题报告 (A B C D)
- Codeforces Good Bye 2014 A.B.C.D.
- codeforces Good Bye 2014题解(A、B、C)
- Good Bye 2014 A B C D E
- Good Bye 2014 A,B,C,D,E
- Codeforces Good Bye 2015 (A,B,C,D)
- Good Bye 2016(A+B)
- Good Bye 2014--B. New Year Permutation
- Good Bye 2014 B.(Floyd+暴力)
- Good Bye 2014 B. New Year Permutation
- Good Bye 2015B
- Good Bye 2014 A.(模拟)
- Good Bye 2014--A. New Year Transportation
- A Good Bye 2014(CF)
- Good Bye 2015 A
- Good Bye 2014 C.(栈模拟)
- 微软公有云事件中心(Azure Event Hubs)在开放物联网大会(OIOT)啼声初试
- javaFx2.x 登陆及其切换界面
- word中表格行转列方法或excel粘贴转置
- 彩票走势图
- 各种几何图形的制作效果
- Good Bye 2014 A B C
- C语言基础--素数
- RTHRHRHFSHGRFGH
- [LeetCode] Serch Insert Position (总结二分查找) Java version
- 编译webkit的wince版本
- EAWebKit的WincePort
- 深究repo的所谓“续传”功能
- Android的surface的三种工作方式
- vc 实现端口转发功能 原理是select,可以显示数据包的大小,可用于调试一些网络程序,我使用它来解析RDP协议的