暑假训练day2 : zoj选练

来源:互联网 发布:java 乐观锁 例子 编辑:程序博客网 时间:2024/05/21 06:32

3844
A. Easy Task

Time Limit: 2 Seconds      Memory Limit: 65536 KB

You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and b with a-b. Your task will not be finished unless all the integers are equal.

Now the problem come, you want to know whether you can finish you task. And if you can finish the task, you want to know the final result.

Input

The first line of the input contain an integer T(T≤ 20) indicates the number of test cases.

Then T cases come. Each case consists of two lines. The first line is an integer n(2≤ n≤ 10) as the problem described. The second line contains n integers, all of them are no less than -100000 and no more than 100000.

Output

For each case you should print one line. If you can finish your task, you should print one of the n integers. Otherwise, you should print "Nooooooo!"(without quotes).

Sample Input

231 2 325 5

Sample Output

25

水,直接模拟。

#include <iostream>#include <string.h>#include <queue>#include <vector>#include <map>#include <algorithm>using namespace std;typedef long long ll;int a[20],b[20];int main() {int t,q,n,i,j,flag;cin >> q;for (t=1;t<=q;t++) {cin >> n;flag=1;for (i=1;i<=n;i++) {cin >> a[i];if (a[i]!=a[i-1]&&i!=1) flag=0;}int k=-0x3f3f3f3f,l=0x3f3f3f3f;int kk=0,ll=0;i=0;if (!flag)while (i<1000000) {i++;flag=0;k=-0x3f3f3f3f,l=0x3f3f3f3f;for (j=1;j<=n;j++) {if (a[j]>k) {k=max(k,a[j]);kk=j;}if (a[j]<l) {l=min(l,a[j]);ll=j;}}if (l==k) break;int num=0;for (j=1;j<=n;j++) {if (j!=ll&&j!=kk) {b[++num]=a[j];}}b[n-1]=b[n]=k-l;for (j=1;j<=n;j++) a[j]=b[j];}else l=a[1];if (i<1000000) cout << l << endl; else cout << "Nooooooo!" << endl;}return 0;}

B. GCD Reduce

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

You are given a sequence {A1A2, ..., AN}. You task is to change all the element of the sequence to 1 with the following operations (you may need to apply it multiple times):

  • choose two indexes i and j (1 ≤ i < j ≤ N);
  • change both Ai and Aj to gcd(AiAj), where gcd(AiAj) is the greatest common divisor of Ai and Aj.

You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5N operations.

Input

Input will consist of multiple test cases.

The first line of each case contains one integer N (1 ≤ N ≤ 105), indicating the length of the sequence. The second line contains N integers, A1A2, ..., AN (1 ≤ Ai ≤ 109).

Output

For each test case, print a line containing the test case number (beginning with 1) followed by one integer M, indicating the number of operations needed. You must assure that M is no larger than 5N. If you cannot find a solution, make M equal to -1 and ignore the following output.

In the next M lines, each contains two integers i and j (1 ≤ i < j ≤ N), indicating an operation, separated by one space.

If there are multiple answers, you can print any of them.

Remember to print a blank line after each case. But extra spaces and blank lines are not allowed.

Sample Input

42 2 3 442 2 2 2

Sample Output

Case 1: 31 31 21 4Case 2: -1

若所有数的共同公约数最大不为1,则无解。否则,用所有的数和第一个数进行操作必能将第一个数构造为1.
#include <iostream>#include <string.h>#include <cstdio>#include <math.h>#include <cmath>#include <map>#include <algorithm>using namespace std;const int maxn=100005;int a[maxn];int gcd(int a,int b) {if (a<b) {int c=a;a=b;b=c;}while (b) {int c=b;b=a%b;a=c;}return a;} int main() {int n,cnt=0,i;while (scanf("%d",&n)!=EOF) {cnt++;printf("Case %d: ",cnt);for (i=1;i<=n;i++) {scanf("%d",&a[i]);}for (i=2;i<=n;i++) {a[1]=a[i]=gcd(a[1],a[i]);}if (a[1]!=1) {printf("-1\n\n");continue;}int num=n-1;for (i=2;i<=n;i++) if (a[i]!=1) num++;printf("%d\n",num);for (i=2;i<=n;i++) printf("%d %d\n",1,i);for (i=2;i<=n;i++) if (a[i]!=1)     printf("%d %d\n",1,i);printf("\n");}return 0;}
C. Infusion Altar

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Bob is recently playing a game called Minecraft, especially a mod called Thaumcraft. It is a mod of magic.

