第三届全国中医药院校大学生程序设计竞赛

Problem A Welcome 模拟
Problem B 绿地装饰 模拟
Problem C 击鼓传花 模拟
Problem D 旅行 线段树
Problem E 解密 模拟
Problem F 巍巍岳麓 最小生成树
Problem G Solution Sets 搜索+剪枝
Problem H 百舸争流 贪心 
Problem I 最长不下降子序列 dp+树状数组
Problem J Mode 模拟
Problem K Matrix 数学+容斥原理
Problem L 药剂 树状数组 

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int w;
 4 void print1() {
 5     for (int i = 1; i <= w; i++) printf(".");
 6 }
 7 void print2() {
 8     printf("o...o"); print1(); printf("o...o"); print1(); printf("o...o"); print1(); printf("ooooo"); print1(); printf("o...o\n");
 9     printf("o...o"); print1(); printf("oo..o"); print1(); printf("o...o"); print1(); printf("o...."); print1(); printf("oo.oo\n");
10     printf("ooooo"); print1(); printf("o.o.o"); print1(); printf("o...o"); print1(); printf("o...."); print1(); printf("o.o.o\n");
11     printf("o...o"); print1(); printf("o..oo"); print1(); printf("o...o"); print1(); printf("o...."); print1(); printf("o...o\n");
12     printf("o...o"); print1(); printf("o...o"); print1(); printf("ooooo"); print1(); printf("ooooo"); print1(); printf("o...o\n");
13 }
14 int main() {
15     cin >> w;
16     print2();
17     return 0;
18 }
Problem A Welcome

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1005;
 4 string ans[maxn], mat[maxn], mat1[5], mat2[5];
 5 int n, k;
 6 void green(int cur) {
 7     for (int i = 0; i < pow(n,cur-1); i++) {
 8         mat[i] = ans[i];
 9         ans[i].clear();
10     }
11  
12     for (int i = 0, now = 0; i < pow(n,cur-1); i++, now += n) {
13         for (int j = 0; j < mat[i].size(); j++) {
14             if (mat[i][j] == '.') {
15                 ans[now] += mat1[0];
16                 ans[now+1] += mat1[1];
17                 ans[now+2] += mat1[2];
18             }
19             else {
20                 ans[now] += mat2[0];
21                 ans[now+1] += mat2[1];
22                 ans[now+2] += mat2[2];
23             }
24         }
25     }
26 }
27 void print() {
28     for (int i = 0; i < pow(n,k); i++)
29         cout << ans[i] << endl;
30 }
31 int main() {
32     //freopen("input.txt", "r", stdin);
33     //freopen("output.txt", "w", stdout);
34     cin >> n >> k;
35     for (int i = 0; i < n; i++) cin >> mat1[i];
36     for (int i = 0; i < n; i++) mat2[i] = (n==2) ? "**" : "***";
37     ans[0] = '.';
38     for (int i = 1; i <= k; i++) green(i);
39     print();
40     return 0;
41 }
Problem B 绿地装饰

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int main() {
 5     int n;
 6     while (cin >> n) {
 7         ll ans = 0;
 8         for (int i = 1; i <= n; i++) {
 9             int a; scanf("%d",&a);
10             ans += a;
11         }
12         printf("%lld\n",ans);
13     }
14     return 0;
15 }
Problem C 击鼓传花

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 const int maxn = 2e5+5;
  5 const LL INF = 1ll<<50;
  6 struct node {
  7     LL c[6];
  8 }T[maxn*4], a[maxn];
  9 LL r[3][maxn];
 10 int n, m, x, y, z, opt, ll, rr;
 11 char s[2][maxn];
 12 char cr[5];
 13 int leaf[maxn];
 14 node operator + (const node &A, const node &B) {
 15     node C;
 16     C.c[0] = min(A.c[0]+B.c[0],A.c[4]+B.c[5]);
 17     C.c[1] = B.c[1];
 18     C.c[2] = min(A.c[2]+B.c[2],A.c[5]+B.c[4]);
 19     C.c[3] = A.c[3];
 20     C.c[4] = min(A.c[4]+B.c[2],A.c[0]+B.c[4]);
 21     C.c[5] = min(A.c[5]+B.c[0],A.c[2]+B.c[5]);
 22     for (int i = 0; i < 6; i++) C.c[i] = min(INF,C.c[i]);
 23     return C;
 24 }
 25 inline void Update(int i) {
 26     if (s[0][i] == 'P' && s[0][i+1] == 'P') a[i].c[0] = r[0][i];
 27     else a[i].c[0] = INF;
 28     if (s[0][i+1] == 'P' && s[1][i+1] == 'P') a[i].c[1] = r[2][i+1];
 29     else a[i].c[1] = INF;
 30     if (s[1][i] == 'P' && s[1][i+1] == 'P') a[i].c[2] = r[1][i];
 31     else a[i].c[2] = INF;
 32     if (s[0][i] == 'P' && s[1][i] == 'P') a[i].c[3] = r[2][i];
 33     else a[i].c[3] = INF;
 34     a[i].c[4] = min(a[i].c[2]+a[i].c[3],a[i].c[0]+a[i].c[1]);
 35     a[i].c[5] = min(a[i].c[0]+a[i].c[3],a[i].c[1]+a[i].c[2]);
 36     for (int j = 4; j < 6; j++) a[i].c[j] = min(INF,a[i].c[j]);
 37 }
 38 inline void update(int k) {
 39     T[k] = T[k<<1]+T[k<<1|1];
 40 }
 41 inline void modify(int x) {
 42     T[leaf[x]] = a[x];
 43     x = leaf[x];
 44     for (x>>=1; x; x>>=1) update(x);
 45 }
 46 inline void build(int l, int r, int k){
 47     if (l == r) {
 48         T[k] = a[l];
 49         leaf[l] = k;
 50         return;
 51     }
 52     int mid = (l+r)>>1;
 53     build(l,mid,k<<1);
 54     build(mid+1,r,k<<1|1);
 55     update(k);
 56 }
 57 inline node Ask(int l, int r, int k) {
 58     if (ll <= l && r <= rr) return T[k];
 59     int mid = (l+r)>>1;
 60     if (rr <= mid) return Ask(l,mid,k<<1);
 61     if (mid < ll) return Ask(mid+1,r,k<<1|1);
 62     return Ask(l,mid,k<<1)+Ask(mid+1,r,k<<1|1);
 63 }
 64 inline void Print(LL x) {
 65     if(x < INF) printf("%lld\n",x);
 66     else printf("-1\n");
 67 }
 68 int main() {
 69     scanf("%d%d",&n,&m);
 70     scanf("%s%s",s[0]+1,s[1]+1);
 71     for (int i = 0; i < 2; i++)
 72         for(int j = 1; j < n; j++)
 73             scanf("%lld",&r[i][j]);
 74     for (int i = 1; i <= n; i++) scanf("%lld",&r[2][i]);
 75     for (int i = 1; i < n; i++) Update(i);
 76     build(1,n-1,1);
 77     while (m--) {
 78         scanf("%d",&opt);
 79         if (opt == 1) {
 80             int l,r; scanf("%d%d",&l,&r);
 81             int L = l, R = r;
 82             if (L > n) L -= n;
 83             if (R > n) R -= n;
 84             if (L > R) swap(L,R), swap(l,r);
 85             if (L == R) {
 86                 if (l == r) {
 87                     char ch;
 88                     if (l > n) ch = s[1][L];
 89                     else ch = s[0][L];
 90                     if (ch == 'P') printf("0\n");
 91                     else printf("-1\n");
 92                 }
 93                 else {
 94                     if (L != n) Print(a[L].c[3]);
 95                     else Print(a[n-1].c[1]);
 96                 }
 97                 continue;
 98             }
 99             ll = L, rr = R-1;
100             node ret = Ask(1,n-1,1);
101             if (l <= n && r <= n) Print(ret.c[0]);
102             else if (l > n && r <= n) Print(ret.c[5]);
103             else if (l <= n && r > n) Print(ret.c[4]);
104             else if (l > n && r > n) Print(ret.c[2]);
105         }
106         else if (opt == 2) {
107             scanf("%d%d%d",&x,&y,&z);
108             r[x][y] = z;
109             if (y < n) Update(y), modify(y);
110             if (y > 1) Update(y-1), modify(y-1);
111         }
112         else if (opt == 3) {
113             scanf("%d%s",&y,cr);
114             if (y > n) x = 1,y -= n;
115             else x = 0;
116             s[x][y] = cr[0];
117             if (y < n) Update(y), modify(y);
118             if (y > 1) Update(y-1), modify(y-1);
119         }
120     }
121     return 0;
122 }
Problem D 旅行

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main() {
 4     string s; cin >> s;
 5     string ans;
 6     for (int i = 0; i < s.size(); i++) {
 7         ans += s[i];
 8         if ((i+1)%8 == 0) {
 9             for (int j = ans.size()-1; j >= 0; j--)
10                 cout << ans[j];
11             ans.clear();
12         }
13     }
14     for (int j = ans.size()-1; j >= 0; j--)
15         cout << ans[j];
16     cout << endl;
17     return 0;
18 }
Problem E 解密

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 3005, inf = 0x3f3f3f3f;
 4 struct edge {
 5     int u, v, w;
 6     edge() {}
 7     edge(int u, int v, int w) : u(u), v(v), w(w) {}
 8 }eds[4005];
 9  
