标签:algo alt view code amp first 子矩阵 oid log
T1珠心算——暴力模拟
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } int n,a[101],ans=0; int main() { n=read(); for(int i=1;i<=n;i++)a[i]=read(); for(int i=1;i<=n;i++){ int t=0; for(int j=1;j<n;j++){ for(int k=j+1;k<=n;k++){ if(a[i]==a[j]+a[k]) t=1; } } if(t)ans++; } printf("%d\n",ans); return 0; }
T2比例简化——暴力+1
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int main() { pair<int,int> q; double a,b;int l; double cha,num,xiao=99999; cin>>a>>b>>l; num=a/b; for(double i=1;i<=l;i=i+1){ for(double j=1;j<=l;j=j+1){ cha=i/j; if(xiao>cha-num&&cha>=num) {q.first=int(i);q.second=int(j);xiao=cha-num;} } } cout<<q.first<<" "<<q.second; return 0; }
T3螺旋矩阵——机智的模拟
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } int n,a,b,ans=0; int main() { n=read();a=read();b=read(); int x=1,y=n; while(a>x&&b>x&&a<y&&b<y){ ans+=4*(n-1); x++;y--;n-=2; } if(a==x)ans+=b-x+1; else if(a==y)ans+=2*(n-1)+y-b+1; else if(b==x)ans+=3*(n-1)+y-a+1; else ans+=n-1+a-x+1; printf("%d\n",ans); return 0; }
T4子矩阵——DFS+DP
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } inline int min(int a,int b){return a<b?a:b;} inline int abs(int a){return a>0?a:-a;} int n,m,r,c,ans=0x7fffffff; int a[18][18],f[18][18]; int w[18],v[18][18],rec[18]; void dp(){ memset(f,127,sizeof(f)); memset(w,0,sizeof(w)); memset(v,0,sizeof(v)); for(int i=1;i<=m;i++) for(int j=1;j<r;j++) w[i]+=abs(a[rec[j]][i]-a[rec[j+1]][i]); for(int i=1;i<=m;i++) for(int j=i+1;j<=m;j++) for(int k=1;k<=r;k++) v[i][j]+=abs(a[rec[k]][i]-a[rec[k]][j]); f[0][0]=0; for(int i=1;i<=c;i++) for(int j=i;j<=m;j++) for(int k=0;k<j;k++) f[i][j]=min(f[i-1][k]+w[j]+v[k][j],f[i][j]); for(int i=c;i<=m;i++) ans=min(ans,f[c][i]); } void dfs(int x,int y){ if(y>r){dp();return;} if(x>n)return; for(int i=x;i<=n;i++){ rec[y]=i;dfs(i+1,y+1); } } int main() { n=read();m=read();r=read();c=read(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=read(); dfs(1,1);printf("%d\n",ans); return 0; }
本文由Yzyet编写,网址为www.cnblogs.com/Yzyet。非Yzyet同意,禁止转载,侵权者必究。
标签:algo alt view code amp first 子矩阵 oid log
原文地址:http://www.cnblogs.com/Yzyet/p/7398046.html