CodeForces div3 第一场

A Wrong Subtraction

题意: 对于一个数操作n次,操作如下: 如果末尾是0就将这个数除以10, 如果末尾不是0就将这个数-1, 直接做就好了。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 1e5+10;
17 int main(){
18     ///Fopen;
19     int n, k;
20     scanf("%d%d", &n, &k);
21     while(k--){
22         int t = n%10;
23         if(t) n--;
24         else n/=10;
25     }
26     printf("%d", n);
27     return 0;
28 }

B Two-gram

题意:求2个连续字符出现次数最多的那2个字符。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 1e3+10;
17 int cnt[N][N];
18 char str[N];
19 int main(){
20     ///Fopen;
21     int n;
22     scanf("%d", &n);
23     scanf("%s", str);
24     int ans = -1, s1, s2;
25     for(int i = 0; i < n-1; i++){
26         int x = str[i]-‘A‘;
27         int y = str[i+1]-‘A‘;
28         cnt[x][y]++;
29         if(cnt[x][y] > ans){
30             ans = cnt[x][y];
31             s1 = x;
32             s2 = y;
33         }
34     }
35     printf("%c%c",s1+‘A‘,s2+‘A‘);
36     return 0;
37 }

C Less or Equal

题意:给你一个数组, 要求找到一个数在[1,1e9]之间,使得恰好有k个数小于等于他,如果没有输出-1, 有就输出任意一个合法答案。

题解:sort一下数组, 如果第k个数等于第k+1个数就找不到一个数满足题意了。 还有0个的情况。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 2e5+10;
17 int n, m;
18 int A[N];
19 int main(){
20     ///Fopen;
21     scanf("%d%d", &n, &m);
22     for(int i = 1; i <= n; i++)
23         scanf("%d", &A[i]);
24     sort(A+1, A+1+n);
25     if(m == 0){
26         if(A[1] == 1) printf("-1");
27         else printf("1");
28     }
29     else if(m == n){
30         printf("%d", (int)1e9);
31     }
32     else{
33         if(A[m] == A[m+1]) printf("-1");
34         else printf("%d", A[m]);
35     }
36     return 0;
37 }

D Divide by three, multiply by two

题意:将题目给的数重新排列,使得前一个数是后一个数的3倍,或者后一个数是前一个数2倍的情况最多。

题解:这个特殊的数列是不可能形成一个环的。DFS找一下就可以了,从长的开始输出。(也有更简单的方法, 但是当时写了DFS就DFS了。。。)

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 1e2+10;
17 int n, m;
18 LL A[N];
19 int cnt[N];
20 LL ans[N];
21 map<LL,int> mp;
22 void dfs(LL u){
23     cnt[mp[u]] = 1;
24     if(u%3 == 0 && mp.count(u/3)){
25         if(cnt[mp[u/3]] == -1)
26             dfs(u/3);
27         cnt[mp[u]] = max(cnt[mp[u]],1+cnt[mp[u/3]]);
28     }
29     if(mp.count(u*2)){
30         if(cnt[mp[u*2]] == -1)
31             dfs(u*2);
32         cnt[mp[u]] = max(cnt[mp[u]],1+cnt[mp[u*2]]);
33     }
34 }
35 void Show(LL u, int h){
36     printf("%I64d ", u);
37     A[mp[u]] = 0;
38     h--;
39     if(h == 0) return ;
40     if(u%3 == 0 && mp.count(u/3) && cnt[mp[u/3]] == h && A[mp[u/3]] != 0)
41         Show(u/3,h);
42
43     else if(mp.count(u*2)
44             && A[mp[u*2]] != 0
45              && cnt[mp[u*2]] == h){
46         Show(u*2,h);
47     }
48 }
49 int main(){
50     scanf("%d", &n);
51     memset(cnt, -1, sizeof(cnt));
52     for(int i = 1; i <= n; i++){
53         scanf("%I64d", &A[i]);
54         mp[A[i]] = i;
55     }
56     for(int i = 1; i <= n; i++){
57         if(cnt[i] == -1){
58             dfs(A[i]);
59         }
60
61     }
62     while(1){
63         int Max = 0, save;
64         for(int i = 1; i <= n; i++){
65             if(cnt[i] >= Max && A[i] != 0)
66                 Max = cnt[i], save = i;
67         }
68         if(Max == 0) break;
69         Show(A[save], Max);
70     }
71     return 0;
72 }

E Cyclic Components

题意:求环的个数, 这个环需要这个环上的点都只有2条边。

题解:DFS一下, 从某个点出发, 如果按照一个方向走能回到该点, 并且路上的点都只有2条边, 就cnt++;

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 1e5+10;
17 vector<int> son[2*N];
18 int vis[N*2];
19 int cnt = 0;
20 void dfs(int u, int l, int v){
21     if(u == v){cnt++; return ;}
22     if(vis[v]) return ;
23     vis[v] = 1;
24     if(son[v].size() == 2){
25         if(l == son[v][0]) dfs(u,v, son[v][1]);
26         else dfs(u,v,son[v][0]);
27     }
28 }
29 int main(){
30     ///Fopen;
31     int n, m;
32     scanf("%d%d", &n, &m);
33     for(int i = 1; i <= m; i++){
34         int u, v;
35         scanf("%d%d", &u, &v);
36         son[u].pb(v);
37         son[v].pb(u);
38     }
39     for(int i = 1; i <= n; i++){
40         if(!vis[i]) {
41             vis[i] = 1;
42             if(son[i].size() == 2){
43                 dfs(i, i, son[i][0]);
44             }
45         }
46     }
47     printf("%d", cnt);
48     return 0;
49 }

F. Consecutive Subsequence

题意:求最长的递增子序列。 这个序列的前一项与后一项相差为1。

