Codeforces Round #737 (Div. 2) B-C
原题 : https://codeforces.com/contest/1557/problem/B
B题题意是给你一个n长度的整数数列 a(不同) ,规定至多k个区间,问能否把这k个区间排序后得到单调递增的区间。
首先原数组是无序的,从-1e9 ~ 1e9 的数字,首先可以将数字离散化处理。离散化后,用map记录下递增数列的位置,缩小需要遍历的空间。
然后用cnt来记录至少需要几个区间,如果原数组的相邻在map中不是相邻的,那么就多一个区间需要。
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 #define pb push_back 4 #define pi pair<int,int> 5 #define fi first 6 #define se second 7 using namespace std; 8 typedef long long ll; 9 const ll maxn=1e6+10; 10 #define int ll 11 inline int read() 12 { 13 char ch = getchar(); 14 int x = 0, f = 1; 15 while(ch < '0' || ch > '9') 16 { 17 if(ch == '-') f = -1; 18 ch = getchar(); 19 } 20 while(ch >= '0' && ch <= '9') 21 { 22 x = x * 10 + ch - '0'; 23 ch = getchar(); 24 } 25 return x * f; 26 } 27 int a[maxn]; 28 int b[maxn]; 29 map<int,int > c; 30 signed main() 31 { 32 ios::sync_with_stdio(false); 33 int t; 34 cin >> t; 35 while (t--) 36 { 37 int n; 38 int k; 39 cin >>n >> k; 40 for (int i = 1;i <= n;i++) 41 { 42 cin >> a[i]; 43 b[i] = a[i]; 44 } 45 sort (b+1,b+1+n); 46 for (int i = 1;i <=n;i++) 47 { 48 c[b[i]] = i;// 离散化,记录下递增数列的位置,将空间缩小。 49 } 50 int cnt = 1; 51 for (int i = 1;i <= n-1;i++) 52 { 53 if (c[a[i]] - c[a[i+1]] != -1) cnt ++; 54 } 55 if (cnt > k) cout << "NO\n"; 56 else cout << "YES\n"; 57 } 58 }
C题是一个难的计数问题(反正我不会)
原题:https://codeforces.com/contest/1557/problem/C
这里给了一个n代表长度,k代表n长度的数组a最大的数为2 ^k (数最多有k位)。求a数组里每个数求&与每个数求⊕后,&大的情况有几种。
通过观察,很容易会想到分奇偶。
一共有 2 ^ n
奇数:
每次先看第一位,容易得到,当第一位全是1或者第一位有偶数个1并且0存在。前者有1种,后者有 2 ^ (n-1) 种。 那么,从高位向地位推(或者反方向也行),每次可行的方式都有 2 ^ (n-1) +1 种,所以n为奇数是 (2 ^(n - 1 ) + 1) ^ k;
偶数 :
从高位到地位推,第一位里大于的情况有1种,等于有 2 ^ (n-1) -1。先计算等于的情况,每次都为等于,对答案的贡献为:(2 ^(n-1) - 1) ^ k。 再计算大于,大于的情况需要每一位计算。第i位时是不相等的,那么要满足前面i位是相等,后面就能任意取。有 (2 ^ (n-1) -1) ^ (i-1) ) + 2 ^ n ^ (k - i) ) . 做一次循环得到答案。
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 #define pb push_back 4 #define pi pair<int,int> 5 #define fi first 6 #define se second 7 using namespace std; 8 typedef long long ll; 9 const ll maxn=1e5+10; 10 #define int ll 11 inline int read() 12 { 13 char ch = getchar(); 14 int x = 0, f = 1; 15 while(ch < '0' || ch > '9') 16 { 17 if(ch == '-') f = -1; 18 ch = getchar(); 19 } 20 while(ch >= '0' && ch <= '9') 21 { 22 x = x * 10 + ch - '0'; 23 ch = getchar(); 24 } 25 return x * f; 26 } 27 int qp (int a,int b,int mod) 28 { 29 int ans = 1; 30 while (b) 31 { 32 if (b & 1) ans = (ans * a) % mod; 33 a = (a * a ) % mod; 34 b >>= 1; 35 } 36 return ans; 37 } 38 int mod = 1e9 + 7; 39 int add(int a,int b) 40 { 41 return (a+b+mod) % mod; 42 } 43 signed main() 44 { 45 ios::sync_with_stdio(false); 46 int _; 47 cin >> _; 48 while (_--) 49 { 50 int n,k; 51 cin >> n >> k; 52 int ans = 0; 53 int temp = qp(2,n-1,mod); 54 if ( (n & 1) == 0) 55 56 { 57 temp --;//等于的情况。 58 temp %= mod; 59 ans = qp(temp,k,mod) % mod; 60 int a = qp(2,n,mod); // 每列的一共有几次。 61 for (int i = 1;i <= k;i++) 62 { 63 ans = add(ans , qp(temp,i-1,mod) % mod *qp(a,(k-i),mod));//等于末尾加上 64 } 65 } 66 else 67 { 68 temp ++; 69 ans = qp(temp,k,mod); 70 ans %= mod; 71 } 72 cout<<ans<<'\n'; 73 } 74 }
我老菜了hhhh

浙公网安备 33010602011771号