题解:P17047 [NWERC 2021] 挑袜子 / Knitpicking
取同种袜子
即只要再任意取一只袜子,必定与取过得袜子成双。
由于不同种袜子间不能组成一对,所以我们先只考虑同种袜子。
我们设这种袜子有 只左脚袜子, 只右脚袜子, 只左右脚都适合的袜子。
如果我们只取左脚或只取右脚,那么必定不能组成一对,即可以取 只袜子。
对于左右脚都适合的袜子,只要取两只就必定可以组成一双,即可以取 只左右脚都适合的袜子。
显然的,左右脚都适合的袜子不能与只适合左脚或右脚的袜子同时取,所以对于同种类型的袜子,最多取 只。
取不同种袜子
对于不同种袜子,只要分别独立计算即可。
由于问题是最多取多少只组成一对,所以答案为
不可能成双情况
即最多取不成双袜子只数与总袜子数相同。
ACcode
#include<bits/stdc++.h>
using namespace std;
#define LL long long
inline LL read(){
char ch=getchar();
int w=1;
LL s=0;
while(ch<'0'||ch>'9'){
if(ch=='-')w*=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=(s<<3)+(s<<1)+(ch^48);
ch=getchar();
}
return w*s;
}
inline void write(LL n){
if(n<0)putchar('-'),n=-n;
if(n>9)write(n/10);
putchar(n%10+'0');
return ;
}
struct node{
LL lft,rgt,any;
};
unordered_map<string,node>a;
vector<string>strs;
int main(){
LL n=read(),ans=0,sum=0;
for(LL i=1;i<=n;i++){
string str,op;
cin>>str>>op;
if((!a[str].lft)&&(!a[str].rgt)&&(!a[str].any))strs.push_back(str);
if(op=="any")sum+=(a[str].any=read());
else if(op=="left")sum+=(a[str].lft=read());
else if(op=="right")sum+=(a[str].rgt=read());
}
for(LL i=0;i<strs.size();i++){
ans+=max({a[strs[i]].lft,a[strs[i]].rgt,min(1LL,a[strs[i]].any)});
}
if(ans==sum)cout<<"impossible";
else write(ans+1);
return 0;
}