Usually, Bob has Obsessions with Symmetry while playing Minecraft. This obsession is useless in the gameplay generally. However, in Thaumcraft, the infusion altar requires symmetry to keep it stable.

Bob built an infusion altar in his secret chamber, but it was not so symmetrical. After some explosions, Bob decided to fix the infusion altar to make it symmetrical.

InfusionAltar

You will be given the map of Bob's secret chamber. It is of size n*n(n is an odd number), the infusion altar is always at the center of his secret chamber. The following picture is a typical map. The 3*3 square in the center is the Infusion Altar, it is a multi-block structure. Here, '#' means Runic Matrix, 'o' means Arcane Pedestal, '.' means an empty place, 'a'-'z' means occult paraphernalia(like skulls, crystals and candles) Bob placed around the Infusion Altar. There will not be characters other than 'a'-'z', '.', '#'.

.aab.bo.obb.#.abo.obbbab.

Now, the question is that at least how many blocks need to be changed to make the whole map symmetrical. Here, being symmetrical means having all four axes of symmetry for a square. Also, you can change any character on the map to any other character.

Input

There are multiple cases. The first line contains one integer T which is the number of test cases.
For each case, The first line contains an integer n ( 3 ≤ n ≤ 99, and n is an odd number)
For the next n lines, each line contains n characters showing the map.
It is guaranteed that the Infusion Altar is at the center of the map. 
It is guaranteed that only 'a'-'z' and '.' will appear out of the Infusion Altar.

Output

One integer for each test case which is the least number of blocks that should be changed.

Sample Input

33o.o.#.o.o5.aab.bo.obb.#.abo.obbbab.5aabbaao.oaa.#.aao.oaaaaaa

Sample Output

032

Hint

The first sample is a standard Infusion Altar.
In second sample, Bob will change his secret chamber to the following map.

.bab.bo.oba.#.abo.ob.bab.
大坑!
对角线4条,横竖2条,斜2条!一共8个方块对称!
一直以为题目意思是四个格子对称。
#include <iostream>#include <string.h>#include <cstdio>#include <math.h>#include <cmath>#include <map>#include <algorithm>using namespace std;typedef long long ll;const int maxn=105;char s[maxn][maxn];int my[288];int main() {int t,n,q,i,j;scanf("%d",&q);for (t=1;t<=q;t++) {scanf("%d",&n);int center=n/2,ans=0;for (i=0;i<n;i++) scanf("%s",s[i]);for (i=0;i<center;i++) {for (j=i;j<center;j++) {if (i==center-1&&j==center-1) continue;memset(my,0,sizeof(my));my[s[i][j]]++;my[s[n-i-1][j]]++;my[s[i][n-j-1]]++;my[s[n-i-1][n-j-1]]++;my[s[j][i]]++;my[s[j][n-i-1]]++;my[s[n-j-1][i]]++;my[s[n-j-1][n-i-1]]++;int m1=max(max(my[s[i][j]],my[s[n-i-1][n-j-1]]),max(my[s[i][n-j-1]],my[s[n-i-1][j]]));int m2=max(max(my[s[j][i]],my[s[j][n-i-1]]),max(my[s[n-j-1][i]],my[s[n-j-1][n-i-1]]));int m=max(m1,m2);if (i==j) ans+=(8-m)/2; else ans+=8-m;}}for (i=0;i<center;i++) {memset(my,0,sizeof(my));my[s[i][center]]++;my[s[n-i-1][center]]++;my[s[center][n-i-1]]++;my[s[center][i]]++;int m=max(max(my[s[i][center]],my[s[n-i-1][center]]),max(my[s[center][n-i-1]],my[s[center][i]]));ans+=4-m;}printf("%d\n",ans);}return 0;}
D. Poker Face

Time Limit: 2 Seconds      Memory Limit: 65536 KB

As is known to all, coders are lack of exercise and Kato is one of them. In order to get fit, he decides to go mining in MINECRAFT. However, it is always dangerous in the mine because there are a lot of ZOMBIEs in MINECRAFT. They kill Kato time and time again when he is mining. But this time, Kato is in invincible mode and wants to observe what the ZOMBIEs' faces are like. After his obervation, he found that the ZOMBIEs' faces are regular. Now he provides you the ZOMBIEs' faces of size 8, 16 and 32, it is your task to draw the other ZOMBIEs. You should find the recursive pattern from the small cases and learn to draw bigger cases.

