Codeforces Round #479 (Div. 3)

手速场2333,这群人贼牛逼!手速贼快!
 
A. Wrong Subtraction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

Input

The first line of the input contains two integer numbers nn and kk (2n1092≤n≤109, 1k501≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.

Output

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.

Examples
input
Copy
512 4
output
Copy
50
input
Copy
1000000000 9
output
Copy
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 }
View Code
B. Two-gram
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

Input

The first line of the input contains integer number nn (2n1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.

Output

Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string sas a substring (i.e. two consecutive characters of the string) maximal number of times.

Examples
input
Copy
7
ABACABA
output
Copy
AB
input
Copy
5
ZZZAA
output
Copy
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 }
View Code
C. Less or Equal
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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. 1x1091≤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).

Input

The first line of the input contains integer numbers nn and kk (1n21051≤n≤2⋅105, 0kn0≤k≤n). The second line of the input contains nninteger numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the sequence itself.

Output

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).

Examples
input
Copy
7 4
3 7 5 1 10 3 20
output
Copy
6
input
Copy
7 2
3 7 5 1 10 3 20
output
Copy
-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 }
View Code
D. Divide by three, multiply by two
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 n1n−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.

Input

The first line of the input contatins an integer number nn (2n1002≤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 (1ai310181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

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.

Examples
input
Copy
6
4 8 6 3 12 9
output
Copy
9 3 6 12 4 8 
input
Copy
4
42 28 84 126
output
Copy
126 42 84 28 
input
Copy
2
1000000000000000000 3000000000000000000
output
Copy
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 }
View Code

然后听说了排序新奇写法,就是把含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;
}
View Code
E. Cyclic Components
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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].
Input

The first line contains two integer numbers nn and mm (1n21051≤n≤2⋅105, 0m21050≤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 (1vi,uin1≤vi,ui≤n, uiviui≠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.

Output

Print one integer — the number of connected components which are also cycles.

Examples
input
Copy
5 4
1 2
3 4
5 4
3 5
output
Copy
1
input
Copy
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
output
Copy
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 }
View Code
F. Consecutive Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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+k1][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.

Input

The first line of the input containing integer number nn (1n21051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the array itself.

Output

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.

Examples
input
Copy
7
3 3 4 7 5 6 8
output
Copy
4
2 3 5 6
input
Copy
6
1 3 5 2 4 6
output
Copy
2
1 4
input
Copy
4
10 9 8 7
output
Copy
1
1
input
Copy
9
6 7 8 3 4 5 9 10 11
output
Copy
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 }
View Code

猜你喜欢

转载自www.cnblogs.com/wujiechao/p/9005190.html