CF778div1+div2(A-C)

一、A题

  

 

 题目意思是我做一次把某个区间逆转的操作,然后输出可能得到的最大的相邻两数之和。很明显,这个题无论如何都能让最大的和次大的两个数相加。因此我们只需要排个序,然后输出最大两个数的和就行了。

 

代码实现

 1 #include "bits/stdc++.h"
 2 #define PII pair<int,int>
 3 #define rep(i,z,n) for(int i = z;i <= n; i++)
 4 #define per(i,n,z) for(int i = n;i >= z; i--)
 5 #define ll long long
 6 #define db double
 7 #define vi vector<int>
 8 #define debug(x) cerr << "!!!" << x << endl;
 9 using namespace std;
10 //从某个串中把某个子串替换成另一个子串
11 string& replace_all(string& src, const string& old_value, const string& new_value) {
12     // 每次重新定位起始位置,防止上轮替换后的字符串形成新的old_value
13     for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
14         if ((pos = src.find(old_value, pos)) != string::npos) {
15             src.replace(pos, old_value.length(), new_value);
16         }
17         else break;
18     }
19     return src;
20 }
21 inline ll read()
22 {
23     ll s,r;
24     r = 1;
25     s = 0;
26     char ch = getchar();
27     while(ch < '0' || ch > '9'){
28         if(ch == '-')
29             r = -1;
30         ch = getchar();
31     }
32     while(ch >= '0' && ch <= '9'){
33         s = (s << 1) + (s << 3) + (ch ^ 48);
34         ch = getchar();
35     }
36     return s * r;
37 }
38 inline void write(ll x)
39 {
40     if(x < 0) putchar('-'),x = -x;
41     if(x > 9) write(x / 10);
42     putchar(x % 10 + '0');
43 }
44 ll a[1010];
45 int main()
46 {
47     int t;
48     cin >> t;
49     while(t--){
50         int n;
51         n = read();
52         rep(i,1,n)
53             a[i] = read();
54         sort(a + 1,a + 1 + n);
55         cout << a[n] + a[n - 1] << endl;
56     }
57     return 0;
58 }

 

 

二、B题

 

 

 

解题思路

  这个题让你不断的删除字符,直到某个字符在这个字符的后面的没有出现时,我们就停止删除。很明显我们可以记录某个字符的位置,从头扫到位,判断是否是最后一位,如果是最后一位了,那么这个字符和这个字符后面的都算作答案串了。

 

代码实现

 1 #include "bits/stdc++.h"
 2 #define PII pair<int,int>
 3 #define rep(i,z,n) for(int i = z;i <= n; i++)
 4 #define per(i,n,z) for(int i = n;i >= z; i--)
 5 #define ll long long
 6 #define db double
 7 #define vi vector<int>
 8 #define debug(x) cerr << "!!!" << x << endl;
 9 using namespace std;
10 //从某个串中把某个子串替换成另一个子串
11 string& replace_all(string& src, const string& old_value, const string& new_value) {
12     // 每次重新定位起始位置,防止上轮替换后的字符串形成新的old_value
13     for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
14         if ((pos = src.find(old_value, pos)) != string::npos) {
15             src.replace(pos, old_value.length(), new_value);
16         }
17         else break;
18     }
19     return src;
20 }
21 inline ll read()
22 {
23     ll s,r;
24     r = 1;
25     s = 0;
26     char ch = getchar();
27     while(ch < '0' || ch > '9'){
28         if(ch == '-')
29             r = -1;
30         ch = getchar();
31     }
32     while(ch >= '0' && ch <= '9'){
33         s = (s << 1) + (s << 3) + (ch ^ 48);
34         ch = getchar();
35     }
36     return s * r;
37 }
38 inline void write(ll x)
39 {
40     if(x < 0) putchar('-'),x = -x;
41     if(x > 9) write(x / 10);
42     putchar(x % 10 + '0');
43 }
44 int main()
45 {
46     int t;
47     cin >> t;
48     while(t--){
49         string s;
50         cin >> s;
51         int vis[30];
52         int len = s.size();
53         memset(vis,0,sizeof(vis));
54         for(int i = 0;i < len;i++)
55             vis[s[i] - '0'] = i;
56         string ans;
57         for(int i = 0;i < len;i++){
58             if(vis[s[i] - '0'] <= i){
59                 int j = i;
60                 while(j < len){
61                     ans += s[j];
62                     j++;
63                 }
64                 break;
65             }
66         }
67         cout << ans << endl;
68     }
69     return 0;
70 }

 

