GDUT finals and gdut finals

Source: Internet
Author: User

GDUT finals and gdut finals

In the finals, I felt that the question was more difficult, but my mentality was better. This is because the effect is better when it is easy. Just like yesterday's point of view, what can be 1A is regarded as doing well.


A. The number is small. The key is to understand the question (I cannot even understand what the input and output are .... Then the Administrator drops the question .... Criticism and criticism ...) Bin God's code worships, knowing that it is State compression. The question says a maximum of six, either 1 <6 enumeration, or 6! Enumeration. Code:

/* ***********************************************Author        :kuangbinCreated Time  :2015/3/15 14:17:30File Name     :GDUT\A.cpp************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;const int INF = 0x3f3f3f3f;int dp[1000];int st[6410],cost[6410];int num[6410];int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);int cnt = 0;for(int i = 0;i < n;i++){int m;int nn;int p;scanf("%d%d",&m,&nn);while(m--){int k;scanf("%d",&k);st[cnt] = 0;num[cnt] = nn;while(k--){scanf("%d",&p);st[cnt] |= (1<<p);}scanf("%d",&cost[cnt]);cnt++;}}for(int i = 0;i < (1<<6);i++)dp[i] = INF;dp[0] = 0;for(int i = 0;i < (1<<6);i++){if(dp[i] == INF)continue;for(int j = 0;j < cnt;j++){if( (i&(1<<num[j])) != 0)continue;if( (i|st[j]) != i )continue;dp[i|(1<<num[j])] = min(dp[i|(1<<num[j])],dp[i]+cost[j]);}}int tot = (1<<6)-1;if(dp[tot] == INF)dp[tot] = -1;printf("%d\n",dp[tot]);}    return 0;}

B. interval DP. Because 0 or 1 is inserted anywhere, the unit length is obviously ans 0, and the two lengths are used as initialization judgment, and then the interval DP

Code:

/* ***********************************************Author        :kuangbinCreated Time  :2015/3/15 13:05:55File Name     :GDUT\B.cpp************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;char str[1010];int a[1010];int dp[1010][1010];int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T;int n;scanf("%d",&T);while(T--){scanf("%d",&n);scanf("%s",str);for(int i = 0;i < n;i++)a[i] = str[i]-'0';memset(dp,0,sizeof(dp));for(int i = 0;i < n;i++)dp[i][i] = 1;for(int i = n-1;i >= 0;i--)for(int j = i+1;j < n;j++){dp[i][j] = j-i+1;if(a[i] != a[j])dp[i][j] = min(dp[i][j],dp[i+1][j-1]);dp[i][j] = min(dp[i][j],dp[i+1][j]+1);dp[i][j] = min(dp[i][j],dp[i][j-1]+1);}printf("%d\n",dp[0][n-1]);}    return 0;}

In addition, there is also a good idea, according to the meaning of the question, reverse, flip, then repeated do not need to add 0 or 1, convert to the maximum number of repeated, therefore, you do not need to write the code for converting to LCS (Longest Common Sequence). The O (n ^ 2) algorithm is n = 1000.


C. There is actually no problem with this question, but everyone will be stuck in a misunderstanding in their own lives. How can I change the course if I get half of it? The first group of data in this question is a very classic primary school competition question: Two pots, three cakes to be prepared, each of which takes 2 minutes to complete. Can you do it in at least a few minutes? The answer is 3. The process is as follows: 1st and 2 cakes are made in the first minute; 2nd and 3 cakes are made in the second minute; and 1 and 3 cakes are made in the third minute.

So it is obvious that the greedy idea is used. Divide all the cakes into different pots as much as possible. This is the upper integer of the average value. However, if a dish cannot be divided into multiple portions, the maximum value must be calculated. Therefore, the result is that the upper integer of the average value is greater than the upper integer of the maximum value. But the code is quite understandable.

#include <iostream>#include <algorithm>#include <stdio.h>#include <math.h>#include <map>#include <set>#include <vector>#include <string>#include <cstring>#include <sstream>#include <queue>#include <stack>using namespace std;#define input freopen("input.txt","r",stdin)#define output freopen("output.txt","w",stdout)#define For1(i,a,b) for (i=a;i<b;i++)#define For2(i,a,b) for (i=a;i<=b;i++)#define Fill(x,a) memset(x,a,sizeof(x))#define inf 99999999#define pi 3.1415926535897932384626433832795028841971int main(){int t,n,m;int Max,aver,num;scanf("%d",&t);while(t--){aver=Max=0;scanf("%d%d",&n,&m);while(n--){scanf("%d",&num);Max=max(Max,num);aver+=num;}aver=(aver+m-1)/m;printf("%d\n",max(aver,Max));}return 0;}

D. math problems.

If GCD (a, B) = n LCM (a, B) = m then m/n is the product of the mutual quality factor of a B, that is, the logarithm of the numbers from 1 to m/n. Sqrt (n) Complexity, obviously acceptable
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<set>#include<iostream>using namespace std;typedef long long LL;LL n, m;LL Gcd(LL x, LL y){if (!y) return x;elsereturn Gcd(y, x%y);}LL solve(LL n){LL sum = 0;LL i, j;for (i = 1; i <= (double)sqrt(n*1.0); i++){if (n%i==0){j = n / i;if (Gcd(i, j) == 1) ++sum;}}return sum;}int main(){int T, i, Case = 0, j, k;//freopen("data.txt", "r", stdin);scanf("%d", &T);while (T--){scanf("%lld%lld", &n, &m);if (m%n){printf("0\n"); continue;}LL temp = m / n;printf("%lld\n", solve(temp));}return 0;}


Question 5. And query set basics. I did this for five times... The reason is that you are too confident. I used fa [I] instead of getf [I] for my dad. Crying .... Do not submit any details .. You can perform other questions to calm down, or write a few test data to check. A lot of fines in 20 minutes.
#include <iostream>#include <algorithm>#include <stdio.h>#include <math.h>#include <map>#include <set>#include <vector>#include <string>#include <cstring>#include <sstream>#include <queue>#include <stack>using namespace std;#define input freopen("input.txt","r",stdin)#define output freopen("output.txt","w",stdout)#define For1(i,a,b) for (i=a;i<b;i++)#define For2(i,a,b) for (i=a;i<=b;i++)#define Fill(x,a) memset(x,a,sizeof(x))#define inf 99999999#define pi 3.1415926535897932384626433832795028841971const long long maxn=1000050;int t;long long n,m;long long fa[maxn];void init(){for(long long i=0;i<=n;i++) fa[i]=i;}long long getf(long long x){if (fa[x]==x) return x;return fa[x]=getf(fa[x]);}int main(){input;long long i,j,k;scanf("%d",&t);while(t--){scanf("%lld%lld",&n,&m);init();while(m--){scanf("%lld",&i);scanf("%lld",&j);long long fi=getf(i);long long fj=getf(j);if (fi!=fj) fa[fi]=fj;}k=0;long long f1=getf(1);for(i=2;i<=n;i++){int fi=getf(i);if (fi!=f1){fa[fi]=f1;k++;}}printf("%lld\n",k);}return 0;}


F. simulate. This question is not difficult. Follow the meaning of the question. You can do whatever you want. Judge how n and m are less than 100 cycles.

#include <iostream>#include <algorithm>#include <stdio.h>#include <math.h>#include <map>#include <set>#include <vector>#include <string>#include <cstring>#include <sstream>#include <queue>#include <stack>using namespace std;#define input freopen("input.txt","r",stdin)#define output freopen("output.txt","w",stdout)#define For1(i,a,b) for (i=a;i<b;i++)#define For2(i,a,b) for (i=a;i<=b;i++)#define Fill(x,a) memset(x,a,sizeof(x))#define inf 99999999#define pi 3.1415926535897932384626433832795028841971int vis[200];int num[200];int main(){//input;int t;int n,m;int i,j,k;scanf("%d",&t);while(t--){memset(vis,0,sizeof(vis));scanf("%d%d",&n,&m);for(i=1;i<=m;i++) scanf("%d",&num[i]);for(i=1;i<=m;i++)for(j=1;j<=n;j++){if (vis[j]) continue;if (j>=num[i]) vis[j]=num[i];}for(i=1;i<=n;i++) printf("%d%c",vis[i],i==n?'\n':' ');}return 0;}

The key issue is that each lamp is turned off at most once, which means that if you have visited this point before, you will not be able to ignore it regardless of the size.


G. math problems. In the number of (1 + n) * n/2, the number of n is divided by 3.
Don't worry if you see the big n, it must be regular. You certainly cannot create tables. It must be the conclusion of the small data guess process.
Or write this series: 1 3 6 10 15 21 28 36 45 55 66 78 ......
Have you seen the rule? Each group contains three numbers. The first in each group is not a multiple of 3, and the second and third are multiples of 3. Write the program by yourself.

H. DP. What I'm most happy about is that my thinking is the same as that of bin. It means some questions can be done. Don't scare yourself, for example, BC33's 3rd question, I think it is status compression .. After the game, it was a complicated backpack .. (Far away ..)
Definition: dp [I] [j] [k] indicates that the first finger is at I, and the second finger is at j. Currently, the shortest path of k cells has been completed. 0 <= I <= 4, 0 <= j <= 4, 1 <= k <= n. According to the data in the question, n is up to 10000. With peace of mind.
When the initial value is-1, it means that the current situation cannot be obtained (a common idea is to Mark-1 as inaccessible and mark the shortest path with positive numbers). What about the initial situation, dp [0] [0] [0] = 0

The concept of recursion is also very simple: if the new node has not been (the value is-1), go to it; if you have been there, take a small value.
The Code is as follows:

#include <iostream>#include <algorithm>#include <stdio.h>#include <math.h>#include <map>#include <set>#include <vector>#include <string>#include <cstring>#include <sstream>#include <queue>#include <stack>using namespace std;#define input freopen("input.txt","r",stdin)#define output freopen("output.txt","w",stdout)#define For1(i,a,b) for (i=a;i<b;i++)#define For2(i,a,b) for (i=a;i<=b;i++)#define Fill(x,a) memset(x,a,sizeof(x))#define inf 99999999#define pi 3.1415926535897932384626433832795028841971int dp[10][10][10050];int t,n;int num[10050];int main(){int i,j,k,dis;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++) scanf("%d",&num[i]);memset(dp,-1,sizeof(dp));dp[0][0][0]=0;for(k=1;k<=n;k++)for(i=0;i<=4;i++)for(j=0;j<=4;j++){if (dp[i][j][k-1]==-1) continue;dis=abs(i-num[k]);if (dp[num[k]][j][k]==-1) dp[num[k]][j][k]=dp[i][j][k-1]+dis;else dp[num[k]][j][k]=min(dp[num[k]][j][k],dp[i][j][k-1]+dis);dis=abs(j-num[k]);if (dp[i][num[k]][k]==-1) dp[i][num[k]][k]=dp[i][j][k-1]+dis;else dp[i][num[k]][k]=min(dp[i][num[k]][k],dp[i][j][k-1]+dis);}int ans=100000000;for(i=0;i<=4;i++)for(j=0;j<=4;j++)if (dp[i][j][n]!=-1&&dp[i][j][n]<ans) ans=dp[i][j][n];printf("%d\n",ans);}return 0;}

Thanks again to bin, axp, Tonny, and acm fans!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.