智商问题
复制代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 inline ll read() 5 { 6 int x=0,f=1;char ch=getchar(); 7 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 9 return x*f; 10 } 11 12 /********************************************************************/ 13 14 const int maxn = 1e6+5; 15 int a[maxn]; 16 int last[maxn]; 17 int n, num; 18 19 int ask(int x){ 20 for(int i = 1;i <= num+1;i++){ 21 if(x <= last[i] && x > last[i-1]){ 22 int b = (i-1)*num; 23 for(int j = 1;j <= num;j++){ 24 if(a[b+j] >= x){ 25 return b+j; 26 } 27 } 28 } 29 } 30 } 31 32 int main(){ 33 n = read(); 34 for(int i = 1;i <= n;i++) 35 a[i] = read(); 36 sort(a+1, a+1+n); 37 num = (int)sqrt(n); 38 for(int i = 1;i <= num;i++) 39 last[i] = a[i*num]; 40 last[num+1] = a[n]; 41 int x; 42 while(scanf("%d", &x) != EOF){ 43 if(x < a[1]) 44 printf("1\n"); 45 else if(x > a[n]) 46 printf("%d\n", n+1); 47 else 48 printf("%d\n", ask(x)); 49 } 50 51 return 0; 52 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2017-09-07 hdu3938 Portal 离线+并查集