111

冯诚阳  •  6天前


#include <cstdio> 
#include <iostream> 
using namespace std; 

int exist[1000001];   

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       exist[height] = 1; 
   } 
    
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
       if (exist[k] > 0) { 
           cout << 1; 
       }  
       else { 
           cout << 0; 
       } 
       if (i < M - 1) { 
           cout << " "; 
       } 
   } 
   
   return 0; 
}


评论:

#include <cstdio> 

char exist[100001];  // 使用 char 类型更省内存,访问更快 

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       exist[height] = 1; 
   } 
    
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
        
       if (exist[k]) { 
           printf("1"); 
       } else { 
           printf("0"); 
       } 
        
       if (i < M - 1) { 
           printf(" "); 
       } 
   } 
    
   return 0; 
}


冯诚阳  •  6天前

#include <cstdio> 
#include <cstring> 

int exist[100001];  // 最大高度 100000,索引 0-100000 

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   // 初始化数组 
   memset(exist, 0, sizeof(exist)); 
    
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       if (height >= 0 && height <= 100000) {  // 安全检查 
           exist[height] = 1; 
       } 
   } 
    
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
        
       // 安全检查,如果 k 超出范围直接输出 0 
       if (k < 0 || k > 100000) { 
           printf("0"); 
       } else { 
           printf("%d", exist[k] ? 1 : 0); 
       } 
        
       if (i < M - 1) { 
           printf(" "); 
       } 
   } 
    
   return 0; 
}


冯诚阳  •  6天前

#include <cstdio> 
#include <cstring> 

int exist[100001];  // 全局变量,在堆区分配,不会栈溢出 

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   memset(exist, 0, sizeof(exist));  // 初始化 
    
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       if (height >= 0 && height <= 100000) { 
           exist[height] = 1; 
       } 
   } 
    
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
        
       if (k >= 0 && k <= 100000 && exist[k]) { 
           printf("1"); 
       } else { 
           printf("0"); 
       } 
        
       if (i < M - 1) { 
           printf(" "); 
       } 
   } 
    
   return 0; 
}


冯诚阳  •  6天前

又到过年了,狗熊岭的动物们都忙碌起来,张灯结彩准备过年。李老板却要光头强砍掉一些百年美人松回去。美人松都是很高的,但是也不会超过长整型(long long)。现在光头强看到丛林里有N颗美人松按照从矮到高排好了,当然每棵松的高度都是已知的。李老板要问光头强M次:每次询问高度为K的美人松有多少颗?

输入

第一行,两个正整数N,M ,1<=N<=10^6,1<=M<=10^5;

第二行,N个正整数,之间用一个空格隔开,表示N棵美人松的高度;

第三行M个正整数k,表示M个询问,每次询问高度为K的美人松有多少棵。

输出

一行M个正整数,之间用一个空格隔开,分别表示对应每次询问高度为K的树的查询统计数量,不存在则输出0

样例

样例输入1

5 2 2 3 3 4 5 3 4

样例输出1

2 1


顾文博  •  6天前

#include <cstdio> 

int cnt[100001];  // 全局变量,默认初始化为 0 

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       if (height >= 0 && height <= 100000) { 
           cnt[height]++; 
       } 
   } 
    
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
        
       if (k >= 0 && k <= 100000) { 
           printf("%d", cnt[k]); 
       } else { 
           printf("0"); 
       } 
        
       if (i < M - 1) { 
           printf(" "); 
       } 
   } 
    
   return 0; 
}


冯诚阳  •  6天前

#include <stdio.h> 

int cnt[100001];  // 计数数组 

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   // 读入N个高度并计数 
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       if (height >= 1 && height <= 100000) { 
           cnt[height]++; 
       } 
   } 
    
   // 处理M个查询 
   int first = 1; 
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
        
       if (!first) { 
           printf(" "); 
       } 
       first = 0; 
        
       if (k >= 1 && k <= 100000) { 
           printf("%d", cnt[k]); 
       } else { 
           printf("0"); 
       } 
   } 
    
   return 0; 
}


冯诚阳  •  6天前

#include <stdio.h> 

int cnt[100001]; 

int main() { 
   int N, M; 
   scanf("%d %d", &N, &M); 
    
   int height; 
   for (int i = 0; i < N; ++i) { 
       scanf("%d", &height); 
       cnt[height]++;  // 题目保证高度在范围内,不加检查更快 
   } 
    
   for (int i = 0; i < M; ++i) { 
       int k; 
       scanf("%d", &k); 
       printf("%d", cnt[k]);  // 直接输出,0就是0 
        
       if (i < M - 1) { 
           printf(" "); 
       } 
   } 
    
   return 0; 
}


冯诚阳  •  6天前