题解:开一个map, 映射一下某个值上一次访问的位置,然后每次处理一个新的点, 如果比这个点小1的值的mp不为0, 就将这个点的位置指向上一个比他小1的点,记录长度, 如果没有比他小1的点, 就指向-1, 然后长度记为1, 然后每次更新最长的值 和 相应的位置就好了。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 2e5+10;
17 map<int,int> mp;
18 int cnt[N];
19 int pre[N];
20 int A[N];
21 int Max = 0, B = 0;
22 void Show(int u){
23     if(u == -1) return;
24     Show(pre[u]);
25     printf("%d ", u);
26 }
27 int main(){
28     ///Fopen;
29     int n;
30     scanf("%d", &n);
31     for(int i = 1; i <= n; i++)
32         scanf("%d", &A[i]);
33     for(int i = 1; i <= n; i++){
34         int t = A[i]-1;
35         if(mp.count(t)){
36             pre[i] = mp[t];
37             cnt[i] = cnt[pre[i]]+1;
38         }
39         else pre[i] = -1, cnt[i] = 1;
40         if(Max < cnt[i]){
41             Max = cnt[i];
42             B = i;
43         }
44         mp[A[i]] = i;
45     }
46     printf("%d\n", cnt[B]);
47     Show(B);
48     return 0;
49 }

原文地址:https://www.cnblogs.com/MingSD/p/9036282.html

时间: 2024-08-29 20:53:40

CodeForces div3 第一场的相关文章

早晨训练赛第一场 B题 哈希

早晨训练赛第一场 B题 B - Trees in a Row Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 402B Description The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i 

【补题】组队训练第二场 &amp; 个人训练第一场

组队第二场: C题 CodeForces Gym 100735D 题意:给你N个木棍,问他们能拼成多少个三角形. 思路:从小到大排序,然后贪心地去取. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #include<vector> 7 #include<set> 8

2015 ACM多校训练第一场

在下面网址看效果更佳>_< http://mlz000.github.io/2015/08/07/2015-ACM%E5%A4%9A%E6%A0%A1%E8%AE%AD%E7%BB%83%E7%AC%AC%E4%B8%80%E5%9C%BA/ 题外话 这个暑假以前就决定要把这次多校的所有题全补了,中间断断续续,总算把第一场的题补全了,鄙视一下颓废的自己... hdu 5288(1001) OO's Sequence Solution 水题,定义两个数组L[i],R[i]示第i个数左侧和右侧最接

计蒜之道2015程序设计大赛初赛第一场——搜狗输入法的分词算法

(一)题面 计蒜之道2015程序设计大赛初赛第一场——搜狗输入法的分词算法 搜狗输入法最近的用户输入中出现了一种新的输入模式,形如 “0k1234567”,搜狗的工程师发现这一模式后了解到,这是一种新被提出的对于十五进制数字的标记模式,其中 “0k” 是标记进制为15的前缀标记,之后的部分 “1234567” 是实际的十五进制的数字串. 在发现这一标记模式后,搜狗的工程师开始尝试在已有的分词算法上进一步加入对于十五进制数字串的处理,把网页上的这种形式的 15 进制数正确地提取出来.我们知道,标记

2014多校第一场A题 || HDU 4861 Couple doubi

题目链接 题意 : 有K个球,给你一个数P,可以求出K个值,(i=1,2,...,k) : 1^i+2^i+...+(p-1)^i (mod p).然后女朋友先取,再xp取,都希望赢,如果女朋友能赢输出YES,否则输出NO 思路 :这个题,在纸上算算差不多就出来结果了,因为要赢,所以一开始必定拿大的,根据规律可以发现最后的那个取余结果不是0就是某个数,所以就看那个数有奇数个还是偶数个即可. 官方题解: 1 #include <stdio.h> 2 #include <string.h&g

HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)

http://acm.hdu.edu.cn/showproblem.php?pid=4508 题目大意: 给定一些数据. 每组数据以一个整数n开始,表示每天的食物清单有n种食物. 接下来n行,每行两个整数a和b,其中a表示这种食物可以带给湫湫的幸福值(数值越大,越幸福),b表示湫湫吃这种食物会吸收的卡路里量. 最后是一个整数m,表示湫湫一天吸收的卡路里不能超过m. 思路: 完全背包. 一开始以为是01背包. 敲了01后样例2不对啊!!! 然后改成完全就过了..就改循环体就好了.. #includ

计蒜之道 初赛第一场B 阿里天池的新任务(简单)

阿里“天池”竞赛平台近日推出了一个新的挑战任务:对于给定的一串 DNA 碱基序列 tt,判断它在另一个根据规则生成的 DNA 碱基序列 ss 中出现了多少次. 首先,定义一个序列 ww: \displaystyle w_{i} = \begin{cases}b, & i = 0\\(w_{i-1} + a) \mod n, & i > 0\end{cases}w?i??={?b,?(w?i−1??+a)modn,???i=0?i>0?? 接下来,定义长度为 nn 的 DNA 碱

2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)

题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少时最少的个数,rb代表1最多时的个数.一张牌翻两次和两张牌翻一次 得到的奇偶性相同,所以结果中lb和最多的rb的奇偶性相同.如果找到了lb和rb,那么,介于这两个数之间且与这两个数奇偶性相同的数均可取到,然后在这个区间内求组合数相加(若lb=3,rb=7,则3,5,7这些情况都能取到,也就是说最后的

2017 计蒜之道 初赛 第一场 B.阿里天池的新任务

2017 计蒜之道 初赛 第一场 B.阿里天池的新任务 1 /* QYP kuai wo dai ma*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<iomanip> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cstdio> 8 #include<queue> 9 #include<ctime