ZOJ 3844 暴力

---恢复内容开始---

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

2
3
1 2 3
2
5 5

Sample Output

2
5

题意N个数,每轮将最大的一个数字ai和最小的数字aj都变成ai-aj,问最终能否使所有数字相同,并输出这个数字。Hint发现每次操作,最大值和最小值的差是严格减少的,所以有限步内就可以达到最终局面,更准确地说,所有情况都会达到最后所有数字相同的局面。暴力操作1e6次,若不能达到则认为不行。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,seg[110];
 4 bool check(){
 5     for(int i=1;i<n;i++) if(seg[i]!=seg[i+1]) return false;
 6     return true;
 7 }
 8 int main(){
 9     int T,max_pos,min_pos;
10     cin>>T;
11     while(T--){
12         cin>>n;
13         max_pos=1,min_pos=1;
14         for(int i=1;i<=n;i++) cin>>seg[i];
15
16         int tot=0;
17         bool flag=false;
18         while(++tot<=1000000){
19             if(check()){
20                 flag=true;
21                 break;
22             }
23             for(int i=1;i<=n;i++){
24                 if(seg[max_pos]<seg[i]) max_pos=i;
25                 if(seg[min_pos]>seg[i]) min_pos=i;
26             }
27             seg[max_pos]=seg[min_pos]=(seg[max_pos]-seg[min_pos]);
28         }
29         if(flag) cout<<seg[1]<<endl;
30         else cout<<"Nooooooo!"<<endl;
31     }
32     return 0;
33 }
时间: 2024-10-11 03:28:01

ZOJ 3844 暴力的相关文章

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

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 bwith a-b. Yo

【ZOJ 3844】Easy Task

题意 每次把序列中最大的数a的一个和最小的数b的一个变成a-b.求最后是否能使序列里的数全部相同,能则输出这个相同的数. 分析 一定是有解的,证明想不出来. 可以直接暴力模拟. 代码 #include<cstdio> int ok(int a[],int n) { int i; for(i=1; i<n; i++) if(a[i]!=a[i+1])break; return i==n; } int main() { int t; scanf("%d",&t);

zoj 3844

省赛热身,一道水题 n个数,n<=10,每次取出最大数和最小数,然后把这两个数替换为max-min,直到最后相等的时候,输出剩余的那一个数,如果不可能,输出Nooooooo! 开始我认为,这个问题每次回减小一个数或者消掉一个数,永远不可能发生循环,用想借助multiset的自动排序,来快速完成,结果就挂在了这上面,一直卡在这道题 1wa之后,我考虑什么时候是不可能的,然后hash,map套multiset,将n个数都搞成七位数,判重,都没过,然后心态炸了 赛后,我用数组模拟了一发1a,就是一个一

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

[数据结构暴力] zoj 3749 Chameleon

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3749 Chameleon Time Limit: 6 Seconds      Memory Limit: 65536 KB Given n groups of integers(all the integers are distinct). You should answer Q queries in this problem. Each query con

暴力 ZOJ 1403 Safecracker

题目传送门 1 /* 2 暴力:纯暴力,在家水水 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <iostream> 8 #include <string> 9 #include <vector> 10 #include <cmath> 11 using namespace std; 12 13 const i

zoj 3818 Pretty Poem(暴力处理字符串)2014年牡丹江赛区网络赛

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

Zoj 3616 Choir III 【有想法的暴力】【容斥】

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3616 题目大意:给出n, m, b, g (0 < n <= 100, 0 < m <= 2000, 0 < b, g <= m * n)这些数字,下面n行,每一行有2*m个数字,第一个数字表示一个同学对于合唱的贡献,第二个数字表示同学的性别,b和g表示在一个矩形内至少有的男生和女生的数量.求一个矩形使得这个矩形内的对合唱的贡献值最大并

ZOJ (狗狗)1426 Counting Rectangles(暴力)

Counting Rectangles Time Limit: 2 Seconds      Memory Limit: 65536 KB We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example,