Codeforces Round #479 (Div. 3) Solution
Or \ (\ rm Div.3 \) compare the physical and mental recreation time penalty ... although I was relatively low ranking ...
A. Wrong Subtraction
simulation.
#include<bits/stdc++.h>
using namespace std;
#define int long long
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int n,a[maxn],k;
signed main() {
read(n),read(k);
for(int i=1;i<=k;i++)
if(n%10==0) n/=10;
else n--;
write(n);
return 0;
}
B. Two-gram
Continue simulation ...
#include<bits/stdc++.h>
using namespace std;
#define int long long
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int n,a[maxn],k;
char s[maxn];
signed main() {
read(n);scanf("%s",s+1);int mx=0,p=0;
for(int i=1,t;i<n;i++) {
a[t=s[i]*100+s[i+1]]++;
if(a[t]>mx) mx=a[t],p=t;
}
putchar(p/100),putchar(p%100);
return 0;
}
C. Less or Equal
Strange sentence sentence sentence ... \ (\ RM WA \) several times, in short, \ (\ rm sort \) on the line.
#include<bits/stdc++.h>
using namespace std;
#define int long long
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int n,a[maxn],k;
signed main() {
read(n),read(k);FOR(i,1,n) read(a[i]);
sort(a+1,a+n+1);
if(k==0) puts(a[1]==1?"-1":"1");
else if(a[k+1]==a[k]) puts("-1");
else write(a[k]);
return 0;
}
D. Divide by three, multiply by two
Because guarantee solvable, \ (O (the n-^ 2) \) map building after the explosion found enumeration on the line.
#include<bits/stdc++.h>
using namespace std;
#define int long long
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 1e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int n,a[maxn],head[maxn],tot,d[maxn],sta[maxn],top,vis[maxn],out[maxn];
struct edge{int to,nxt;}e[maxn<<1];
void ins(int u,int v) {e[++tot]=(edge){v,head[u]},head[u]=tot,d[v]++,out[u]++;}
void dfs(int x) {
sta[++top]=x;
if(!out[x]) {
for(int i=1;i<=top;i++) printf("%I64d ",a[sta[i]]);puts("");
exit(0);
}
for(int i=head[x];i;i=e[i].nxt) dfs(e[i].to);
top--;
}
signed main() {
read(n);FOR(i,1,n) read(a[i]);
FOR(i,1,n) FOR(j,1,n) if(i!=j) {
if(a[i]*2==a[j]) ins(i,j);
else if(a[j]==a[i]/3&&a[i]%3==0) ins(i,j);
}
FOR(i,1,n) if(!d[i]) dfs(i);
return 0;
}
E. Cyclic Components
Title search explosive nature, ring satisfies each point of degree \ (2 \) , the complexity of the \ (O (n-m +) \) .
#include<bits/stdc++.h>
using namespace std;
#define int long long
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 5e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int n,a[maxn],head[maxn],tot,cnt,vis[maxn],sz,m,ans,d[maxn],bo=1;
struct edge{int to,nxt;}e[maxn];
void ins(int u,int v) {d[u]++;e[++tot]=(edge){v,head[u]},head[u]=tot;}
void dfs(int x) {
vis[x]=1;if(d[x]!=2) bo=0;
for(int i=head[x];i;i=e[i].nxt)
if(!vis[e[i].to]) dfs(e[i].to);
}
signed main() {
read(n),read(m);
for(int i=1,x,y;i<=m;i++) read(x),read(y),ins(x,y),ins(y,x);
for(int i=1;i<=n;i++) if(!vis[i]) dfs(i),ans+=bo,bo=1;
write(ans);
return 0;
}
F. Consecutive Subsequence
\ (\ RM DP \) , consider a violent thing, provided \ (f [i] [j ] \) represents the current \ (I \) bits ending \ (J \) maximum length.
The first dimension can scroll, notice that each number can only affect a state, so the total state little, you can open \ (\ rm map \) keep the second dimension.
The largest program recorded directly to what the end, swept backwards again just fine.
#include<bits/stdc++.h>
using namespace std;
#define int long long
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 5e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int n,a[maxn],ans,p,sta[maxn],top;
map<int,int > f;
signed main() {
read(n);FOR(i,1,n) read(a[i]);
for(int i=1,x;i<=n;i++) {
x=f[a[i]]=max(f[a[i]-1]+1,f[a[i]]);
if(x>ans) ans=x,p=a[i];
}
write(ans);
for(int i=n;i;i--)
if(a[i]==p) sta[++top]=i,p--;
while(top) printf("%I64d ",sta[top--]);puts("");
return 0;
}