标签:dep string double 素数环 ring target 深搜 int monkey
虽然AK了但是手速不够快而且罚时爆炸QAQ....
找素数环,经典深搜问题,要注意找完排列再判断素数为超时,正确的做法是边搜边判断,注意输出格式==
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <queue> 7 #include <map> 8 #define ll long long 9 #define out(a) printf("%d ",a) 10 #define writeln printf("\n") 11 const int N=1e5+50; 12 using namespace std; 13 int n; 14 int a[N]; 15 int tot=0,cnt=1; 16 bool flag,vis[N]; 17 int read() 18 { 19 int s=0,t=1; char c; 20 while (c<‘0‘||c>‘9‘){if (c==‘-‘) t=-1; c=getchar();} 21 while (c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘; c=getchar();} 22 return s*t; 23 } 24 ll readl() 25 { 26 int s=0,t=1; char c; 27 while (c<‘0‘||c>‘9‘){if (c==‘-‘) t=-1; c=getchar();} 28 while (c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘; c=getchar();} 29 return s*t; 30 } 31 bool check(int x,int y) 32 { 33 int sum=x+y; 34 for (int i=2;i*i<=sum;i++) 35 if (sum%i==0) return false; 36 return true; 37 } 38 void dfs(int dep) 39 { 40 if (dep==n&&check(a[n],a[1])){ 41 for (int i=1;i<=n;i++) 42 if (i!=n) out(a[i]); 43 else printf("%d",a[i]); 44 writeln; 45 } 46 for (int i=1;i<=n;i++){ 47 if (!vis[i]&&check(a[cnt],i)){ 48 a[++cnt]=i; 49 vis[i]=true; 50 dfs(dep+1); 51 cnt--; 52 vis[i]=false; 53 } 54 } 55 } 56 int main() 57 { 58 while (~scanf("%d",&n)){ 59 printf("Case %d:\n",++tot); 60 a[1]=1; vis[1]=true; 61 dfs(1); writeln; 62 } 63 return 0; 64 }
n个长方体,x,y,z可以作为长方体任意的长宽高,现在要你堆长方体,要求上面的长方体的长和宽严格小于下面的,求最大高度.
显然,长和宽是单调上升的,很容易想到排序后求LIS,由于求的是高度,可以把高度看作价值n2求一波LIS.
要注意的是长方体有6种放置方式,dp数组初值要设为第i个长方体高度(因为这个一直卡在这题...)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <queue> 7 #include <map> 8 #define ll long long 9 #define out(a) printf("%d ",a) 10 #define writeln printf("\n") 11 const int N=1e5+50; 12 using namespace std; 13 int n,x,y,z,tot,cnt=0; 14 int ans; 15 int f[N]; 16 struct node 17 { 18 int x,y,z,f; 19 }a[N]; 20 int read() 21 { 22 int s=0,t=1; char c; 23 while (c<‘0‘||c>‘9‘){if (c==‘-‘) t=-1; c=getchar();} 24 while (c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘; c=getchar();} 25 return s*t; 26 } 27 ll readl() 28 { 29 int s=0,t=1; char c; 30 while (c<‘0‘||c>‘9‘){if (c==‘-‘) t=-1; c=getchar();} 31 while (c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘; c=getchar();} 32 return s*t; 33 } 34 bool cmp(node a,node b) 35 { 36 return a.x==b.x?a.y<b.y:a.x<b.x; 37 } 38 int main() 39 { 40 while (~scanf("%d",&n)!=0){ 41 if (n==0) break; 42 ans=-233333; tot=0; 43 memset(f,0,sizeof(f)); 44 memset(a,0,sizeof(a)); 45 for (int i=1;i<=n;i++){ 46 x=read(),y=read(),z=read(); 47 a[++tot].x=x,a[tot].y=y,a[tot].z=a[tot].f=z; 48 a[++tot].x=x,a[tot].y=z,a[tot].z=a[tot].f=y; 49 a[++tot].x=y,a[tot].y=x,a[tot].z=a[tot].f=z; 50 a[++tot].x=y,a[tot].y=z,a[tot].z=a[tot].f=x; 51 a[++tot].x=z,a[tot].y=x,a[tot].z=a[tot].f=y; 52 a[++tot].x=z,a[tot].y=y,a[tot].z=a[tot].f=x; 53 } 54 sort(a+1,a+tot+1,cmp); 55 for (int i=2;i<=tot;i++){ 56 for (int j=1;j<i;j++) 57 if (a[i].x>a[j].x&&a[i].y>a[j].y) a[i].f=max(a[i].f,a[j].f+a[i].z); 58 ans=max(ans,a[i].f); 59 } 60 printf("Case %d: maximum height = %d",++cnt,ans); writeln; 61 } 62 return 0; 63 } 64
C.最大报销额
题意就懒得说了,大概就是做一个带有小数的01背包。
把小数乘上100然后xjb写就可以了,要注意的是强制转换如:(double)x,当x为一个式子时最好加上括号(double)(x).
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <queue> 7 #include <map> 8 #define ll long long 9 #define out(a) printf("%d ",a) 10 #define writeln printf("\n") 11 const int N=3e6+50; 12 using namespace std; 13 double Q,suma,sumb,sumc,v; 14 int n,maxn,m,cnt=0; 15 int f[N],num[31]; 16 bool flag; 17 char c; 18 int read() 19 { 20 int s=0,t=1; char c; 21 while (c<‘0‘||c>‘9‘){if (c==‘-‘) t=-1; c=getchar();} 22 while (c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘; c=getchar();} 23 return s*t; 24 } 25 ll readl() 26 { 27 int s=0,t=1; char c; 28 while (c<‘0‘||c>‘9‘){if (c==‘-‘) t=-1; c=getchar();} 29 while (c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘; c=getchar();} 30 return s*t; 31 } 32 bool check(int a,int b,int c) 33 { 34 int sum=a+b+c; 35 if (a<=600&&b<=600&&c<=600&&sum<=1000) return true; 36 return false; 37 } 38 int main() 39 { 40 while (~scanf("%lf%d",&Q,&n)){ 41 if (n==0) break; 42 maxn=(int)(Q*100); cnt=0; 43 memset(f,0,sizeof(f)); 44 for (int i=1;i<=n;i++){ 45 m=read(); flag=false; 46 suma=sumb=sumc=0; 47 for (int j=1;j<=m;j++){ 48 scanf(" %c:%lf",&c,&v); 49 if (c==‘A‘) suma+=v; 50 else if (c==‘B‘) sumb+=v; 51 else if (c==‘C‘) sumc+=v; 52 else flag=true; 53 } 54 if (check(suma,sumb,sumc)&&!flag) num[++cnt]=(int)((suma+sumb+sumc)*100); 55 } 56 for (int i=1;i<=cnt;i++) 57 for (int j=maxn;j>=num[i];j--) 58 f[j]=max(f[j],f[j-num[i]]+num[i]); 59 printf("%.2lf",f[maxn]/100.0); writeln; 60 } 61 return 0; 62 } 63
标签:dep string double 素数环 ring target 深搜 int monkey
原文地址:https://www.cnblogs.com/Kaleidoscope233/p/9403798.html