[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. 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

Author: CHEN, Zemin

Source: ZOJ Monthly, January 2015

解题思路:

题意为给定n个数(2<=n<=10),每个数的范围为 -100000到100000,有一种操作:找到这n个数中最大的数max,最小的数min,把这两个数都替换为 max-min , 重复这样的操作,直到这n个数完全相同,如果可以达到这样的目的,就输出最后那n个数中的其中一个(每个数都一样),如果不能,则输出 Nooooooo!。

n的范围很小,直接按照题意排序模拟即可,一开始觉得任何数据都可以达到目的,没有输出Nooooooo!的情况,写了一发,交上去,果断WA.后来和二师兄讨论,应该记录状态才对,一旦出现相同的状态,那么就不能达到目的,输出Nooooooo!。方法为哈希查找,每一个状态用包含这n个数的结构体来保存,关键字是这n个数的和,以n个数的和对一个大素取余作为关键字,10个数的最大和不超过100000*10,所以开辟这么大的结构体类型的vector数组,当出现一个新的状态时,就去该状态的和取余后的索引中的那些状态去比较,这样可以减少比较次数,因为一个状态和另一个状态相同,它们分别的元素之和取余后相等是大前提。

注意:n个元素和可能是负数,建立索引要加上mod变为正数.

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
using namespace std;
#define ll long long
const int mod=100000*10+1;
const int inf=0x3f3f3f3f;
const int maxn=12;
int num[maxn];
int n;
struct Node
{
    int a[maxn];
};
vector<Node>vc[mod+10];

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<=mod;i++)
            vc[i].clear();
        Node tp;
        cin>>n;
        int sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>num[i];
            sum+=num[i];
        }
        tp.a[n]=sum;
        sort(num,num+n);
        if(num[0]==num[n-1])
        {
            cout<<num[0]<<endl;
            continue;
        }
        for(int i=0;i<n;i++)
            tp.a[i]=num[i];
        vc[(tp.a[n]+mod)%mod].push_back(tp);
        int ans=inf;
        while(1)
        {
            int cha=num[n-1]-num[0];
            sum=sum-num[n-1]-num[0]+2*cha;//新的sum
            num[0]=cha,num[n-1]=cha;
            sort(num,num+n);
            if(num[0]==num[n-1])
            {
                ans=num[0];
                break;
            }
            tp.a[n]=sum;
            for(int i=0;i<n;i++)
                tp.a[i]=num[i];
            bool ok=0;
            int idx=(tp.a[n]+mod)%mod;//去相应的索引中去找看是否有相同的状态
            for(int i=0;i<vc[idx].size();i++)
            {
                    int j;
                    for(j=0;j<n;j++)
                    {
                        if(tp.a[j]!=vc[idx][i].a[j])
                            break;
                    }
                    if(j==n)
                    {
                        ok=1;//找到了!
                        break;
                    }
            }
            if(ok==1)
                break;
            vc[idx].push_back(tp);
        }
        if(ans!=inf)
            cout<<ans<<endl;
        else
            cout<<"Nooooooo!"<<endl;
    }
    return 0;
}
时间: 2024-10-07 06:09:06

[ACM] ZOJ 3844 Easy Task (模拟+哈希)的相关文章

ZOJ 2969: Easy Task

ZOJ 2969: Easy Task 1 ///@date 2017-02-07 2 ///@author Sycamore, ZJNU 3 #include <iostream> 4 using namespace std; 5 int c[1001]; 6 int main() 7 { 8 int T; 9 cin >> T; 10 while (T--) 11 { 12 int N; 13 cin >> N; 14 for (int i = N; i >=

杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> /* 题意:找闰年. if((i%4==0 && i%100!=0) || i%400==0)count++; 3 2005 25 1855 12 2004 10000 2108 1904 43236 */ int main() { int t,y,n; int i,count=0; whil

哈理工2015暑假训练赛BNU16488 Easy Task(简单题)

A - Easy Task Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusPracticeZOJ 2969 Description Calculating the derivation of a polynomial is an easy task. Given a function f(x) , we use (f(x))' to denote its derivatio

HDU-1076-An Easy Task(Debian下水题测试.....)

An Easy Task Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17062    Accepted Submission(s): 10902 Problem Description Ignatius was born in a leap year, so he want to know when he could hold h

HDU-------An Easy Task

An Easy Task Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4088 Accepted Submission(s): 2327   Problem Description Ignatius was born in a leap year, so he want to know when he could hold his bir

2016 省热身赛 Easy Task

Easy Task Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these nintegers. And then you sho

CodeForces462 A. Appleman and Easy Task

A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you

An Easy Task(简箪题)

B. An Easy Task Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 35999 Font Size:  +   - You are given an easy task by your supervisor -- to find the best va

Codeforces 263A. Appleman and Easy Task

A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you