A Groundhog and 2-Power Representation
https://ac.nowcoder.com/acm/contest/5674/A
python一行代码:
print(eval(input().replace("(","**(")))
c++栈模拟:遇到数字,左括号就进栈,遇到右括号开始出栈,累加遇到的每一个数字,直到遇到左括号,弹出左括号以及左括号前面的那个数字,求幂次并将这个结果入栈
#include<bits/stdc++.h>
using namespace std;
string s;
stack<int> st;
int main(){
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]>='0'&&s[i]<='9') st.push(s[i]-'0');
else if(s[i]=='(') st.push(-1);
else if(s[i]==')'){
int num=st.top(); st.pop();
while(!st.empty()){
int tmp1=st.top();st.pop();
if(tmp1==-1) break;
num+=tmp1;
}
int di=st.top();st.pop();
num=pow(di,num);
st.push(num);
}
}
int big=st.top();st.pop();//累加结果
while(!st.empty()){
int q1=st.top();st.pop();
if(q1==-1) break;
big+=q1;
}
cout<<big;
}
F Groundhog Looking Dowdy
https://ac.nowcoder.com/acm/contest/5674/F
先把所有的衣服放到一维数组中,按照k值排序。尺取法维护每一个天数m的区间,并且维护最大值和最小值的差值,最后输出答案即可
#include<bits/stdc++.h>
using namespace std;
struct Node{
int k,day;
}node[2000005];
bool cmp(Node a,Node b){
return a.k<b.k;
}
int ha[2000005]
int main(){
int n,m,cnt=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int k;
scanf("%d",&k);
for(int j=1;j<=k;j++){
scanf("%d",&node[cnt].k);
node[cnt].day=i;
cnt++;
}
}
sort(node,node+cnt,cmp);
int res=1e9;
int cou=0;//区间内不同天数
int s=0;//区间左断点
for(int t=0;t<cnt;t++){//枚举区间右端点
if(!ha[node[t].day]) cou++;//如果这一天没有被选过,cou++
ha[node[t].day]++;//这一天标记被选了多少次
if(cou==m){//选择天数够了m天
res=min(res,node[t].k-node[s].k);
ha[node[t].day]--;//左端点要右移,那么左端点代表的天数减一
if(!ha[node[s].day]) cou--;//如果被减成0了说明区间的天数少了一个
s++;//左端点推进
}
}
printf("%d",res);
}