2017 JUST Programming Contest 2.0

B. So You Think You Can Count?

dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1e5 + 50;
 5 const int mod = 1e9+7;
 6 ll dp[maxn];//dp[i]表示以i结尾的方案数。
 7 int num[20];
 8 char s[maxn];
 9 int main()
10 {
11     int n;
12     cin >> n;
13     cin >> s + 1;
14     dp[0] = 1;
15     for(int i = 1;i <= n;i++)
16     {
17         memset(num, 0,sizeof(num));
18         for(int j = i;j > 0;j--)
19         {
20             num[s[j]-'0']++;
21             if(num[s[j]-'0'] > 1) break;
22             dp[i] = (dp[i] + dp[j-1])%mod;
23         }
24     }
25     cout << dp[n] << endl;
26     return 0;
27 }
View Code

C. MRT Map

思路:存下每个字符串26个字母出现的次数,然后建边的时候计算一遍权值,然后dijkstra就好了。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int,int> P;
 4 const int maxn = 5e5 + 50;
 5 const int INF = 0x3f3f3f3f;
 6 int n, m;
 7 int num[maxn][26];
 8 int cal(int u, int v)
 9 {
10     int ans = 0;
11     for(int i = 0;i < 26;i++){
12         if(num[u][i] && num[v][i])
13             ans++;
14     }
15     return ans;
16 }
17 struct edge{
18     int to;
19     int cost;
20 }e;
21 vector <edge> G[maxn];
22 int d[maxn];
23 void add(int u, int v)
24 {
25     e.to = v;
26     e.cost = cal(u,v);
27     G[u].push_back(e);
28 }
29 void dijkstra(int s)
30 {
31     priority_queue<P,vector<P>,greater<P> > que;
32     fill(d ,d + n + 1,INF);
33     d[s] = 0;
34     que.push(P(0,s));
35     while(!que.empty())
36     {
37         P p= que.top();que.pop();
38         int v = p.second;
39         if(d[v] < p.first) continue;
40         for(int i = 0;i < G[v].size();i++)
41         {
42             edge e = G[v][i];;
43             if(d[e.to] > d[v] +e.cost)
44             {
45                 d[e.to]= d[v] + e.cost;
46                 que.push(P(d[e.to],e.to));
47             }
48         }
49     }
50 }
51 int main()
52 {
53     std::ios::sync_with_stdio(false);
54     cin >> n >> m;
55     string a;
56     for(int i = 1;i <= n;i++)
57     {
58         cin >> a;
59         int len = a.size();
60         for(int j = 0;j < len;j++){
61             if(a[j] >= 'a' && a[j] <= 'z') num[i][a[j] - 'a']++;
62             else num[i][a[j] - 'A']++;
63         }
64 
65     }
66     for(int i = 1;i <= m;i++)
67         {
68             int u, v;
69             cin >> u >> v;
70             add(u, v);
71             add(v, u);
72         }
73     int s, t;
74     cin >> s >> t;
75     dijkstra(s);
76     cout << d[t] << endl;
77     return 0;
78 }
View Code

D. Husam's Bug跑

签到题1

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     std::ios::sync_with_stdio(false);
 6     int t;
 7     cin >> t;
 8     while(t--)
 9     {
10         string s;
11         int cnt = 0, num = 0, sum = 0;//字母 数字 特殊符号
12         cin >> s;
13         for(int i = 0;i < s.size();i++)
14         {
15             if(s[i] >= 'a' && s[i] <= 'z') cnt++;
16             if(s[i] >= 'A' && s[i] <= 'Z') cnt++;
17             if(s[i] >= '0' && s[i] <= '9') num++;
18             if(s[i] == '@' || s[i] == '?' || s[i] == '!') sum++;
19         }
20         if(cnt < 4){
21             cout << "The last character must be a letter." << endl;
22         }
23         else if(num < 4){
24             cout << "The last character must be a digit." << endl;
25         }
26         else if(sum < 2){
27             cout << "The last character must be a symbol." << endl;
28         }
29         else cout << "The last character can be any type." << endl;
30     }
31     return 0;
32 }
View Code

E. Abdalrahman Ali Bugs

思路:容易得出X是在 2 - 最大出现次数之间,直接暴力枚举所有答案取最优解。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll num[27];
 5 ll check(ll x)
 6 {
 7     ll ans = 0;
 8     for(int i = 0;i < 26;i++){
 9         ans += (num[i] % x) * num[i];
10     }
11     return ans;
12 }
13 int main()
14 {
15     std::ios::sync_with_stdio(false);
16     string a;
17     cin >> a;
18     ll mx = 0;
19     for(int i = 0;i < a.size();i++)
20     {
21         int t = a[i] - 'a';
22         num[t]++;
23         mx = max(num[t],mx);
24     }
25     ll x = 2;
26     ll ans = check(x);
27     for(ll i = 2;i <= mx;i++){
28         if(check(i) < ans){
29             x = i;
30             ans = check(i);
31         }
32     }
33     cout << x << endl;
34     return 0;
35 }
View Code

