zoukankan      html  css  js  c++  java
  • [HDOJ 5183] Negative and Positive (NP) 【Hash】

    题目链接:HDOJ - 5183

    题目分析

    分两种情况,奇数位正偶数位负或者相反。

    从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中。

    BestCoder比赛的时候我写了 STL map, 然后TLE...

    注意: Hash负数的时候 % 了一个质数,得到的是负数还要 + Mod !!

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    
    using namespace std;
    
    #define Debug(x) cout << #x << " = " << x << endl
    
    typedef long long LL;
    typedef double DB;
    
    inline int gmax(int a, int b) {return a > b ? a : b;}
    inline int gmin(int a, int b) {return a < b ? a : b;}
    
    inline void Read(int &Num) 
    {
    	char c = getchar();
    	bool Neg = false; 
    	while (c < '0' || c > '9')
    	{
    		if (c == '-') Neg = true;
    		c = getchar();
    	}
    	Num = c - '0'; c = getchar();
    	while (c >= '0' && c <= '9') 
    	{
    		Num = Num * 10 + c - '0';
    		c = getchar();
    	}
    	if (Neg) Num = -Num;
    }
    
    const int MaxN = 1000000 + 5, Mod = 1000007;
    
    int T, n, k;
    int A[MaxN];
    
    struct HashNode
    {
    	int x;
    	HashNode *Next;
    } HA[MaxN], *P = HA, *Hash[2][Mod + 5];
    
    bool Find(int f, LL Num) 
    {
    	int HN = ((Num % Mod) + Mod) % Mod;
    	for (HashNode *j = Hash[f][HN]; j; j = j -> Next)
    		if (j -> x == Num) return true;
    	return false;
    }
    
    void Insert(int f, LL Num) 
    {
    	int HN = ((Num % Mod) + Mod) % Mod;
    	++P; P -> x = Num; 
    	P -> Next = Hash[f][HN]; Hash[f][HN] = P;
    }
    
    int main() 
    {	
    	scanf("%d", &T);
    	for (int Case = 1; Case <= T; ++Case) 
    	{
    		memset(Hash, 0, sizeof(Hash));
    		P = HA;
    		scanf("%d%d", &n, &k);
    		for (int i = 1; i <= n; ++i) Read(A[i]);
    		int Temp;
    		LL Sum0, Sum1;
    		Sum0 = Sum1 = 0;
    		Insert(1, 0);
    		bool Flag = false;
    		for (int i = 1; i <= n; ++i) 
    		{
    			if (i & 1) Temp = -A[i];
    			else Temp = A[i];
    			Sum0 = Sum0 + (LL)Temp;
    			Sum1 = Sum1 - (LL)Temp; 
    			if (Find(0, Sum0 - (LL)k) || Find(1, Sum1 - (LL)k)) 
    			{
    				Flag = true;
    				break;
    			}
    			if (i & 1) Insert(0, Sum0);
    			if ((i & 1) == 0) Insert(1, Sum1);
    		}
    		if (Flag) printf("Case #%d: Yes.
    ", Case);
    		else printf("Case #%d: No.
    ", Case);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    [iOS]Xcode+GitHub远程代码托管(GIT, SVN)
    [iOS]Xcode处理过时方法的警告
    [iOS]@synthesize和@dynamic关键字
    [iOS]图片高清度太高, 导致内存过大Crash
    [软件]Xcode查找系统framework所在路径
    [软件]在浏览器里添加MarkDown Here(插件)
    [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(四. iOS端代码实现)
    ios -Unity3D的EasyAR集成到已经有项目中。
    iOS创建安全的单例
    阿里云轻量应用服务器 配置mysql详解(转载)
  • 原文地址:https://www.cnblogs.com/JoeFan/p/4321345.html
Copyright © 2011-2022 走看看