标签:style blog color os io ar for 数据 div
没参加,过了好久才补齐:
第一题:好贱的题啊,题意描述不清,水又水得很。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 using namespace std; 5 int n,s,w[101]; 6 int main(){ 7 scanf("%d%d",&n,&s); 8 s=s*100; 9 int x1,y1,q=1; 10 for (int i=1;i<=n;i++){ 11 scanf("%d%d",&x1,&y1); 12 w[i]=x1*100+y1; 13 if (w[i]<=s) q=0; 14 } 15 if (q) {printf("-1\n");return 0;} 16 int ans=0; 17 for (int i=1;i<=n;i++){ 18 if (w[i]>s) continue; 19 if ((s-w[i])%100>ans) ans=(s-w[i])%100; 20 } 21 printf("%d\n",ans); 22 return 0; 23 }
第二题:贪心
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 using namespace std; 5 int n; 6 int h[100010],energy,ans; 7 int main(){ 8 scanf("%d",&n); 9 energy=0; 10 for (int i=1;i<=n;i++) 11 scanf("%d",&h[i]); 12 h[0]=0; 13 for (int i=1;i<=n;i++){ 14 if (energy+h[i-1]<h[i]) {ans+=h[i]-energy-h[i-1];energy=0;} 15 else energy-=(h[i]-h[i-1]); 16 } 17 printf("%d\n",ans); 18 return 0; 19 }
第三题:还是很水啊,记得用long long
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 int n,x1,y1,x2,y2; 5 long long a[2001][2001],ans1,ans2; 6 long long l[6001],R[6001],*r; 7 int main(){ 8 r=R+3001; 9 ans1=-1;ans2=-1; 10 scanf("%d",&n); 11 memset(l,0,sizeof(l)); 12 memset(R,0,sizeof(R)); 13 for (int i=1;i<=n;i++) 14 for (int j=1;j<=n;j++){ 15 scanf("%I64d",&a[i][j]); 16 l[i+j]+=a[i][j]; 17 r[i-j]+=a[i][j]; 18 } 19 for (int i=1;i<=n;i++) 20 for (int j=1;j<=n;j++) 21 if ((i+j)%2==0) { 22 if (l[i+j]+r[i-j]-a[i][j]>ans1) {ans1=l[i+j]+r[i-j]-a[i][j];x1=i;y1=j;} 23 }else { 24 if (l[i+j]+r[i-j]-a[i][j]>ans2) {ans2=l[i+j]+r[i-j]-a[i][j];x2=i;y2=j;} 25 } 26 printf("%I64d\n",(long long )(ans1+ans2)); 27 printf("%d %d %d %d\n",x1,y1,x2,y2); 28 return 0; 29 }
第四题:多排列的lcs,动态规划,以其中一个排列为基准,用f[i]表示第一个排列中以i为结尾的最长公共序列,
f[i]=max{f[j]+1} (j<i,且每个排列中a[i],a[j]这两个数对应的顺序相同)最后得出最大的f[i]即可。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 using namespace std; 5 int a[7][1001]; 6 int pos[7][1001]; 7 int f[1001]; 8 int main(){ 9 int n,T; 10 scanf("%d%d",&n,&T); 11 memset(f,0,sizeof(f)); 12 for (int i=1;i<=T;i++){ 13 for (int j=1;j<=n;j++){ 14 scanf("%d",&a[i][j]); 15 pos[i][a[i][j]]=j; 16 } 17 } 18 for (int i=1;i<=n;i++){ 19 int Max=0; 20 for (int j=1;j<i;j++){ 21 bool flag=1; 22 for (int k=1;k<=T;k++){ 23 if (pos[k][a[1][j]]>pos[k][a[1][i]]) flag=0; 24 } 25 if (flag) if (f[j]>Max) Max=f[j]; 26 } 27 f[i]=Max+1; 28 } 29 int ans=0; 30 for (int i=1;i<=n;i++) if (f[i]>ans) ans=f[i]; 31 printf("%d\n",ans); 32 return 0; 33 }
第五题:我只会暴力,看了题解之后发现我按照题解的方法写的比暴力跑得还慢,想想算了,数据比较弱,树的深度很小,所以暴力能过。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <vector> 5 #include <cctype> 6 #define pb push_back 7 using namespace std; 8 int value[100001],fat[100001]; 9 int n,q; 10 vector <int> Ed[100001]; 11 void dfs(int node,int father){ 12 fat[node]=father; 13 for (int i=0;i<Ed[node].size();i++) 14 if (Ed[node][i]!=father) dfs(Ed[node][i],node); 15 } 16 inline int readint(){ 17 char ch;int ret=0; 18 ch=getchar(); 19 while (!isdigit(ch)) ch=getchar(); 20 while (isdigit(ch)) { 21 ret=ret*10+ch-‘0‘; 22 ch=getchar(); 23 } 24 return ret; 25 } 26 inline int gcd(int a,int b){ 27 int t; 28 while (b!=0){ 29 t=a;a=b;b=t%b; 30 } 31 return a; 32 } 33 int main(){ 34 n=readint();q=readint(); 35 for (int i=1;i<=n;i++) 36 value[i]=readint(); 37 int v1,v2; 38 for (int i=1;i<=n-1;i++){ 39 v1=readint();v2=readint(); 40 Ed[v1].pb(v2); 41 Ed[v2].pb(v1); 42 } 43 dfs(1,-1); 44 int kind,x,p,flag; 45 while (q--){ 46 kind=readint(); 47 if (kind==1) { 48 x=readint();p=value[x];flag=1; 49 while (fat[x]!=-1) { 50 x=fat[x]; 51 if (gcd(value[x],p)>1) {printf("%d\n",x);flag=0;break;} 52 } 53 if (flag) printf("-1\n"); 54 }else { 55 x=readint();value[x]=readint(); 56 } 57 } 58 return 0; 59 }
标解的做法以后再写一遍看看吧。
标签:style blog color os io ar for 数据 div
原文地址:http://www.cnblogs.com/yzh119/p/3958660.html