F. Certifications

思路:在a数组中二分一个大于X的最小值,找不到则输出那一串。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1e5 + 50;
 5 ll a[maxn];
 6 const ll INF = 1e18;
 7 int main()
 8 {
 9     std::ios::sync_with_stdio(false);
10     int n, m;
11     cin >> n;
12     for(int i = 0;i < n;i++){
13         cin >> a[i];
14     }
15     sort(a, a + n);
16     a[n] = INF;
17     cin >> m;
18     while(m--)
19     {
20         int x;
21         cin >> x;
22         int l = 0, r = n, o = 0;
23         while(l <= r){
24             int mid = (l + r) / 2;
25             if(a[mid] >= x) r = mid - 1, o = mid;
26             else l = mid + 1;
27            // cout << o << endl;
28         }
29         if(a[o] == INF) cout << "Dr. Samer cannot take any offer :(." << endl;
30         else cout << a[o] << endl;
31     }
32     return 0;
33 }
View Code

G. In the Chairman's office

qaq签到题2。


 H. Give Me This Pizza

思路:右边最近的较大值,这不是很白的一个单调栈么,暑假队内赛第一场的A,相似度80%。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 50;
 4 int st[maxn], ans[maxn], a[maxn];
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     int n;
 9     cin >> n;
10     memset(ans,-1,sizeof(ans));
11     for(int i = 0;i < n;i++) cin >> a[i];
12     int cnt = 0;
13     for(int i = 0;i < n;i++)
14     {
15         while(cnt > 0 && a[st[cnt]] < a[i]){
16             ans[st[cnt--]] = i;
17         }
18         st[++cnt] = i;
19     }
20     for(int i = 0;i < n;i++){
21         if(ans[i] == -1) cout << ans[i] << " ";
22         else cout << a[ans[i]] << " ";
23     }
24     return 0;
25 }
View Code

I. Husam and the Broken Present 1

思路:对角线元素是平方和,对其开根号即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 50;
 4 int ans[maxn];
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     int n;
 9     cin >> n;
10     int a;
11     for(int i = 0;i < n;i++){
12         for(int j = 0;j < n;j++){
13             cin >> a;
14             if(i == j) ans[i] = sqrt(a);
15         }
16     }
17     for(int i = 0;i < n;i++){
18         if(i == n - 1) cout << ans[i] << endl;
19         else cout << ans[i] << " ";
20     }
21     return 0;
22 }
View Code

K.Counting Time

思路:可以很暴力的一道题,9!枚举出3*3矩阵所有状态,判断和题目所给的矩阵非0位相不相等并且满不满足题意。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int mp[10][10];
 5 int mp2[10][10];
 6 int a[10];
 7 int vis[10];
 8 int ans;
 9 int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
10 int dy[8] = {-1, 0, 1, 0, -1, 1, 1, -1};
11  
12 bool check(int x, int y){
13     if(x >= 0 && x < 3 && y >= 0 && y < 3) return true;
14     else return false;
15 }
16 bool check1(int x, int y,char val)
17 {
18     if(mp2[x][y] == 9) return true;
19     for(int i = 0;i < 8;i++)
20     {
21         int nx = x + dx[i];
22         int ny = y + dy[i];
23         if(check(nx, ny) && mp2[nx][ny] == val + 1) return true;
24     }
25     return false;
26 }
27 bool check3(){
28     for(int i = 0;i < 3;i++){
29         for(int j = 0;j < 3;j++){
30             if(!check1(i, j, mp2[i][j])) return false;
31         }
32     }
33     return true;
34 }
35 int check2()
36 {
37     for(int i = 0;i < 9;i++){
38         int x = i / 3;
39         int y = i % 3;
40         mp2[x][y] = a[i];
41         if(mp[x][y] != a[i] && mp[x][y] != 0) return 0;
42     }
43     if(check3()) return 1;
44     else return 0;
45 }
46 void dfs(int pos)
47 {
48     if(pos >= 9){
49         ans += check2();
50         return;
51     }
52     for(int i = 1;i <= 9;i++)
53     {
54         if(!vis[i]) {
55             a[pos] = i;
56             vis[i] = 1;
57             dfs(pos + 1);
58             vis[i] = 0;
59         }
60     }
61 }
62 int main()
63 {
64     std::ios::sync_with_stdio(false);
65     char c;
66     for(int i = 0;i < 3;i++){
67         for(int j = 0;j < 3;j++)
68         {
69             cin >> c;
70             int t = c - '0';
71             mp[i][j] = t;
72         }
73     }
74     dfs(0);
75     cout << ans << endl;
76     return 0;
77 }
View Code

猜你喜欢

转载自www.cnblogs.com/Carered/p/11767194.html