PAT 天梯赛真题集
题目:L2-010 排座位
题意:
1. x与y是敌对关系: a)也有共同好友:OK but...
b)无共同朋友:No way
2. x与y是朋友关系:No problem
3. x与y既不是朋友也不敌对:OK
朋友间并查集,用map记录敌对关系。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include <bits/stdc++.h> #define scf3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scf2(a,b) scanf("%d%d",&a,&b) using namespace std; int fa[110]; int find( int x){ return fa[x] == x ? x : fa[x] = find(fa[x]);} int main(){ map< pair< int , int > , bool >mp; int n, m, k; scf3(n,m,k); int x, y , op; for ( int i = 1; i <= n; i++) fa[i] = i; for ( int i = 1; i <= m; i++){ scf3(x,y,op); if (op == 1){ fa[find(x)] = find(y); } else if (op == -1){ mp[make_pair(x,y)] = false ; mp[make_pair(y,x)] = false ; } } while (k--){ scf2(x,y); if (find(x) == find(y)){ if ( !mp.count(make_pair(x,y)) || !mp.count(make_pair(y,x)) ) //不敌对 puts ( "No problem" ); else puts ( "OK but..." ); } else { if ( !mp.count(make_pair(x,y)) || !mp.count(make_pair(y,x)) ) //不敌对 puts ( "OK" ); else puts ( "No way" ); } } } |
题意:模拟,sort.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#include <bits/stdc++.h> using namespace std; struct Node { int num; int get; double val; }node[10010]; bool cmp( const Node &a, const Node &b){ //核心排序代码 if (a.val == b.val) { if (a.get == b.get) return a.num < b.num; else return a.get > b.get; } else return a.val > b.val; } int main(){ int n, m; memset (node,0, sizeof (node)); scanf ( "%d" ,&n); for ( int i = 1; i <= n; i++) //初始化 下标等于编号 node[i].num = i; for ( int i = 1; i <= n; i++){ scanf ( "%d" ,&m); int x; double y; while (m--){ scanf ( "%d%lf" , &x, &y); node[x].val += y; node[x].get ++; node[i].val -= y; } } sort(node + 1, node + n + 1, cmp); for ( int i = 1; i <= n; i++){ printf ( "%d %.2f\n" ,node[i].num,node[i].val/100); } } |
题目:L2-008 最长对称子串 用力戳我直达原题
题意:暴力,O(n2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <bits/stdc++.h> using namespace std; int main(){ string s; getline(cin,s); int ans = -1; for ( int i = 0; i < s.size(); i++){ // aba型 int sum = 1; int idx = i + 1; for ( int j = i - 1; j >= 0; j--){ if (s[idx] != s[j]) break ; else sum += 2; idx++; if (idx >= s.size()) break ; } ans = max(ans,sum); } for ( int i = 0; i < s.size(); i++){ //aabb型 if (s[i] != s[i+1]) continue ; int sum = 2; int idx = i + 2; for ( int j = i - 1; j >= 0; j--){ if (s[idx] != s[j]) break ; else sum += 2; idx++; if (idx >= s.size()) break ; } ans = max(ans,sum); } cout << ans << endl; } |
题目:L2-016. 愿天下有情人都是失散多年的兄妹
用力戳我直达原题题意:就是....不能近亲结婚...也不能同性恋....
用二叉树存双亲,DFS搜x的前四代人,存进set,再搜y的前四代人,看是否存在于set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include <bits/stdc++.h> using namespace std; set< int >st; bool flag; struct Node { int fa,mon; char sex; }node[100100]; void dfs( int x, int num){ if (num > 5) return ; st.insert(x); if (node[x].fa != -1) find(node[x].fa,num + 1); if (node[x].mon != -1) find(node[x].mon,num + 1); } void judge( int x, int num){ if (num > 5) return ; if (st.find(x) != st.end()){ flag = false ; return ; } if (node[x].fa != -1) judge(node[x].fa, num + 1); if (node[x].mon != -1) judge(node[x].mon,num + 1); } int main(){ int n, id, fa, mon; char sex; memset (node,-1, sizeof (node)); scanf ( "%d" ,&n); while (n--){ scanf ( "%d %c %d %d" ,&id, &sex, &fa, &mon); node[id].fa = fa; node[id].mon = mon; node[id].sex = sex; node[fa].sex = 'M' ; //父母也要设置性别啊...不然扣了8分orz node[mon].sex = 'F' ; } scanf ( "%d" ,&n); int x, y; while (n--){ cin >> x >> y; if (node[x].sex == node[y].sex){ puts ( "Never Mind" ); continue ; } st.clear(); flag = true ; dfs(x,1); //深搜将四代存进set judge(y,1); //深搜看四代是否与x重,用st.find() if (flag == true ) puts ( "Yes" ); else puts ( "No" ); } } |
题目:L2-015. 互评成绩 用力戳我直达原题
题意: 模拟,sort,最后存stack。如此水题,去年决赛时竟没做满分,而且弄得很复杂.......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include <bits/stdc++.h> using namespace std; bool cmp( const int &a, const int &b){ return a > b;} int main(){ int n, k , m, x; double sco[10010]; scanf ( "%d%d%d" ,&n, &k, &m); for ( int i = 0; i < n; i++){ int maxx = -1, minn = 110; for ( int j = 0; j < k; j++){ scanf ( "%d" ,&x); maxx = max(maxx, x); minn = min(minn, x); sco[i] += x; } sco[i] -= (maxx + minn); //去掉最高分和最低分 } sort(sco, sco + n, cmp); stack< double >sta; for ( int i = 0; i < m; i++){ sta.push(sco[i] / (k - 2)); } bool cnt = false ; while (!sta.empty()){ //倒序输出 if (cnt) printf ( " " ); cnt = true ; printf ( "%.3f" ,sta.top()); sta.pop(); } } |
题目:L1-006 抢红包 用力戳我直达原题
题意:求最长的 最小连续因子
做法:三个for暴力枚举。注意len==1也要跑,因为例如 9 * 9,结果是1 3 ,而不是 1 9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <bits/stdc++.h> #define scf0(a) scanf("%s",&a) #define scf1(a) scanf("%d",&a) #define scf2(a,b) scanf("%d%d",&a,&b) #define scf3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define MEM(a,b) memset(a,b,sizeof(a)) #define pii pair<int,int> #define pdd pair<double,double> #define LL long long using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-8; const int maxn = 10000 + 5; int main() { int n, len, i, j; scf1(n); bool flag = false ; for (len = 12; len >= 1; len--) { //长度从12往1搜 for (i = 2; i <= ( int ) sqrt (n); i++) { //i代表连续因子的第一个数,因子最大只能是sqrt(n) LL sum = 1; for (j = i; j <= len+i-1; j++) { //连续len个数相乘 sum *= j; } if (n % sum == 0) { flag = true ; break ; } } if (flag) break ; } if (flag) { printf ( "%d\n" ,len); bool cnt = false ; for ( int k = i; k <= len+i-1; k++) { if (cnt) printf ( "*" ); cnt = true ; printf ( "%d" ,k); } puts ( "" ); } else { printf ( "1\n%d" ,n); } } |