Input

There are multiple test cases. For each case there is only a number N (8 ≤ N≤ 1024, N is a power of 2), which is size of the ZOMBIE's face. The input ends with an integer less than eight.

Output

For each case, output the image of the ZOMBIE's face. And print an empty line after each case.

Sample Input

816320

Sample Output

***********  ******  ******  **** **** ** *  * ** *  * **************************              ** ****    **** ** *  *    *  * ** *  *    *  * ** *  *    *  * ** ****    **** **              **   ********   **   * *  * *   **   * *  * *   **   * **** *   **   ***  ***   **   ***  ***   **   ***  ***   **************************************************                              **                              **                              **   ********        ********   **   *      *        *      *   **   *      *        *      *   **   *      *        *      *   **   *      *        *      *   **   *      *        *      *   **   *      *        *      *   **   *      *        *      *   **   ********        ********   **                              **                              **                              **       ****************       **       *   ***  ***   *       **       *   ***  ***   *       **       *   ***  ***   *       **       *   * **** *   *       **       *   * *  * *   *       **       *   * *  * *   *       **       *   ********   *       **       *              *       **       * ****    **** *       **       * *  *    *  * *       **       * *  *    *  * *       **       * *  *    *  * *       **       * ****    **** *       **       *              *       *********************************

Hint

The sample out put is the ZOMBIE's face of size 8, 16, 32.
把上一个图案倒过来,再加上眼睛。。。
比赛时看错,打了5k,还是没过。唉

E. Consecutive Blocks

Time Limit: 2 Seconds      Memory Limit: 65536 KB

There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in a row. And the i-th block's color is Ci (1 ≤ Ci ≤ 109). Now you can remove at most K (0 ≤ K ≤ N) blocks, then rearrange the blocks by their index from left to right. Please figure out the length of the largest consecutive blocks with the same color in the new blocks created by doing this.

For example, one sequence is {1 1 1 2 2 3 2 2} and K=1. We can remove the 6-th block, then we will get sequence {1 1 1 2 2 2 2}. The length of the largest consecutive blocks with the same color is 4.

Input

Input will consist of multiple test cases and each case will consist of two lines. For each test case the program has to read the integers N and K, separated by a blank, from the first line. The color of the blocks will be given in the second line of the test case, separated by a blank. The i-th integer means Ci.

Output

Please output the corresponding length of the largest consecutive blocks, one line for one case.

Sample Input

8 11 1 1 2 2 3 2 2

Sample Output

4

先排序,把值相同的数按初始顺序排列在一起。最后,在这一有序数列中更新左边界、右边界,使删除的数不超过k个,同时都拥有同一个数值。

#include <iostream>#include <string.h>#include <cstdio>#include <math.h>#include <cmath>#include <map>#include <algorithm>using namespace std;const int maxn=100005;struct Point {int val,pos;};Point a[maxn];bool cmp (Point x,Point y){return x.val<y.val || (x.val==y.val&&x.pos<y.pos);}int main() {int n,k,i,j,ans;while (scanf("%d%d",&n,&k)!=EOF) {for (i=1;i<=n;i++) {a[i].pos=i;scanf("%d",&a[i].val);}sort(a+1,a+n+1,cmp);j=ans=1;for (i=2;i<=n;i++) {if (a[i].val==a[i-1].val) {while ((a[i].pos-a[j].pos)-(i-j)>k) j++;ans=max(ans,i-j+1);} else {j=i;}}printf("%d\n",ans);}return 0;}
F.An Easy Game

Time Limit: 2 Seconds      Memory Limit: 65536 KB

One day, Edward and Flandre play a game. Flandre will show two 01-strings s1 and s2, the lengths of two strings are n. Then, Edward must move exact k steps. In each step, Edward should change exact m positions of s1. That means exact m positions of s1, '0' will be changed to '1' and '1' will be changed to '0'.

The problem comes, how many different ways can Edward change s1 to s2 after k steps? Please calculate the number of the ways mod 1000000009.

Input

Input will consist of multiple test cases and each case will consist of three lines. The first line of each case consist of three integers n (1 ≤ n ≤ 100), k (0 ≤ k ≤ 100), m (0 ≤ m ≤ n). The second line of each case is a 01-string s1. The third line of each case is a 01-string s2.

Output

For each test case, you should output a line consist of the result.

Sample Input

3 2 1100001

Sample Output

2

Hint

100->101->001100->000->001