10 int n, m, s, t, st, sum, mx, ans = inf;
11 int p[maxn];
12 bool cmp(edge a, edge b) { return a.w < b.w; }
13 int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
14 void Kruskal() {
15     sort(eds+1, eds+1+m, cmp);
16     for (int i = 1; i <= m; i++) {
17         if ((s == eds[i].u && t == eds[i].v) || (t == eds[i].u && s == eds[i].v)) {
18             st = eds[i].w;
19             break;
20         }
21     }
22  
23     for (int i = 1; i <= m-n+2; i++) {
24         for (int j = 1; j <= n; j++) p[j] = j;
25         p[s] = t, mx = st, sum = 1;
26         for (int j = i; j <= m; j++) {
27             int x = find(eds[j].u); int y = find(eds[j].v);
28             if (x != y) { mx = max(mx, eds[j].w); p[x] = y; sum++; }
29             if (sum == n-1) {
30                 ans = min(ans,mx);
31                 break;
32             }
33         }
34     }
35 }
36 int main() {
37     scanf("%d%d",&n,&m);
38     for (int i = 1; i <= m; i++) {
39         scanf("%d%d%d",&eds[i].u,&eds[i].v,&eds[i].w);
40     }
41     scanf("%d%d",&s,&t); Kruskal();
42     printf("%d %d\n",n-1,ans);
43     return 0;
44 }
Problem F 巍巍岳麓

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int n, m, ans = 0;
 5 const ll mx = 1e15;
 6 void dfs(int cur, int last, ll b, ll a) {
 7     if (b*last+(n-cur+1)*a < a*last) return;
 8     if (b*m+(n-cur)*last >= a*m) return;
 9     if (cur == n) {
10         if (a % (a-b) == 0) ans++;
11         return;
12     }
13     if (b > mx) {
14         ll k = __gcd(a,b);
15         a /= k, b /= k;
16     }
17     for (int i = last; i <= m; i++)
18         dfs(cur+1,i,b*i+a,a*i);
19 }
20 int main() {
21     scanf("%d%d",&n,&m);
22     dfs(1,1,0,1);
23     printf("%d\n",ans);
24     return 0;
25 }
Problem G Solution Sets

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100005;
 4 struct py {
 5     string name;
 6     int a;
 7 }pys[maxn];
 8 int b[maxn];
 9 bool cmp(py x, py y) {
10     if (x.a == y.a) return x.name < y.name;
11     return x.a > y.a;
12 }
13 bool cmp1(int x, int y) { return x > y; }
14 int main() {
15     //freopen("input.txt", "r", stdin);
16     int n; cin >> n;
17     for (int i = 1; i <= n; i++)
18         cin >> pys[i].name >> pys[i].a;
19     //sort(pys+1,pys+1+n,cmp);
20     int m; cin >> m;
21     for (int i = 1; i <= m; i++)
22         cin >> b[i];
23     sort(b+1,b+1+m,cmp1);
24  
25     string name; cin >> name;
26     int now_r, now_s;
27     // 成绩最好
28     for (int i = 1; i <= n; i++) {
29         if (pys[i].name == name) {
30             pys[i].a += b[1];
31             break;
32         }
33     }
34     sort(pys+1,pys+1+n,cmp);
35     for (int i = 1; i <= n; i++) {
36         if (pys[i].name == name) {
37             now_r = i;
38             now_s = pys[i].a;
39             break;
40         }
41     }
42     for (int i = n, pos = now_r+1; i >= now_r+1 && pos <= n; i--, pos++) {
43         if (now_s < pys[i].a+b[pos] || (pys[i].a+b[pos] == now_s && name > pys[i].name)) {
44             now_r++, i++;
45         }
46     }
47     cout << now_r;
48     // 成绩最差
49     for (int i = 1; i <= n; i++) {
50         if (pys[i].name == name) {
51             pys[i].a -= b[1];
52             pys[i].a += b[n];
53             break;
54         }
55     }
56     sort(pys+1,pys+1+n,cmp);
57     for (int i = 1; i <= n; i++) {
58         if (pys[i].name == name) {
59             now_r = i;
60             now_s = pys[i].a;
61             break;
62         }
63     }
64     for (int i = now_r+1, pos = n-now_r; i <= n && pos >= 1; i++, pos--) {
65         if (now_s < pys[i].a+b[pos] || (pys[i].a+b[pos] == now_s && name > pys[i].name)) {
66             now_r++;
67         }
68         else {
69             i--;
70         }
71     }
72     cout << " " << now_r << endl;
73     return 0;
74 }
Problem H 百舸争流

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 100005;
 5 int n, l, ans = 0, cnt = 0;
 6 struct A {
 7     int val, id;
 8 }a[maxn];
 9 int b[2][maxn], c[2][maxn], id[maxn];
