华为机试第三题 天然货仓
原题链接
#include<bits/stdc++.h> using namespace std; int main(){ int length_good; int length_array; string str; set<pair<int,int>>st; int count=0; getline(cin,str); length_good=atoi(str.c_str()); getline(cin,str); length_array=atoi(str.c_str()); int ord[length_array]; getline(cin,str); for(int i=0;i<length_array;i++){ if(str.find(',')==string::npos) ord[i]=atoi(str.c_str()); int barrier=str.find(','); ord[i]=atoi(str.substr(0,barrier).c_str()); str=str.substr(barrier+1,str.size()-barrier-1); } //cout<<length_good<<endl<<length_array<<endl; for(int i=0;i<length_array;i++){ //cout<<ord[i]<<","; st.insert(pair<int,int>(ord[i],i)); } set<pair<int,int>>::iterator it=st.begin(); pair<int,int>temp=*it; //cout<<it->first();//为什么*it.first有问题 <纵坐标,横坐标><y,x> while(temp.first!=0){ for(int i=0;i<length_good;i++){ if(it->first!=temp.first||it->second!=(temp.second+i)){ temp=*it; break; } st.insert(pair<int,int>(it->first+1,it->second)); st.erase(it); it=st.begin(); if(i==(length_good-1)){ count++; temp=*it; } } } cout<<count; }