DP好题。
dp[i][j]表示走i步之后有j个数位和目标数不同。
每走一步,在j个不同的数中选l个,n-j个相同的数中选m-l个改变。相应的转移方程可以利用组合数推出。
#include <iostream>#include <string.h>#include <cstdio>#include <math.h>#include <cmath>#include <map>#include <algorithm>typedef long long ll;const int maxn=105;const ll mod=1000000009;using namespace std;ll dp[maxn][maxn],c[maxn][maxn];char s1[maxn],s2[maxn];int main() {int n,k,m,i,j,cnt,l;for (i=0;i<=100;i++) {for (j=0;j<=100;j++) {c[i][j]=1;}}for (i=2;i<=100;i++) {for (j=1;j<=i-1;j++) {c[i][j]=c[i-1][j-1]+c[i-1][j];c[i][j]%=mod;}}while(scanf("%d%d%d",&n,&k,&m)!=EOF) {    scanf("%s",s1);scanf("%s",s2);cnt=0;for (i=0;i<n;i++) if (s1[i]!=s2[i]) cnt++;memset(dp,0,sizeof(dp));dp[0][cnt]=1;for (i=0;i<k;i++) {for (j=0;j<=n;j++) {for (l=0;l<=m;l++) {if ((j+m-2*l)>=0&&(j+m-2*l)<=n&&abs(l)<=j&&(n-j)>=(m-l)) {dp[i+1][j-l+m-l]+=(((dp[i][j]*c[j][l])%mod)*c[n-j][m-l])%mod;dp[i+1][j+m-l-l]%=mod;    }}}}printf("%lld\n",dp[k][0]);}return 0;}


G. Machine

Time Limit: 2 Seconds      Memory Limit: 65536 KB

In a typical assembly line, machines are connected one by one. The first machine's output product will be the second machine's raw material. To simplify the problem, we put all machines into a two-dimension shelf. Every machine occupied exactly one grid and has two input ports and only one output port. One input port can get material from only one machine.

Pipes will be used to connect between these machines. There are two kinds of pipes : 'I' kind and 'L' kind. We should notice that the 'I' kind pipe can be linked one by one. Each pipe will also occupied one grid.

In Bob's factory, each machine will get raw materials from zero, one or two other machines. Some machines don't need any input materials, but any machine must have an output. Machines are coded by numbers from 1 to n. The output of the machines with greater code can be the input of the machines with less code. The machine NO.1's output product will be the final product, and will not be any other machine's input. Bob's factory has a shelf with infinite height, but finite width. He will give you the dependency relationship of these machines, and want you to arrange these machines and pipes so that he can minimize the width of the shelf.

Here's an example for you to help understand :

Products will falling from higher machine to lower machine through the pipes. Here, machine 1 gets materials from machine 2 and machine 3. The whole width of this system is 2.

Input

For each case, the first line will be an integer n indicates the number of the machines (2≤ n≤ 10000). The following line will include n-1 numbers. The i-th number ai means that the output of machine i+1 will be the input of machine ai (ai≤ i). The same code will be appeared at most twice. Notice machine 1's output will be the final output, and won't be any machine's input.

Output

For each case, we need exactly one integer as output, which is the minimal width of the shelf.

Sample Input

31 171 1 2 2 3 3

Sample Output

23

Hint

Case 1 is the example.
Case 2:


树型DP。
如果两个子树占用行数不同,则总可以把占用行数多的子树放在上面,这样的话,占用的行数就是子树行数的较大者。如果行数相同,则必多占用一行。从叶子向上DP。
#include <iostream>#include <string.h>#include <cstdio>#include <math.h>#include <cmath>#include <map>#include <algorithm>const int maxn=10005;using namespace std;int head[maxn],num;struct Edge{int pre,from,to;};Edge edge[maxn];void addedge(int from,int to) {edge[num]=(Edge){head[from],from,to};head[from]=num;num++;}int dfs(int now) {if (head[now]==-1) {return 1;} else {int ans,k;ans=dfs(edge[head[now]].to);if (edge[head[now]].pre!=-1) {k=dfs(edge[edge[head[now]].pre].to);if (ans==k) return k+1; else return max(ans,k);} else return ans;}}int main() {int n,i,x,ans;while (scanf("%d",&n)!=EOF) {num=0;memset(head,-1,sizeof(head));for (i=2;i<=n;i++) {scanf("%d",&x);addedge(x,i);}ans=dfs(1);printf("%d\n",ans);}return 0;}



原创粉丝点击