AC修炼计划(AtCoder Regular Contest 179)A~C

一给  金牌会员 | 2024-7-14 12:18:51 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 542|帖子 542|积分 1626

A - Partition

A题传送门
这道题不难发现,如果数字最终的和大于等于K,我们可以把这个原数列从大到小排序,得到最终答案。
如果和小于K,则从小到大排序,同时验证是否符合要求。
  1. #pragma GCC optimize(3)  //O2优化开启
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int,int> PII;
  8. // const int mod=1e9+7;
  9. const int MX=0x3f3f3f3f3f3f3f3f;
  10. //inline int read()                     //快读
  11. //{
  12. //    int xr=0,F=1; char cr;
  13. //    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
  14. //    while(cr>='0'&&cr<='9')
  15. //        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
  16. //    return xr*F;
  17. //}
  18. //void write(int x)                     //快写
  19. //{
  20. //    if(x<0) putchar('-'),x=-x;
  21. //    if(x>9) write(x/10); putchar(x%10+'0');
  22. //}
  23. // 比 unordered_map 更快的哈希表
  24. // #include <ext/pb_ds/assoc_container.hpp>
  25. // using namespace __gnu_pbds;
  26. // const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
  27. // struct chash {
  28. //     int operator()(int x) const { return x ^ RANDOM; }
  29. // };
  30. // typedef gp_hash_table<int, int, chash> hash_t;
  31. constexpr ll mod = 1e9 + 7;                                //此处为自动取模的数
  32. class modint{
  33.     ll num;
  34. public:
  35.     modint(ll num = 0) :num(num % mod){}
  36.     ll val() const {
  37.         return num;
  38.     }
  39.     modint pow(ll other) {
  40.         modint res(1), temp = *this;
  41.         while(other) {
  42.             if(other & 1) res = res * temp;
  43.             temp = temp * temp;
  44.             other >>= 1;
  45.         }
  46.         return res;
  47.     }
  48.     constexpr ll norm(ll num) const {
  49.         if (num < 0) num += mod;
  50.         if (num >= mod) num -= mod;
  51.         return num;
  52.     }
  53.     modint inv(){ return pow(mod - 2); }
  54.     modint operator+(modint other){ return modint(num + other.num); }
  55.     modint operator-(){ return { -num }; }
  56.     modint operator-(modint other){ return modint(-other + *this); }
  57.     modint operator*(modint other){ return modint(num * other.num); }
  58.     modint operator/(modint other){ return *this * other.inv(); }
  59.     modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
  60.     modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
  61.     modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
  62.     modint &operator/=(modint other) { return *this *= other.inv(); }
  63.     friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
  64.     friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
  65. };
  66. int n,k;
  67. int a[500005];
  68. void icealsoheat(){
  69.    
  70.     cin>>n>>k;
  71.     for(int i=1;i<=n;i++)cin>>a[i];
  72.     // sort(a+1,a+1+n);
  73.    
  74.     if(k<=0){
  75.         int sum=0;
  76.         for(int i=1;i<=n;i++){
  77.             sum+=a[i];
  78.         }
  79.         if(sum>=k){
  80.             cout<<"Yes\n";
  81.             sort(a+1,a+1+n,[&](int x,int y){return x>y;});
  82.             for(int i=1;i<=n;i++){
  83.                 cout<<a[i]<<" ";
  84.             }
  85.         }
  86.         else{
  87.             cout<<"No\n";
  88.         }
  89.     }
  90.     else{
  91.         cout<<"Yes\n";
  92.         sort(a+1,a+1+n);
  93.         for(int i=1;i<=n;i++){
  94.             cout<<a[i]<<" ";
  95.         }
  96.     }
  97.    
  98. }
  99. signed main(){
  100.     ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
  101.     cin.tie();
  102.     cout.tie();
  103.     int _yq;
  104.     _yq=1;
  105.     // cin>>_yq;
  106.     while(_yq--){
  107.         icealsoheat();
  108.     }
  109. }
  110. //
  111. //⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
  112. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
  113. //⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
  114. //⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
  115. //⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
  116. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
  117. //⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
  118. //⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
  119. //⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
  120. //⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
  121. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
  122. //⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
  123. //
复制代码
B - Between B and B