10 bool cmp (A x, A y) { return x.val < y.val; }
11 int lowbit(int x) { return x&(-x); }
12 void add(int k, int x, int d) {
13     while (x <= cnt) {
14         c[k][x] = max(c[k][x],d);
15         x += lowbit(x);
16     }
17 }
18 int sum(int k, int x) {
19     int ret = 0;
20     while (x > 0) {
21         ret = max(c[k][x], ret);
22         x -= lowbit(x);
23     }
24     return ret;
25 }
26 int main() {
27     scanf("%d%d",&n,&l);
28     for (int i = 1 ; i <= n; i++)
29         scanf("%d",&a[i].val), a[i].id = i;
30     sort(a+1,a+1+n,cmp);
31     for (int i = 1; i <= n; i++) {
32         if (a[i].val == a[i-1].val) id[a[i].id] = cnt;
33         else  id[a[i].id] = ++cnt;
34     }
35  
36     for (int i = 1; i <= n; i++) {
37         b[0][i] = sum(0,id[i])+1;
38         add(0,id[i],b[0][i]);
39         if (i > l) {
40             b[1][i] = sum(1,id[i])+1;
41             add(1,id[i],b[1][i]);
42             add(1,id[i-l],b[0][i-l]);
43         }
44         ans = max(ans,b[1][i]);
45     }
46     for (int i = 1; i <= n-l; i++)
47         ans = max(ans,b[0][i]);
48     printf("%d\n",ans);
49     return 0;
50 }
Problem I 最长不下降子序列

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 30005;
 4 int a[maxn], mx = 0;
 5 int main() {
 6     memset(a, 0, sizeof(a));
 7     int n; scanf("%d",&n);
 8     for (int i = 1; i <= n; i++) {
 9         int x; scanf("%d",&x);
10         a[x]++;
11     }
12     for (int i = 1; i <= 30000; i++)
13         mx = max(mx,a[i]);
14     for (int i = 1; i <= 30000; i++)
15         if (a[i] == mx) printf("%d %d\n", i, mx);
16     return 0;
17 }
Problem J Mode

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1000005;
 5 ll f[maxn];
 6 int main() {
 7     ll n, m; scanf("%lld%lld",&n,&m);
 8     n %= m;
 9     f[1] = 0; f[2] = 1;
10     for (int i = 3; i <= m; i++)
11         f[i] = (i-1)*(f[i-1]+f[i-2])%m;
12     ll ans = f[n]*f[n]%m;
13     printf("%lld\n",ans);
14     return 0;
15 }
Problem K Matrix

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1010;
 4 int C[30][maxn][maxn], ans[30];
 5 string mat[maxn];
 6 int n, m;
 7 int lowbit(int y) {
 8     return y&(-y);
 9 }
