Codeforces Round #479 (Div. 3)
1 second
256 megabytes
standard input
standard output
Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm:
- if the last digit of the number is non-zero, she decreases the number by one;
- if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).
You are given an integer number nn. Tanya will subtract one from it kk times. Your task is to print the result after all kk subtractions.
It is guaranteed that the result will be positive integer number.
The first line of the input contains two integer numbers nn and kk (2≤n≤1092≤n≤109, 1≤k≤501≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.
Print one integer number — the result of the decreasing nn by one kk times.
It is guaranteed that the result will be positive integer number.
512 4
50
1000000000 9
1
水题,不说。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
#define mod 1000000007
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define pbk pop_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
using namespace std;
LL n;
int k;
int main()
{
scanf("%I64d%d",&n,&k);
for(int i=;i<=k;i++)
{
if(n%==)
n/=;
else
n--;
}
printf("%I64d\n",n);
return ;
}
1 second
256 megabytes
standard input
standard output
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams.
You are given a string ss consisting of nn capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string ss = "BBAABBBA" the answer is two-gram "BB", which contained in ss three times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other.
The first line of the input contains integer number nn (2≤n≤1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.
Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string ss as a substring (i.e. two consecutive characters of the string) maximal number of times.
7
ABACABA
AB
5
ZZZAA
ZZ
拿个string 和map 存一存就好了。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
#define mod 1000000007
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define pbk pop_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
using namespace std;
const int N=1e2+;
map<string,int> st;
string str,s;
int n,ans;
int main()
{
cin>>n;
cin>>s;
ios::sync_with_stdio(false);
for(int i=;i<n-;i++)
{
str="";
str+=s[i];
str+=s[i+];
st[str]++;
}
ans=;
for(auto p:st)
{
if(p.second>ans)
{
ans=p.second;
str=p.first;
}
}
cout<<str<<endl;
}
2 seconds
256 megabytes
standard input
standard output
You are given a sequence of integers of length nn and integer number kk. You should print any integer number xx in the range of [1;109][1;109](i.e. 1≤x≤1091≤x≤109) such that exactly kk elements of given sequence are less than or equal to xx.
Note that the sequence can contain equal elements.
If there is no such xx, print "-1" (without quotes).
The first line of the input contains integer numbers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 0≤k≤n0≤k≤n). The second line of the input contains nninteger numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the sequence itself.
Print any integer number xx from range [1;109][1;109] such that exactly kk elements of given sequence is less or equal to xx.
If there is no such xx, print "-1" (without quotes).
7 4
3 7 5 1 10 3 20
6
7 2
3 7 5 1 10 3 20
-1
如果有解就是排完序后第k个。特判k==0,当最小数为1的时候无解,否则为1。特判k>n无解。特判a[k]==a[k+1]的情况,这样也是无解的。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
#define mod 1000000007
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define pbk pop_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
using namespace std;
const int N=2e5+;
int a[N];
int n,m,k,t;
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",a+i);
sort(a+,a+n+);
if(k==)
{
if(a[]==)
printf("-1\n");
else
printf("1\n");
return ;
}
printf("%d\n",(k>n || (k<n && a[k]==a[k+]))?-:a[k]);
return ;
}
1 second
256 megabytes
standard input
standard output
Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1operations of the two kinds:
- divide the number xx by 33 (xx must be divisible by 33);
- multiply the number xx by 22.
After each operation, Polycarp writes down the result on the board and replaces xx by the result. So there will be nn numbers on the board after all.
You are given a sequence of length nn — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
The first line of the input contatins an integer number nn (2≤n≤1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅10181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Print nn integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
6
4 8 6 3 12 9
9 3 6 12 4 8
4
42 28 84 126
126 42 84 28
2
1000000000000000000 3000000000000000000
3000000000000000000 1000000000000000000
我可能是代码最长的那个233333。
反正就暴力链嘛,找到一个链一个,然后找没有被链的那个当头输出就好了。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
#define mod 1000000007
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define pbk pop_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
using namespace std;
const int N=1e2+;
LL a[N];
int nexted[N];
int n,hd,vis[N];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%I64d",a+i);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(!vis[j] && a[j]%== && a[i]==a[j]/)
{
nexted[i]=j;
vis[j]=;
break;
}
else if(!vis[j] && a[i]%== && a[i]==a[j]*)
{
nexted[i]=j;
vis[j]=;
break;
}
for(int i=;i<=n;i++)
if(vis[i]==)
{
hd=i;
break;
}
for(int i=;i<=n;i++)
{
printf("%I64d ",a[hd]);
hd=nexted[hd];
}
printf("\n");
return ;
}
然后听说了排序新奇写法,就是把含3因数多的尽可能的往前排,在含3因数一样的情况下小的数排前面。这样因为保证有解,排序出来的结果就是答案。
学了下lambda表达式
#include<bits/stdc++.h>
using namespace std;
long long n,a[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%I64d",a+i);
sort(a+,a+n+,[](long long a,long long b)
{
int cnt[]={,};
while(a%==) a/=,cnt[]++;
while(b%==) b/=,cnt[]++;
return cnt[]==cnt[]?a<b:cnt[]>cnt[];
});
for(int i=;i<=n;i++)
printf("%I64d%c",a[i]," \n"[i==n]);
return ;
}
2 seconds
256 megabytes
standard input
standard output
You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph theory.
An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex aa is connected with a vertex bb, a vertex bb is also connected with a vertex aa). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.
Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and vv.
A connected component is a cycle if and only if its vertices can be reordered in such a way that:
- the first vertex is connected with the second vertex by an edge,
- the second vertex is connected with the third vertex by an edge,
- ...
- the last vertex is connected with the first vertex by an edge,
- all the described edges of a cycle are distinct.
A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.
There are 66 connected components, 22 of them are cycles: [7,10,16][7,10,16] and [5,11,9,15][5,11,9,15].
The first line contains two integer numbers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 0≤m≤2⋅1050≤m≤2⋅105) — number of vertices and edges.
The following mm lines contains edges: edge ii is given as a pair of vertices vivi, uiui (1≤vi,ui≤n1≤vi,ui≤n, ui≠viui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,uivi,ui) there no other pairs (vi,uivi,ui) and (ui,viui,vi) in the list of edges.
Print one integer — the number of connected components which are also cycles.
5 4
1 2
3 4
5 4
3 5
1
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
2
对每个点都做个标记。每个点都dfs一下判断是不是形成一个圈,如果度>2的就可以直接return了。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
#define mod 1000000007
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define pbk pop_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
using namespace std;
const int N=2e5+;
struct edg
{
int next,to;
}edge[N<<];
int head[N],etot,vis[N],ct[N];
int n,m,u,v;
int ans;
void init()
{
clr_1(head);
clr(vis);
clr(ct);
etot=;
ans=;
return ;
}
void addedge(int u,int v)
{
edge[++etot]=(edg){head[u],v};
head[u]=etot;
return ;
}
bool dfs(int u,int hed,int fa,int dep)
{
vis[u]=;
if(ct[u]>)
return ;
int p;
for(int i=head[u];i!=-;i=edge[i].next)
{
p=edge[i].to;
if(p==fa)
continue;
if(vis[p] && p==hed)
{
if(dep>=)
return ;
else
return ;
}
if(!vis[p])
return dfs(p,hed,u,dep+);
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
ct[u]++;
ct[v]++;
}
for(int i=;i<=n;i++)
{
if(!vis[i])
if(dfs(i,i,i,))
ans++;
}
printf("%d\n",ans);
return ;
}
2 seconds
256 megabytes
standard input
standard output
You are given an integer array of length nn.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [x,x+1,…,x+k−1][x,x+1,…,x+k−1] for some value xx and length kk.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4][5,3,1,2,4] the following arrays are subsequences: [3][3], [5,3,1,2,4][5,3,1,2,4], [5,1,4][5,1,4], but the array [1,3][1,3] is not.
The first line of the input containing integer number nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the array itself.
On the first line print kk — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
7
3 3 4 7 5 6 8
4
2 3 5 6
6
1 3 5 2 4 6
2
1 4
4
10 9 8 7
1
1
9
6 7 8 3 4 5 9 10 11
6
1 2 3 7 8 9
LIS的 O(n) 写法 加上离散化。拿上lower_bound去找离散化的数。这样就能解决了。不过map的离散化代码能更短点。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
#define mod 1000000007
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define pbk pop_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
using namespace std;
const int N=2e5+;
int a[N],uni[N],n,m,now[N],pre[N],last,maxn,t,p,ans[N],num[N];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",a+i);
uni[i]=a[i];
}
sort(uni+,uni+n+);
m=unique(uni+,uni+n+)-uni-;
last=maxn=;
for(int i=;i<=n;i++)
{
t=lower_bound(uni+,uni+m+,a[i])-uni;
if(num[t]<)
{
now[t]=i;
num[t]=;
}
p=lower_bound(uni+,uni+m+,a[i]-)-uni;
if(p>m || uni[p]!=a[i]-)
continue;
if(num[t]<num[p]+)
{
now[t]=i;
num[t]=num[p]+;
pre[i]=now[p];
}
if(num[t]>maxn)
{
last=i;
maxn=num[t];
}
}
printf("%d\n",maxn);
for(int i=maxn;i>=;i--)
{
ans[i]=last;
last=pre[last];
}
for(int i=;i<=maxn;i++)
printf("%d ",ans[i]);
printf("\n");
return ;
}
Codeforces Round #479 (Div. 3)的更多相关文章
- Codeforces Round #479 (Div. 3) A. Wrong Subtraction
题目网址:http://codeforces.com/contest/977/problem/A 题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉 ...
- Codeforces Round #479 (Div. 3) C. Less or Equal
题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- Codeforces Round #479 (Div. 3)解题报告
题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...
- Codeforces Round #479 (Div. 3) B. Two-gram
原题代码:http://codeforces.com/contest/977/problem/B 题解:有n个字符组成的字符串,输出出现次数两个字符组合.例如第二组样例ZZ出现了两次. 方法:比较无脑 ...
- Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F
A. Wrong Subtraction 题目大意: 定义一种运算,让你去模拟 题解: 模拟 #include <iostream> #include <cstdio> ...
- Codeforces Round #479 (Div. 3)题解
CF首次推出div3给我这种辣鸡做,当然得写份博客纪念下 A. Wrong Subtraction time limit per test 1 second memory limit per test ...
- E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
#include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...
- Codeforces Round #479 (Div. 3)解题代码
A. Wrong Subtraction #include <bits/stdc++.h> using namespace std; int main() { int n,k; cin&g ...
随机推荐
- 在eclipse安装mybatis的插件
1.在help中打开 2.搜索mybatipse 3:功能简介 1:要查找某一个方法 在dao接口中某一个方法中 按住 Ctrl键 鼠标指到方法名称上 选择open xml 就会自动跳转 ...
- 详解H5中的history单页面,手动实现单页面开发,细说h5单页面原理
就目前来看,前端的单页面开发占了很大一部分,一方面无刷新的切换增强了体验,并且浏览器记录依然存在,前进后退都没问题,在之前我们通地址栏中的hash改变来触发onhashchange方法来实现单页面应用 ...
- rabbitmq集群搭建方法简介(测试机linux centos)【转】
本文将介绍四台机器搭建rabbitmq集群: rabbitmq IP和主机名(每台机器已安装RabbitMQ 3.5.6, Erlang 18.1) 192.168.87.73 localhost73 ...
- FileZilla 配置备份与还原【转】
FileZilla是一款免费开源的FTP软件,安装和配置都很简单.在安装目录下的FileZilla Server Interface.xml和FileZilla Server.xml两个文件是程序的配 ...
- [转载]FFmpeg完美入门[1] - FFmpeg介绍及安装
1 FFmpeg简介 FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件).它提供了录制.转换以及流化音视 频的完整解决方案.它包含了非常先 ...
- [ python ] 集合的使用
集合的使用 可变数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. 集合的增删查 集合是没有改这种说法的: (1)集合的元素无序的: (2)集合的元素为不可变类型 增加 add 为集 ...
- JNDI(Java Naming and Directory Interface,Java命名和目录接口)
JNDI(Java Naming and Directory Interface,Java命名和目录接口)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访 ...
- 查看压缩包内容tar -tf
linux 压缩文件内容查看 分类:Linux | 标签: linux 压缩文件内容查看 2012-03-14 22:01阅读(1243)评论(0) 1. zipinfo 执行zipinfo ...
- IDEA 部署项目的时候出错:Jar not loaded错误
2011-10-18 17:03:52 org.apache.catalina.loader.WebappClassLoader validateJarFile 信息: validateJarFile ...
- WordPress用户登录后根据不同的角色跳转到不同的页面处理
WordPress提供了很多的方法,可以针对这些方法做很多的改造,实现千变万化的需求. 比如这里就有一个这样的需求点:需要根据不同的角色,在登录后转向到不同的页面地址. 一种办法是结合WordPres ...