B题传送门
想了半天没搞出来,厥后看了大佬的题解提示才名顿开。
这题可以用dp的头脑去求,通过题目标数据,我们可以大胆去猜,首先复杂度肯定要带个n,其次m仅仅等于10也让我们可以发散性的去想到状压的                                             2                            10                                       2^{10}                  210的复杂度,还可以再添一个m,以是最终的复杂度最多为O(nmlog                                             2                            10                                       2^{10}                  210)。
我们可通过状压来枚举各个数是否符合条件可以放入的情况。比如第i位,假如我向这个数位放入的数字是j,首先,放入j的条件条件是在当前位数和上一个放入j的位数之间我们放入了至少一个a[j],此时j可以放入,同时,放入了j后,j会使得后面全部a[x]=j的数字都可以放入,我们可以通过状态去枚举各个数字是否可以放入,能放入的话对应的二进制位数就是1,不能放入就是0。
  1. #pragma GCC optimize(3)  //O2优化开启
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int,int> PII;
  8. // const int mod=1e9+7;
  9. const int MX=0x3f3f3f3f3f3f3f3f;
  10. //inline int read()                     //快读
  11. //{
  12. //    int xr=0,F=1; char cr;
  13. //    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
  14. //    while(cr>='0'&&cr<='9')
  15. //        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
  16. //    return xr*F;
  17. //}
  18. //void write(int x)                     //快写
  19. //{
  20. //    if(x<0) putchar('-'),x=-x;
  21. //    if(x>9) write(x/10); putchar(x%10+'0');
  22. //}
  23. // 比 unordered_map 更快的哈希表
  24. // #include <ext/pb_ds/assoc_container.hpp>
  25. // using namespace __gnu_pbds;
  26. // const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
  27. // struct chash {
  28. //     int operator()(int x) const { return x ^ RANDOM; }
  29. // };
  30. // typedef gp_hash_table<int, int, chash> hash_t;
  31. constexpr ll mod = 998244353;                                //此处为自动取模的数
  32. class modint{
  33.     ll num;
  34. public:
  35.     modint(ll num = 0) :num(num % mod){}
  36.     ll val() const {
  37.         return num;
  38.     }
  39.     modint pow(ll other) {
  40.         modint res(1), temp = *this;
  41.         while(other) {
  42.             if(other & 1) res = res * temp;
  43.             temp = temp * temp;
  44.             other >>= 1;
  45.         }
  46.         return res;
  47.     }
  48.     constexpr ll norm(ll num) const {
  49.         if (num < 0) num += mod;
  50.         if (num >= mod) num -= mod;
  51.         return num;
  52.     }
  53.     modint inv(){ return pow(mod - 2); }
  54.     modint operator+(modint other){ return modint(num + other.num); }
  55.     modint operator-(){ return { -num }; }
  56.     modint operator-(modint other){ return modint(-other + *this); }
  57.     modint operator*(modint other){ return modint(num * other.num); }
  58.     modint operator/(modint other){ return *this * other.inv(); }
  59.     modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
  60.     modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
  61.     modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
  62.     modint &operator/=(modint other) { return *this *= other.inv(); }
  63.     friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
  64.     friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
  65. };
  66. int n,m;
  67. int a[50005];
  68. modint dp[20005][2000];
  69. int id[50005];
  70. void icealsoheat(){
  71.     int m,n;
  72.     cin>>n>>m;
  73.     for(int i=1;i<=n;i++)cin>>a[i];
  74.     for(int i=1;i<=n;i++){
  75.         id[a[i]]|=(1<<(i-1));
  76.     }
  77.     // for(int i=0;i<n;i++)dp[0][1<<i]=1;
  78.     dp[0][(1<<n)-1]=1;
  79.    
  80.     for(int i=1;i<=m;i++){
  81.         for(int j=1;j<=n;j++){
  82.             for(int o=0;o<(1<<n);o++){
  83.                 if(o>>(j-1)&1){
  84.                     
  85.                     int x=o;
  86.                     x-=(1<<(j-1));
  87.                     x|=id[j];
  88.                     dp[i][x]+=dp[i-1][o];
  89.                 }
  90.             }
  91.         }
  92.     }
  93.     modint ans=0;
  94.     for(int i=0;i<(1<<n);i++)ans+=dp[m][i];
  95.     cout<<ans;
  96. }
  97. signed main(){
  98.     ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
  99.     cin.tie();
  100.     cout.tie();
  101.     int _yq;
  102.     _yq=1;
  103.     // cin>>_yq;
  104.     while(_yq--){
  105.         icealsoheat();
  106.     }
  107. }
  108. //
  109. //⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
  110. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
  111. //⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
  112. //⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
  113. //⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
  114. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
  115. //⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
  116. //⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
  117. //⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
  118. //⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
  119. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
  120. //⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
  121. //
复制代码
C - Beware of Overflow

