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 ...
随机推荐
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...
- 【洛谷 P4134】 [BJOI2012]连连看(费用流)
题目链接 首先是可以\(O(n^2)\)枚举出所有符合要求的点对的,然后考虑建图. 还是拆点把每个点拆成入点和出点,源点连入点,出点连汇点,流量都是1,费用都是0. 然后对于没对符合要求的\((x,y ...
- Coursera在线学习---第六节.构建机器学习系统
备: High bias(高偏差) 模型会欠拟合 High variance(高方差) 模型会过拟合 正则化参数λ过大造成高偏差,λ过小造成高方差 一.利用训练好的模型做数据预测时,如果效果不好 ...
- 64_q1
QMsgBox-0-9.20130830git94677dc.fc26.i686.rpm 13-Feb-2017 23:40 40674 QMsgBox-0-9.20130830git94677dc. ...
- Ubuntu 10.04 分辨率调整
最近学长们看了我的本本都在问我,显卡驱动是不是出现什么问题了···分辨率这么差.当时我的分辨率是1024X768,于是我就想修改我的屏幕分辨率改成1280X800.本来很简单的事情,我做起来却非常的曲 ...
- Tutorial 7: Schemas & client libraries
转载自:http://www.django-rest-framework.org/tutorial/7-schemas-and-client-libraries/ Tutorial 7: Schema ...
- Mathtype公式位置偏上
Mathtype公式位置偏上 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公式的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题.然后就是行距稍微大 ...
- acm专题---动态规划
题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096 #1400 : Composition 时间限制:10000ms 单点时限:1 ...
- linux下rz,sz安装
1.sz rz yum安装 yum install lrzsz
- Linux 基础目录和命令
Linux 标准目录结构 初学Linux,首先需要弄清Linux 标准目录结构 / root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存 ...