三、C题

  

 

 解题思路

  这个题问你是否存在一个数,能经过他说的两个操作转化为他给的目标序列。

  如果正着推最后一个数是否存在,这明显不可能。

  这时候我们假装答案存在,并且最后是sum,我们看是否能经过这些操作得到目标序列

 

  我们用两个multiset记录目标串和枚举串。

  枚举串初始化为sum 

  目标串是输入的串

  

  我们每次枚举如果是奇数我可以进行的到sum / 2和sum / 2 + 1的数字

  当我们的sum小于目标串的最大值时,肯定就不可以枚举到目标串了,就可以直接退出循环,或者我们把目标串枚举完了,也可以退出循环。

  最后如果目标串的没有被消除的话就输出NO

                否则输出YES

三、代码实现

 1 #include "bits/stdc++.h"
 2 #define PII pair<int,int>
 3 #define rep(i,z,n) for(int i = z;i <= n; i++)
 4 #define per(i,n,z) for(int i = n;i >= z; i--)
 5 #define ll long long
 6 #define db double
 7 #define vi vector<int>
 8 #define debug(x) cerr << "!!!" << x << endl;
 9 using namespace std;
10 //从某个串中把某个子串替换成另一个子串
11 string& replace_all(string& src, const string& old_value, const string& new_value) {
12     // 每次重新定位起始位置,防止上轮替换后的字符串形成新的old_value
13     for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
14         if ((pos = src.find(old_value, pos)) != string::npos) {
15             src.replace(pos, old_value.length(), new_value);
16         }
17         else break;
18     }
19     return src;
20 }
21 inline ll read()
22 {
23     ll s,r;
24     r = 1;
25     s = 0;
26     char ch = getchar();
27     while(ch < '0' || ch > '9'){
28         if(ch == '-')
29             r = -1;
30         ch = getchar();
31     }
32     while(ch >= '0' && ch <= '9'){
33         s = (s << 1) + (s << 3) + (ch ^ 48);
34         ch = getchar();
35     }
36     return s * r;
37 }
38 inline void write(ll x)
39 {
40     if(x < 0) putchar('-'),x = -x;
41     if(x > 9) write(x / 10);
42     putchar(x % 10 + '0');
43 }
44 int n;
45 multiset <ll> mp1;
46 multiset <ll> mp2;
47 int main()
48 {
49     int t;
50     t = read();
51     while(t--){
52         ll sum = 0;
53         n = read();
54         mp1.clear();
55         mp2.clear();
56         rep(i,1,n) {
57             ll d;
58             d = read();
59             sum += d;
60             mp1.insert(d);
61         }
62         if(n == 1){
63             cout << "YES" << endl;
64             continue;
65         }
66         mp2.insert(sum);
67         for(auto iter = mp2.rbegin();!mp1.empty();iter = mp2.rbegin()){
68             if(*iter < *(mp1.rbegin()))
69                 break;
70             if(mp1.count(*iter)){
71                 mp1.erase(mp1.lower_bound(*iter));
72                 mp2.erase(mp2.lower_bound(*iter));
73                 continue;
74             }
75             mp2.insert((*iter + 1) / 2);
76             mp2.insert(*iter / 2);
77             mp2.erase(mp2.lower_bound(*iter));
78         }
79         if(mp1.empty())
80             cout << "YES" << endl;
81         else
82             cout << "NO" << endl;
83     }
84     return 0;
85 }
posted @ 2022-03-24 19:52  scannerkk  阅读(92)  评论(0)    收藏  举报