Codeforces Round #479 (Div. 3)
还是( m Div.3)比较娱乐身心...虽然我因为罚时名次比较低...
A. Wrong Subtraction
模拟。
#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('
');}
#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
继续模拟...
#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('
');}
#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
奇怪的判判判...( m WA)了好几次,总之( m sort)就行了。
#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('
');}
#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
因为保证有解,(O(n^2))枚举之后建图爆搜就行了。
#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('
');}
#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
爆搜题,环的性质满足每个点度数为(2),复杂度(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('
');}
#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
( m dp),考虑一个暴力的玩意,设(f[i][j])表示当前(i)位结尾为(j)的最大长度。
第一维可以滚动,注意到每个数只能影响到一个状态,所以总状态很少,可以开( m map)存第二维。
方案直接记录最大的那个以什么结尾,倒着扫一遍就好了。
#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('
');}
#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;
}