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 k subtractions.
It is guaranteed that the result will be positive integer number.
The first line of the input contains two integer numbers 1≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.
Print one integer number — the result of the decreasing k times.
It is guaranteed that the result will be positive integer number.
512 4
50
1000000000 9
1
水题,不说。
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 5 #define mod 1000000007 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define pb push_back 9 #define pbk pop_back 10 #define ls(i) (i<<1) 11 #define rs(i) (i<<1|1) 12 #define mp make_pair 13 using namespace std; 14 LL n; 15 int k; 16 int main() 17 { 18 scanf("%I64d%d",&n,&k); 19 for(int i=1;i<=k;i++) 20 { 21 if(n%10==0) 22 n/=10; 23 else 24 n--; 25 } 26 printf("%I64d\n",n); 27 return 0; 28 }
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 s 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 n capital Latin letters.
Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times.
7
ABACABA
AB
5
ZZZAA
ZZ
拿个string 和map 存一存就好了。
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 5 #define mod 1000000007 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define pb push_back 9 #define pbk pop_back 10 #define ls(i) (i<<1) 11 #define rs(i) (i<<1|1) 12 #define mp make_pair 13 using namespace std; 14 const int N=1e2+10; 15 map<string,int> st; 16 string str,s; 17 int n,ans; 18 int main() 19 { 20 cin>>n; 21 cin>>s; 22 ios::sync_with_stdio(false); 23 for(int i=0;i<n-1;i++) 24 { 25 str=""; 26 str+=s[i]; 27 str+=s[i+1]; 28 st[str]++; 29 } 30 ans=0; 31 for(auto p:st) 32 { 33 if(p.second>ans) 34 { 35 ans=p.second; 36 str=p.first; 37 } 38 } 39 cout<<str<<endl; 40 }
You are given a sequence of integers of length x.
Note that the sequence can contain equal elements.
If there is no such
The first line of the input contains integer numbers 1≤ai≤109) — the sequence itself.
Print any integer number x.
If there is no such
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]的情况,这样也是无解的。
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 5 #define mod 1000000007 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define pb push_back 9 #define pbk pop_back 10 #define ls(i) (i<<1) 11 #define rs(i) (i<<1|1) 12 #define mp make_pair 13 using namespace std; 14 const int N=2e5+10; 15 int a[N]; 16 int n,m,k,t; 17 int main() 18 { 19 scanf("%d%d",&n,&k); 20 for(int i=1;i<=n;i++) 21 scanf("%d",a+i); 22 sort(a+1,a+n+1); 23 if(k==0) 24 { 25 if(a[1]==1) 26 printf("-1\n"); 27 else 28 printf("1\n"); 29 return 0; 30 } 31 printf("%d\n",(k>n || (k<n && a[k]==a[k+1]))?-1:a[k]); 32 return 0; 33 }
Polycarp likes to play with numbers. He takes some integer number n−1operations of the two kinds:
- divide the number 3);
- multiply the number 2.
After each operation, Polycarp writes down the result on the board and replaces n numbers on the board after all.
You are given a sequence of length n — 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 1≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Print n 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。
反正就暴力链嘛,找到一个链一个,然后找没有被链的那个当头输出就好了。
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 5 #define mod 1000000007 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define pb push_back 9 #define pbk pop_back 10 #define ls(i) (i<<1) 11 #define rs(i) (i<<1|1) 12 #define mp make_pair 13 using namespace std; 14 const int N=1e2+10; 15 LL a[N]; 16 int nexted[N]; 17 int n,hd,vis[N]; 18 int main() 19 { 20 scanf("%d",&n); 21 for(int i=1;i<=n;i++) 22 scanf("%I64d",a+i); 23 for(int i=1;i<=n;i++) 24 for(int j=1;j<=n;j++) 25 if(!vis[j] && a[j]%2==0 && a[i]==a[j]/2) 26 { 27 nexted[i]=j; 28 vis[j]=1; 29 break; 30 } 31 else if(!vis[j] && a[i]%3==0 && a[i]==a[j]*3) 32 { 33 nexted[i]=j; 34 vis[j]=1; 35 break; 36 } 37 for(int i=1;i<=n;i++) 38 if(vis[i]==0) 39 { 40 hd=i; 41 break; 42 } 43 for(int i=1;i<=n;i++) 44 { 45 printf("%I64d ",a[hd]); 46 hd=nexted[hd]; 47 } 48 printf("\n"); 49 return 0; 50 }
然后听说了排序新奇写法,就是把含3因数多的尽可能的往前排,在含3因数一样的情况下小的数排前面。这样因为保证有解,排序出来的结果就是答案。
学了下lambda表达式
#include<bits/stdc++.h> using namespace std; long long n,a[110]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",a+i); sort(a+1,a+n+1,[](long long a,long long b) { int cnt[2]={0,0}; while(a%3==0) a/=3,cnt[0]++; while(b%3==0) b/=3,cnt[1]++; return cnt[0]==cnt[1]?a<b:cnt[0]>cnt[1]; }); for(int i=1;i<=n;i++) printf("%I64d%c",a[i]," \n"[i==n]); return 0; }
You are given an undirected graph consisting of m 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 a). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.
Two vertices v.
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.
The first line contains two integer numbers 0≤m≤2⋅105) — number of vertices and edges.
The following ui,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了。
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 5 #define mod 1000000007 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define pb push_back 9 #define pbk pop_back 10 #define ls(i) (i<<1) 11 #define rs(i) (i<<1|1) 12 #define mp make_pair 13 using namespace std; 14 const int N=2e5+10; 15 struct edg 16 { 17 int next,to; 18 }edge[N<<2]; 19 int head[N],etot,vis[N],ct[N]; 20 int n,m,u,v; 21 int ans; 22 void init() 23 { 24 clr_1(head); 25 clr(vis); 26 clr(ct); 27 etot=0; 28 ans=0; 29 return ; 30 } 31 void addedge(int u,int v) 32 { 33 edge[++etot]=(edg){head[u],v}; 34 head[u]=etot; 35 return ; 36 } 37 bool dfs(int u,int hed,int fa,int dep) 38 { 39 vis[u]=1; 40 if(ct[u]>2) 41 return 0; 42 int p; 43 for(int i=head[u];i!=-1;i=edge[i].next) 44 { 45 p=edge[i].to; 46 if(p==fa) 47 continue; 48 if(vis[p] && p==hed) 49 { 50 if(dep>=2) 51 return 1; 52 else 53 return 0; 54 } 55 if(!vis[p]) 56 return dfs(p,hed,u,dep+1); 57 } 58 return 0; 59 } 60 int main() 61 { 62 scanf("%d%d",&n,&m); 63 init(); 64 for(int i=1;i<=m;i++) 65 { 66 scanf("%d%d",&u,&v); 67 addedge(u,v); 68 addedge(v,u); 69 ct[u]++; 70 ct[v]++; 71 } 72 for(int i=1;i<=n;i++) 73 { 74 if(!vis[i]) 75 if(dfs(i,i,i,0)) 76 ans++; 77 } 78 printf("%d\n",ans); 79 return 0; 80 }
You are given an integer array of length n.
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 k.
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 [1,3] is not.
The first line of the input containing integer number 1≤ai≤109) — the array itself.
On the first line print k — 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的离散化代码能更短点。
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x)) 5 #define mod 1000000007 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define pb push_back 9 #define pbk pop_back 10 #define ls(i) (i<<1) 11 #define rs(i) (i<<1|1) 12 using namespace std; 13 const int N=2e5+10; 14 int a[N],uni[N],n,m,now[N],pre[N],last,maxn,t,p,ans[N],num[N]; 15 int main() 16 { 17 scanf("%d",&n); 18 for(int i=1;i<=n;i++) 19 { 20 scanf("%d",a+i); 21 uni[i]=a[i]; 22 } 23 sort(uni+1,uni+n+1); 24 m=unique(uni+1,uni+n+1)-uni-1; 25 last=maxn=1; 26 for(int i=1;i<=n;i++) 27 { 28 t=lower_bound(uni+1,uni+m+1,a[i])-uni; 29 if(num[t]<1) 30 { 31 now[t]=i; 32 num[t]=1; 33 } 34 p=lower_bound(uni+1,uni+m+1,a[i]-1)-uni; 35 if(p>m || uni[p]!=a[i]-1) 36 continue; 37 if(num[t]<num[p]+1) 38 { 39 now[t]=i; 40 num[t]=num[p]+1; 41 pre[i]=now[p]; 42 } 43 if(num[t]>maxn) 44 { 45 last=i; 46 maxn=num[t]; 47 } 48 } 49 printf("%d\n",maxn); 50 for(int i=maxn;i>=1;i--) 51 { 52 ans[i]=last; 53 last=pre[last]; 54 } 55 for(int i=1;i<=maxn;i++) 56 printf("%d ",ans[i]); 57 printf("\n"); 58 return 0; 59 }