PAT—甲级
- 1148 Werewolf - Simple Version (20 分)
- 1149 Dangerous Goods Packaging (25 分)
- 1153 Decode Registration Card of PAT (25 分)
- 1154 Vertex Coloring (25 分)
1148 Werewolf - Simple Version (20 分)
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/1038429808099098624
AC代码
#include
using namespace std;
int main()
{int n;cin>>n;int a[n&#43;1];for(int i&#61;1;i<&#61;n;i&#43;&#43;)cin>>a[i];for(int i&#61;1;i<n;i&#43;&#43;){for(int j&#61;i&#43;1;j<&#61;n;j&#43;&#43;){unordered_map<int,int> wolf;wolf[i]&#61;wolf[j]&#61;1;int cnt1&#61;0,cnt2&#61;0;for(int k&#61;1;k<&#61;n;k&#43;&#43;){if( (a[k]<0 && wolf[abs(a[k])]&#61;&#61;0) || (a[k]>0 &&wolf[abs(a[k])]&#61;&#61;1) ) {cnt2&#43;&#43;;if(wolf[k]&#61;&#61;1){cnt1&#43;&#43;;}}}if(cnt1&#61;&#61;1&&cnt2&#61;&#61;2){cout<<i<<" "<<j;return 0;}}}cout<<"No Solution";
}
注意事项
注意题目中的这句话at least one but not all the werewolves were lying, and there were exactly 2 liars
&#xff0c;其实就是说谎话的有两人、狼人好人各一个&#xff0c;不要被at least欺骗了&#xff0c;狼人有且仅有一个说谎
1149 Dangerous Goods Packaging (25 分)
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/1038429908921778176
AC代码
#include
using namespace std;
int main()
{int n,m,k;cin>>n>>m;unordered_map<string,int> f,vis;while(n--){string s1,s2;cin>>s1>>s2;f[s1&#43;s2]&#61;f[s2&#43;s1]&#61;vis[s1]&#61;vis[s2]&#61;1;}while(m--){cin>>k;getchar();vector<string> a;string t;for(int i&#61;0;i<k;i&#43;&#43;){cin>>t;if(vis[t]) a.push_back(t);}int flag&#61;0,len&#61;a.size();for(int i&#61;0;i<len;i&#43;&#43;){for(int j&#61;i&#43;1;j<len;j&#43;&#43;){if(f[a[i]&#43;a[j]]&#61;&#61;1){flag&#61;1;break;}}if(flag) break;}if(flag) cout<<"No"<<endl;else cout<<"Yes"<<endl;}
}
注意事项
- f的存储方式其实和二维数组类似&#xff0c;但在有些题中会比较方便
- 在判断之前&#xff0c;要提前通过vis把不在不兼容list的数字去掉&#xff0c;减少比较的时间&#xff0c;否则会超时
1153 Decode Registration Card of PAT (25 分)
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/1071785190929788928
AC代码
#include
using namespace std;
struct node{string s;int value;
};
bool cmp(node x,node y)
{if(x.value&#61;&#61;y.value) return x.s<y.s;else return x.value>y.value;
}
int main()
{int n,m;cin>>n>>m;vector<node> ans(n);for(int i&#61;0;i<n;i&#43;&#43;)cin>>ans[i].s>>ans[i].value;for(int t&#61;1;t<&#61;m;t&#43;&#43;){int type,sum&#61;0,cnt&#61;0; string s;cin>>type>>s;printf("Case %d: %d %s\n",t,type,s.c_str());vector<node> res;if(type&#61;&#61;1){for(int i&#61;0;i<n;i&#43;&#43;){if(ans[i].s[0]&#61;&#61;s[0]) res.push_back(ans[i]);}}else if(type&#61;&#61;2){for(int i&#61;0;i<n;i&#43;&#43;){if(ans[i].s.substr(1,3)&#61;&#61;s){cnt&#43;&#43;;sum&#43;&#61;ans[i].value;}}if(cnt) cout<<cnt<<" "<<sum<<endl;} else if(type&#61;&#61;3){unordered_map<string,int> f;for(int i&#61;0;i<n;i&#43;&#43;){if(ans[i].s.substr(4,6)&#61;&#61;s) f[ans[i].s.substr(1,3)]&#43;&#43;;}for(auto it:f){res.push_back({it.first,it.second});}} sort(res.begin(),res.end(),cmp); for(int i&#61;0;i<res.size();i&#43;&#43;){printf("%s %d\n",res[i].s.c_str(),res[i].value);
注意事项
- 这三种类型的输出格式看似不同&#xff0c;其实1、3是可以合并的&#xff0c;又因为输入是字符串加数字的格式&#xff0c;也可以将1、3的输出转化为同样的格式。合并后代码逻辑更加清晰。
- 这道题最后的输出要写为printf而不能用cout&#xff0c;众所周知printf比cout快。如果采用
cout<&#xff0c;就会超时&#xff0c;这就是一句之差。
1154 Vertex Coloring (25 分)
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552
AC代码
#include
using namespace std;
int n,m,color[10001];
vector<int> vec[10001];
int main()
{int n,m,k;cin>>n>>m;while(m--){int x,y;cin>>x>>y;vec[x].push_back(y);vec[y].push_back(x);}cin>>k;while(k--){set<int> sets;for(int i&#61;0;i<n;i&#43;&#43;){cin>>color[i];sets.insert(color[i]);}int flag&#61;0;for(int i&#61;0;i<n;i&#43;&#43;){for(int j&#61;0;j<vec[i].size();j&#43;&#43;){if(color[i]&#61;&#61;color[vec[i][j]]){flag&#61;1;break;}if(flag) break;}}if(flag) cout<<"No"<<endl;else printf("%d-coloring\n",sets.size());}
}
注意事项
- 这道题不能按照n个点直接两两比较&#xff0c;这样会超时。要比较和当前点相连的点颜色是否不同
- 统计颜色可以直接使用set