31 2 331 2 1 Sample OutputCase 1: 3Case 2: 2HintThe possible configurations of the samples are illustrated below:题意:利用题目中给定的规律寻找到最少建筑的数量。弄懂题意很重要......数据范围不大,可以暴力试一试。建筑的最大可能是总数减去0高度的,则答案就是最大可能减去重复的建筑。从开始遍历,如果当前建筑低于前一个建筑,则遍历位于当前建筑前面的建筑,看是否有与其相等的。若当前建筑高于前一个建筑,说明当前建筑是单独的,继续遍历下一个建筑。具体看代码。AC代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int a[1000005],x,len; int main() { int n,i,cas = 1,ans,j; while(~scanf("%d",&n)) { ans = 0; for(i = 0; i<n; i++) scanf("%d",&a[i]); if(a[0]==0) ans++; for(i = 1; i<n; i++) { if(a[i]==0) ans++; 0高度建筑 else { for(j = i-1; j>=0; j--) { if(a[i]>a[j]) break; else if(a[i]==a[j]) 建筑重复的情况 { ans++; break; } } } } printf("Case %d: %d/n",cas++,n-ans); ans包括 0高度建筑与重复建筑 } return 0; }
新闻热点
疑难解答