C题传送门
还是我太菜了,看题目都费劲,最后还是不争气的看了题解,竟然题解把查询放入了排序的cmp函数中,确实还是我本身的思维太范围了,容易想到的是,我们把全部的数从小到大排序,然后头尾相加,再把相加的数通过二分按次序放入这个数中,重复上述操作,知道符合最后条件为止,O(nlogn)的复杂度,以是不会超过25000次扣问。
  1. #pragma GCC optimize(3)  //O2优化开启
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int,int> PII;
  8. // const int mod=1e9+7;
  9. const int MX=0x3f3f3f3f3f3f3f3f;
  10. //inline int read()                     //快读
  11. //{
  12. //    int xr=0,F=1; char cr;
  13. //    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
  14. //    while(cr>='0'&&cr<='9')
  15. //        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
  16. //    return xr*F;
  17. //}
  18. //void write(int x)                     //快写
  19. //{
  20. //    if(x<0) putchar('-'),x=-x;
  21. //    if(x>9) write(x/10); putchar(x%10+'0');
  22. //}
  23. // 比 unordered_map 更快的哈希表
  24. // #include <ext/pb_ds/assoc_container.hpp>
  25. // using namespace __gnu_pbds;
  26. // const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
  27. // struct chash {
  28. //     int operator()(int x) const { return x ^ RANDOM; }
  29. // };
  30. // typedef gp_hash_table<int, int, chash> hash_t;
  31. constexpr ll mod = 998244353;                                //此处为自动取模的数
  32. class modint{
  33.     ll num;
  34. public:
  35.     modint(ll num = 0) :num(num % mod){}
  36.     ll val() const {
  37.         return num;
  38.     }
  39.     modint pow(ll other) {
  40.         modint res(1), temp = *this;
  41.         while(other) {
  42.             if(other & 1) res = res * temp;
  43.             temp = temp * temp;
  44.             other >>= 1;
  45.         }
  46.         return res;
  47.     }
  48.     constexpr ll norm(ll num) const {
  49.         if (num < 0) num += mod;
  50.         if (num >= mod) num -= mod;
  51.         return num;
  52.     }
  53.     modint inv(){ return pow(mod - 2); }
  54.     modint operator+(modint other){ return modint(num + other.num); }
  55.     modint operator-(){ return { -num }; }
  56.     modint operator-(modint other){ return modint(-other + *this); }
  57.     modint operator*(modint other){ return modint(num * other.num); }
  58.     modint operator/(modint other){ return *this * other.inv(); }
  59.     modint &operator*=(modint other) { num = num * other.num % mod; return *this; }
  60.     modint &operator+=(modint other) { num = norm(num + other.num); return *this; }
  61.     modint &operator-=(modint other) { num = norm(num - other.num); return *this; }
  62.     modint &operator/=(modint other) { return *this *= other.inv(); }
  63.     friend istream& operator>>(istream& is, modint& other){ is >> other.num; other.num %= mod; return is; }
  64.     friend ostream& operator<<(ostream& os, modint other){ other.num = (other.num + mod) % mod; return os << other.num; }
  65. };
  66. int n;
  67. bool cmp(int a,int b){
  68.     cout<<"? "<<a<<" "<<b<<endl;
  69.     int s;
  70.     cin>>s;
  71.     return s;
  72. }
  73. // bool check(int p,int o){
  74. //     cout<<"? "<<p<<" "<<o<<endl;
  75. //     int x;
  76. //     cin>>x;
  77. //     return x==0;
  78. // }
  79. void icealsoheat(){
  80.     cin>>n;
  81.     vector<int>ve;
  82.     for(int i=1;i<=n;i++){
  83.         ve.push_back(i);
  84.     }
  85.     sort(ve.begin(),ve.end(),cmp);
  86.     while(ve.size()>1){
  87.         int le=ve[0];
  88.         int re=ve.back();
  89.         // int x=le+re;
  90.         cout<<"+ "<<le<<" "<<re<<endl;
  91.         int x;
  92.         cin>>x;
  93.         ve.erase(ve.begin());
  94.         ve.pop_back();
  95.         // cout<<"+"<<i
  96.         if(ve.size()==0){
  97.             break;
  98.         }
  99.         int l=0,r=ve.size()-1;
  100.         int mid;
  101.         while(l<r){
  102.             mid=(l+r)>>1;
  103.             if(cmp(x,ve[mid]))r=mid;
  104.             else l=mid+1;
  105.         }
  106.         if(!cmp(x,ve[l]))l++;
  107.         ve.insert(ve.begin()+l,x);
  108.     }
  109.     cout<<"!"<<endl;
  110. }
  111. signed main(){
  112.     ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
  113.     cin.tie();
  114.     cout.tie();
  115.     int _yq;
  116.     _yq=1;
  117.     // cin>>_yq;
  118.     while(_yq--){
  119.         icealsoheat();
  120.     }
  121. }
  122. //
  123. //⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
  124. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
  125. //⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
  126. //⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
  127. //⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
  128. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
  129. //⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
  130. //⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
  131. //⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
  132. //⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
  133. //⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
  134. //⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
  135. //
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

一给

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表