「CF263X」Codeforces Round #161 (Div. 2)
A.Beautiful Matrix
模拟,求到中点的曼哈顿距离
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int x; int main() { for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) { int x=read(); if(x)printf("%d\n",abs(3-i)+abs(3-j)); } return 0; } |
B. Squares
排序一下判断即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int n,K; int a[105]; int main() { n=read();K=read(); for(int i=1;i<=n;i++)a[i]=read(); sort(a+1,a+n+1,greater<int>()); bool flag=0; if(K<=n) { if(a[K+1]!=a[K]) { printf("%d 0\n",a[K]); flag=1; } } if(!flag)puts("-1"); return 0; } |
C. Circle of Numbers
如果一个点与俩个点都有连边,则它在这两个点的一侧
所以dfs依次确定一下即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } bool flag; int n,m; vector<int>e[100005]; int ans[100005]; bool vis[100005]; bool dfs(int x,int f,int K) { vis[x]=1;ans[K]=x; if(K==n)return 1; if(f==-1) { for(int i=0;i<e[x].size();i++) if(!vis[e[x][i]]) if(dfs(e[x][i],x,K+1))return 1; } else { for(int i=0;i<e[x].size();i++) if(e[x][i]!=f) { int v=e[x][i],t1=0,t2=0; for(int j=0;j<e[v].size();j++) t1|=(e[v][j]==x),t2|=(e[v][j]==f); if(t1&&t2&&!vis[v]) if(dfs(v,x,K+1))return 1; } } vis[x]=0; return 0; } int main() { n=read();m=n*2; for(int i=1;i<=m;i++) { int u=read(),v=read(); e[u].push_back(v); e[v].push_back(u); } for(int i=1;i<=n;i++) if(e[i].size()!=4) { puts("-1"); return 0; } if(!dfs(1,-1,1)) { puts("-1"); return 0; } for(int i=1;i<=n;i++) printf("%d ",ans[i]); return 0; } |
D.Cycle in Graph
感受了一下,觉得随便从一个点开始深搜即可。。。找出过这个点的所有环判断一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } bool flag; int n,m,K; vector<int>e[100005],ans; int vis[100005]; void dfs(int x) { if(flag)return; ans.push_back(x); vis[x]=ans.size(); for(int i=0;i<e[x].size();i++) if(vis[e[x][i]]) { if(ans.size()-vis[e[x][i]]+1>K&&!flag) { printf("%lu\n",ans.size()-vis[e[x][i]]+1); for(int j=vis[e[x][i]]-1;j<ans.size();j++) printf("%d ",ans[j]); flag=1; } } else dfs(e[x][i]); ans.pop_back(); vis[x]=0; } int main() { n=read();m=read();K=read(); for(int i=1;i<=m;i++) { int u=read(),v=read(); e[u].push_back(v); e[v].push_back(u); } dfs(1); return 0; } |
E. Rhombus
其实是个模拟题。?QAQ
用二维前缀和优化下暴力,其实还是个暴力
确定x,y,把系数写出来,发现是若干个矩形叠加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int n,m,K,a1,a2; ll mx,a[1005][1005],s[1005][1005]; ll get(int x1,int y1,int x2,int y2) { return s[x2][y2]+s[x1-1][y1-1]-s[x2][y1-1]-s[x1-1][y2]; } void solve() { for(int i=K;i+K<=n+1;i++) for(int j=K;j+K<=m+1;j++) { ll tmp=0; for(int k=1;k<=K;k++) tmp+=get(i-K+k,j-k+1,i+K-k,j+k-1); if(tmp>=mx)mx=tmp,a1=i,a2=j; } } int main() { n=read();m=read();K=read(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=read(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]+=a[i-1][j]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) s[i][j]=s[i][j-1]+a[i][j]; solve(); printf("%d %d\n",a1,a2); return 0; } |
Subscribe