10 void add(int ch, int x, int y, int d) {
11     while (y <= m) {
12         C[ch][x][y] += d; y += lowbit(y);
13     }
14 }
15 int sum(int ch, int x, int y) {
16     int ret = 0;
17     while (y > 0) {
18         ret += C[ch][x][y]; y -= lowbit(y);
19     }
20     return ret;
21 }
22 int main() {
23     while (scanf("%d%d",&n,&m) == 2) {
24         memset(C,0,sizeof(C));
25         for (int i = 1; i <= n; i++) {
26             cin >> mat[i]; mat[i] = "0"+mat[i];
27             for (int j = 1; j <= m; j++) {
28                 add(mat[i][j]-'a',i,j,1);
29             }
30         }
31         int q; scanf("%d",&q);
32         while (q--) {
33             int op; scanf("%d",&op);
34             if (op == 0) {
35                 int x, y; scanf("%d%d",&x,&y); getchar();
36                 char z; scanf("%c",&z);
37                 add(mat[x][y]-'a',x,y,-1);
38                 mat[x][y] = z;
39                 add(mat[x][y]-'a',x,y,1);
40             }
41             else {
42                 memset(ans,0,sizeof(ans));
43                 int x1, x2, y1, y2;
44                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
45                 for (int i = x1; i <= x2; i++) {
46                     for (int ch = 0; ch < 26; ch++) {
47                         ans[ch] += sum(ch,i,y2)-sum(ch,i,y1-1);
48                     }
49                 }
50                 printf("%d",ans[0]);
51                 for (int i = 1; i < 26; i++) printf(" %d",ans[i]);
52                 printf("\n");
53             }
54         }
55     }
56     return 0;
57 }
Problem L 药剂

 

posted @ 2019-10-19 20:20  麻辣猪仔  阅读(874)  评论(3编辑  收藏  举报