D. Journey to Un’Goro 思路:思维+搜索
一开始以为是构造,好吧但是是搜索。
我们先考虑什么时候是最大值?
首先考虑,题目要求我们从 且红色的数量是奇数被认为是好的。那么我们考虑记录 表示前 个里面红色的数量。那么从 的红色的数量就可以表示为: 。又因为要求红色的数量是奇数,那么对于从 红色的数量是奇数那么: 和 的奇偶性应该不一样。那么我们考虑对于 里面 有 个奇数和 个是偶数,那么最终的答案对数就是 。那么如果要求最大的,两个数的和为 且乘积的值最大,肯定是两个越接近的数的乘积。那么答案就是 。
接下来输出方案数。原本以为答案只输出前 种只是为了不要输出太多,其实是为了方便我们剪枝。之后我们只要考虑去搜就 啦。
剪枝的话:记录前 步,前缀是偶数出现次数 ,前缀是奇数出现次数 ,当前 的奇偶性 , 表示偶数, 表示奇数。如果当 或者 其中一个大于一半以上就直接 了,因为肯定不满足。如果方案数已经有 了,那么也直接 。
注意:
只有红色能改变奇偶性
要求字典序最小,那先考虑放 。
为什么要记录 ?因为如果当前是奇数那么加上一个 还是奇数,否则如果是偶数加上一个数还是偶数,我需要知道前缀 是奇数还是偶数来确定当前加的这个使得奇数前缀增加还是偶数前缀增加。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <bits/stdc++.h> using namespace std;typedef long long ll;const int mod = 1e9 + 7 ;const int N = 2e5 + 10 ;ll n,hav; char a[N];void dfs (int step,int cnt0,int cnt1,int st) { if (hav>=100 )exit (0 ); if (cnt0>(ll)ceil (1.0 *(n+1 )/2 )||cnt1>(ll)ceil (1.0 *(n+1 )/2 ))return ; if (step>n) { for (int i = 1 ;i <= n; i++) cout<<a[i]; cout<<"\n" ; hav++; return ; } a[step] = 'b' ; dfs (step+1 , cnt0 + (st^1 ), cnt1 + st, st); a[step] = 'r' ; st ^= 1 ; dfs (step+1 , cnt0 + (st^1 ), cnt1 + st, st); } int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ), cout.tie (nullptr ); cin>>n; cout<<(n+1 )/2 *(ll)ceil ((n+1 )*1.0 /2 )<<"\n" ; dfs (1 ,1 ,0 ,0 ); return 0 ; }
F. Kobolds and Catacombs 思路:思维
我们先对原数组 排序得到最终数组 。我们去求这两个数组的前缀和,当且仅当前缀和一样的时候可以被分成一段,那么我们只需要判断有几个前缀和一样就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <bits/stdc++.h> using namespace std;typedef long long ll;const int mod = 1e9 + 7 ;const int N = 2e6 + 10 ;ll a[N],b[N],sa[N],sb[N]; int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ), cout.tie (nullptr ); int n; cin>>n; for (int i = 1 ;i <= n; i++) cin>>a[i],b[i] = a[i]; sort (b+1 ,b+1 +n); for (int i = 1 ;i <= n; i++) sa[i] = sa[i-1 ]+a[i],sb[i] = sb[i-1 ]+b[i]; int cnt = 0 ; for (int i = 1 ;i <= n; i++) if (sa[i]==sb[i])cnt++; cout<<cnt<<"\n" ; return 0 ; }
I. Rise of Shadows 思路:数论,追击问题,我们以时针作为参照物(静止不动),相对运动速度针对分针分析。
绝 对 绝 对
相 对 绝 对 绝 对 绝 对
相当于分针相对于时针以每分钟 的速度运动。
绝 对 绝 对
其中
那么柿子转化为:绝 对 绝 对
即:
根据剩余定理 :如果 是模 的一个完全剩余系,且有 ,那么 也是模 的一个完全剩余系。更一般地, 也是完全剩余系。
为满足互质条件,同除 ,那么
那么:
同时
接下来只需求解出 的整数解的个数。
因为 是 的完全剩余系,又 ,那么 也是构成模 意义下的完全剩余系。
由于 之后余数 ,因此一共又 ,一共 个,并且无重复。即 于余数值一一对应。在 中一共有 个。
再把 还原到 ,答案就是 个。
注意特判: ,此时 所有都满足,答案是 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;typedef long long ll;const int mod = 1e9 + 7 ;const int N = 2e5 + 10 ;ll H,M,A; int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ), cout.tie (nullptr ); cin>>H>>M>>A; if (2 *A==H*M)cout<<H*M<<"\n" ; else { ll g = __gcd(H-1 ,H*M); cout<<(2 *(A/g)+1 )*g<<"\n" ; } return 0 ; }
K.Scholomance Academy 思路:小模拟
注意:分界点的处理。考虑,如果去除分界点会怎么样呢?会变成一条斜线,我们考虑把它拉平(emmm,可能有点抽象hh)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 #include <bits/stdc++.h> using namespace std;typedef long long ll;const int N = 1e6 + 10 ;vector<int > vx; vector<int > a, b; vector<pair<double , double >> event; int n;void solve () { cin>>n; for (int i = 1 ; i <= n; i++) { char opt[2 ]; int x; scanf ("%s%d" , opt, &x); vx.push_back (x); vx.push_back (x - 1 ); vx.push_back (x + 1 ); if (opt[0 ] == '+' ) a.push_back (x); else b.push_back (x); } sort (vx.begin (), vx.end ()); vx.erase (unique (vx.begin (), vx.end ()), vx.end ()); sort (a.begin (), a.end ()); sort (b.begin (), b.end ()); int sza = a.size (), szb = b.size (); for (auto x : vx) { int TP = 0 , FN = 0 , FP = 0 , TN = 0 ; if (lower_bound (a.begin (), a.end (), x) == a.end ()) TP = 0 , FN = sza; else { int sz = lower_bound (a.begin (), a.end (), x) - a.begin () + 1 ; TP = sza - sz + 1 ; FN = sza - TP; } if (lower_bound (b.begin (), b.end (), x) == b.end ()) TN = szb; else { int sz = lower_bound (b.begin (), b.end (), x) - b.begin () + 1 ; FP = szb - sz + 1 ; TN = szb - FP; } double TPR = 1.0 * TP / (1.0 * TP + FN); double FPR = 1.0 * FP / (1.0 * TN + FP); event.push_back ({FPR, TPR}); } event.push_back ({0.0 , 0.0 }); sort (event.begin (), event.end ()); int m = event.size (); double res = 0 ; for (int i = 1 ; i < m; i++) { double tx = event[i - 1 ].first, ty = event[i - 1 ].second; double x = event[i].first, y = event[i].second; if (x == 0.5 && y == 0.5 ) y = 0.25 ; if (x != tx && y != ty) y = ty; res = res + (x - tx) * y; } printf ("%.11lf\n" , res); } int main () { solve (); }