CFCodeTonDiv1+Div2
一、A题

解题思路
这个题把式子化简一下就可以得到了a[i]和a[j]为最小和最大值就行了,所以答案就是最小值和最大值的下标
代码实现
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 a[100010]; 45 int main() 46 { 47 int t; 48 cin >> t; 49 while(t--){ 50 int n; 51 int mcox,amcox; 52 int am,m; 53 m = INT_MAX; 54 am = -INT_MAX; 55 n = read(); 56 rep(i,1,n) {a[i] = read();if(a[i] < m) {mcox = i;m = a[i];} if(a[i] > am){am = a[i],amcox = i;}} 57 cout << mcox << ' ' << amcox << endl; 58 } 59 return 0; 60 }
二、B题

解题思路
因为每次你都需要把一个数变成0,那么如果能够存在一组数的差距为k,那么肯定能在最后的时候差距还为k,因为两个数的一直都在执行减前面的数的操作,最后肯定能够合成k。
代码实现
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 a[200010]; 45 int main() 46 { 47 int t; 48 t = read(); 49 while(t--){ 50 int n,k; 51 set <int> ans; 52 n = read(); 53 k = read(); 54 bool ok = false; 55 rep(i,1,n) {a[i] = read();ans.insert(a[i]);} 56 rep(i,1,n) if(ans.count(a[i] + k)) {ok = true;break;} 57 if(ok) cout << "YES" << endl; 58 else cout << "NO" << endl; 59 } 60 return 0; 61 }
三、C题

解题思路
如果是全1的话就可以做到相等,或者是有1,但是没有两个数相差1也可以实现。
因为如果没有两个数相差1,我可以从最大的那边开始依次模最大的数-1,使最大的数变为1,其他的数不变,一直这样操作的话就会使整个数列都变为1
代码实现
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 a[100010]; 45 int main() 46 { 47 int t; 48 cin >> t; 49 while(t--){ 50 int n; 51 n = read(); 52 bool allone = true; 53 bool haveone = false; 54 bool ove = false; 55 rep(i,1,n) {a[i] = read();if(a[i]!=1) allone = false;if(a[i] == 1) haveone = true;} 56 sort(a + 1,a + 1 + n); 57 rep(i,1,n - 1) if(abs(a[i] - a[i + 1]) == 1) ove = true; 58 if(!allone && haveone && ove) 59 cout << "NO" << endl; 60 else 61 cout << "YES" << endl; 62 } 63 return 0; 64 }
四、D题

解题思路
证明半天没有证明出来为什么这样子写,但是这样子写雀氏能够对。
我们用n不断除2,直到n为奇数为止,然后不断乘一个2,最后的答案就是,min(2 ^ n + 1,d),就是这两个数中最小的那个。
原理知道一点点,但是证不明白。
代码实现
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 void solve(ll k) 45 { 46 if(k & 1){ 47 cout << 2 << endl; 48 return; 49 } 50 ll ans = 1; 51 while(k){ 52 if(k & 1) 53 break; 54 k = k / 2; 55 ans *= 2; 56 } 57 if(k == 1) 58 cout << -1 << endl; 59 else 60 cout << min(2 * ans, k) << endl; 61 } 62 int main() 63 { 64 int t; 65 cin >> t; 66 while(t--){ 67 ll n; 68 n = read(); 69 solve(n); 70 } 71 return 0; 72 }
本文来自博客园,作者:{scanner},转载请注明原文链接:{https://home.cnblogs.com/u/scannerkk/}

浙公网安备 33010602011771号