#include\x20 #include\x20 using\x20namespace\x20std;

int\x20main()\x20{ \x20\x20int\x20n,\x20m,\x20k; \x20\x20cin\x20>>\x20n\x20>>\x20m\x20>>\x20k; \x20\x20int\x20s1[n],\x20s2[m]; \x20\x20for(int\x20i=0;\x20i<n;\x20i++)\x20{ \x20\x20\x20\x20cin\x20>>\x20s1[i]; \x20\x20} \x20\x20for(int\x20i=0;\x20i<m;\x20i++)\x20{ \x20\x20\x20\x20cin\x20>>\x20s2[i]; \x20\x20} \x20\x20sort(s1,\x20s1+n); \x20\x20sort(s2,\x20s2+m,\x20greater()); \x20\x20long\x20long\x20sum\x20=\x200;\x20//\x20使用\x20long\x20long\x20防止溢出 \x20\x20for(int\x20i=0;\x20i<n;\x20i++)\x20{ \x20\x20\x20\x20if(i\x20<\x20k\x20&&\x20s2[i]\x20>\x20s1[i])\x20{ \x20\x20\x20\x20\x20\x20sum\x20+=\x20s2[i]; \x20\x20\x20\x20}else\x20{ \x20\x20\x20\x20\x20\x20sum\x20+=\x20s1[i]; \x20\x20\x20\x20} \x20\x20} \x20\x20cout\x20<<\x20sum\x20<<\x20endl; \x20\x20return\x200; }

C++ 算法题:序列交换最大和 - 代码解析和优化

原文地址: https://www.cveoy.top/t/topic/pHca 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录