A

B

C

D

给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法

因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果dis[i]+dis[j]+1>=distance(s,t)&&dis[j]+dis[i]+1>=distance(i,j)就为一条要求边

  1. #include <bits/stdc++.h>
  2. #define PI acos(-1.0)
  3. #define mem(a,b) memset((a),b,sizeof(a))
  4. #define TS printf("!!!\n")
  5. #define pb push_back
  6. #define inf 1e9
  7. //std::ios::sync_with_stdio(false);
  8. using namespace std;
  9. //priority_queue<int,vector<int>,greater<int>> que; get min
  10. const double eps = 1.0e-10;
  11. const double EPS = 1.0e-4;
  12. typedef pair<int, int> pairint;
  13. typedef long long ll;
  14. typedef unsigned long long ull;
  15. //const int maxn = 3e5 + 10;
  16. const int maxn = ;
  17. const int turn[][] = {{, }, { -, }, {, }, {, -}};
  18. const int turn2[][] = {{, }, { -, }, {, }, {, -}, {, -}, { -, -}, {, }, { -, }};
  19. //priority_queue<int, vector<int>, less<int>> que;
  20. //next_permutation
  21. string a;
  22. vector<int> gra[];
  23. int from, to;
  24. int anser;
  25. int n, m, s, t;
  26. int sum;
  27. int tong[][];
  28. int sdis[];
  29. int tdis[];
  30. void getsdis()
  31. {
  32. for (int i = ; i <= n; i++)
  33. {
  34. if (i == s)
  35. {
  36. continue;
  37. }
  38. sdis[i] = INT_MAX;
  39. }
  40. queue<int> que;
  41. que.push(s);
  42. while (!que.empty())
  43. {
  44. int now = que.front();
  45. que.pop();
  46. int len = gra[now].size();
  47. for (int i = ; i < len; i++)
  48. {
  49. int to = gra[now][i];
  50. if (sdis[to] > sdis[now] + )
  51. {
  52. sdis[to] = sdis[now] + ;
  53. que.push(to);
  54. }
  55. }
  56. }
  57. }
  58. void gettdis()
  59. {
  60. for (int i = ; i <= n; i++)
  61. {
  62. if (i == t)
  63. {
  64. continue;
  65. }
  66. tdis[i] = INT_MAX;
  67. }
  68. queue<int> que;
  69. que.push(t);
  70. while (!que.empty())
  71. {
  72. int now = que.front();
  73. que.pop();
  74. int len = gra[now].size();
  75. for (int i = ; i < len; i++)
  76. {
  77. int to = gra[now][i];
  78. if (tdis[to] > tdis[now] + )
  79. {
  80. tdis[to] = tdis[now] + ;
  81. que.push(to);
  82. }
  83. }
  84. }
  85. }
  86. int main()
  87. {
  88. cin >> n >> m >> s >> t;
  89. for (int i = ; i <= m; i++)
  90. {
  91. scanf("%d %d", &from, &to);
  92. gra[from].pb(to);
  93. gra[to].pb(from);
  94. tong[from][to] = tong[to][from] = ;
  95. }
  96. sum = (n - ) * n / ;
  97. getsdis();
  98. gettdis();
  99. int mindis = sdis[t];
  100. // cout << mindis << endl;
  101. // for (int i = 1; i <= n; i++)
  102. // {
  103. // cout << sdis[i] << " ";
  104. // }
  105. // cout << endl;
  106. // for (int i = 1; i <= n; i++)
  107. // {
  108. // cout << tdis[i] << " ";
  109. // }
  110. // cout << endl;
  111. for (int i = ; i <= n - ; i++)
  112. {
  113. for (int j = i + ; j <= n; j++)
  114. {
  115. if (tong[i][j])
  116. {
  117. continue;
  118. }
  119. if (sdis[i] + tdis[j] + >= mindis && sdis[j] + tdis[i] + >= mindis)
  120. {
  121. anser++;
  122. }
  123. }
  124. }
  125. cout << anser << endl;
  126. return ;
  127. }

E

给你N个水管 每个水管流出的水的温度是恒定的 但是流量是可控的 给你一个温度T问你每秒最多的流量是多少

按每个水管的温度排序 然后贪心

  1. #include <bits/stdc++.h>
  2. #define PI acos(-1.0)
  3. #define mem(a,b) memset((a),b,sizeof(a))
  4. #define TS printf("!!!\n")
  5. #define pb push_back
  6. #define inf 1e9
  7. //std::ios::sync_with_stdio(false);
  8. using namespace std;
  9. //priority_queue<int,vector<int>,greater<int>> que; get min
  10. const double eps = 1.0e-10;
  11. typedef pair<int, int> pairint;
  12. typedef long long ll;
  13. typedef unsigned long long ull;
  14. //const int maxn = 3e5 + 10;
  15. const int maxn = ;
  16. const int turn[][] = {{, }, { -, }, {, }, {, -}};
  17. const int turn2[][] = {{, }, { -, }, {, }, {, -}, {, -}, { -, -}, {, }, { -, }};
  18. //priority_queue<int, vector<int>, less<int>> que;
  19. //next_permutation
  20. //double a[200005];
  21. //double t[200005];
  22. pair<double, double> wendu[];
  23. int n;
  24. double T;
  25. double anser;
  26. int flag = ;
  27. double nowx, nowy;
  28. bool cmp(pair<double, double> a, pair<double, double> b)
  29. {
  30. if (a.second == b.second)
  31. {
  32. return a.first < b.first;
  33. }
  34. return a.second < b.second;
  35. }
  36. int main()
  37. {
  38. cin >> n >> T;
  39. for (int i = ; i <= n; i++)
  40. {
  41. scanf("%lf", &wendu[i].first);
  42. }
  43. for (int i = ; i <= n; i++)
  44. {
  45. scanf("%lf", &wendu[i].second);
  46. }
  47. sort(wendu + , wendu + + n, cmp);
  48. int left = ;
  49. int right = n;
  50. for (int i = ; i <= n; i++)
  51. {
  52. nowx += 1.0 * wendu[i].first * wendu[i].second;
  53. nowy += wendu[i].first;
  54. }
  55. //printf("%.8f\n", nowx);
  56. //printf("%.8f\n", nowy);
  57. if (fabs(nowx / nowy - T) < eps)
  58. {
  59. printf("%.7f\n", nowy);
  60. return ;
  61. }
  62. else if (nowx / nowy - T > eps)
  63. {
  64. while (fabs(nowx / nowy - T) > eps)
  65. {
  66. if (wendu[right].second <= T)
  67. {
  68. flag = ;
  69. break;
  70. }
  71. double nextx, nexty;
  72. nextx = nowx - 1.0 * wendu[right].first * wendu[right].second;
  73. nexty = nowy - wendu[right].first;
  74. if (nextx / nexty - T > eps)
  75. {
  76. nowx = nextx;
  77. nowy = nexty;
  78. right--;
  79. }
  80. else
  81. {
  82. anser = nowy - (nowx - 1.0 * T * nowy) / (wendu[right].second - T);
  83. printf("%.7f\n", anser);
  84. return ;
  85. }
  86. }
  87. if (flag)
  88. {
  89. printf("%.7f\n", nowy);
  90. }
  91. }
  92. else
  93. {
  94. while (fabs(nowx / nowy - T) > eps)
  95. {
  96. if (wendu[left].second >= T)
  97. {
  98. flag = ;
  99. break;
  100. }
  101. double nextx, nexty;
  102. nextx = nowx - 1.0 * wendu[left].first * wendu[left].second;
  103. nexty = nowy - wendu[left].first;
  104. if (nextx / nexty - T < eps)
  105. {
  106. nowx = nextx;
  107. nowy = nexty;
  108. left++;
  109. }
  110. else
  111. {
  112. anser = nowy - (1.0 * T * nowy - nowx) / (T - wendu[left].second);
  113. printf("%.7f\n", anser);
  114. return ;
  115. }
  116. }
  117. if (flag)
  118. {
  119. printf("%.7f\n", nowy);
  120. }
  121. }
  122. if (!flag)
  123. {
  124. cout << << endl;
  125. }
  126. return ;
  127. }

F

给你一个三行的M列的矩阵(M<=1e18) 有N个障碍 要求从(2,1)开始到(2,M)结束 有多少种方法%MOD

先把矩阵离散化为2*N+1个矩阵 然后分情况用矩阵快速幂算答案

  1. #include <bits/stdc++.h>
  2. #define PI acos(-1.0)
  3. #define mem(a,b) memset((a),b,sizeof(a))
  4. #define TS printf("!!!\n")
  5. #define pb push_back
  6. #define inf 1e9
  7. //std::ios::sync_with_stdio(false);
  8. using namespace std;
  9. //priority_queue<int,vector<int>,greater<int>> que; get min
  10. const double eps = 1.0e-10;
  11. const double EPS = 1.0e-4;
  12. typedef pair<int, int> pairint;
  13. typedef long long ll;
  14. typedef unsigned long long ull;
  15. //const int maxn = 3e5 + 10;
  16. const int turn[][] = {{, }, { -, }, {, }, {, -}};
  17. //priority_queue<int, vector<int>, less<int>> que;
  18. //next_permutation
  19. ll Mod = ;
  20. class Matrix//定义一个矩阵结构体
  21. {
  22. public:
  23. ll M[][];
  24. clear()
  25. {
  26. mem(M, );
  27. }
  28. init()
  29. {
  30. clear();
  31. for (int i = ; i < ; i++)
  32. {
  33. M[i][i] = ;
  34. }
  35. }
  36. Matrix()//初始化
  37. {
  38. mem(M, );
  39. }
  40. Matrix(ll Arr[][])//用数组来初始化
  41. {
  42. for (int i = ; i < ; i++)
  43. for (int j = ; j < ; j++)
  44. {
  45. M[i][j] = Arr[i][j];
  46. }
  47. }
  48. };
  49. Matrix operator * (Matrix A, Matrix B) //重载乘法运算符
  50. {
  51. Matrix Ans;
  52. Ans.clear();
  53. for (int i = ; i < ; i++)
  54. for (int j = ; j < ; j++)
  55. for (int k = ; k < ; k++)
  56. {
  57. Ans.M[i][j] = (Ans.M[i][j] + 1LL * A.M[i][k] * B.M[k][j] % Mod) % Mod;
  58. }
  59. return Ans;
  60. }
  61. Matrix MFPOW(Matrix now, ll n)
  62. {
  63. Matrix x;
  64. x.init();
  65. while (n)
  66. {
  67. if (n & )
  68. {
  69. x = x * now;
  70. }
  71. now = now * now;
  72. n >>= 1ll;
  73. }
  74. return x;
  75. }
  76. void print(Matrix x)
  77. {
  78. for (int i = ; i < ; i++)
  79. {
  80. for (int j = ; j < ; j++)
  81. {
  82. cout << x.M[i][j] << " ";
  83. }
  84. cout << endl;
  85. }
  86. }
  87. struct block
  88. {
  89. ll where, from, to;
  90. };
  91. ll a[][] = {{, , }, {, , }, {, , }};
  92. ll b[][] = {{, , }, {, , }, {, , }};
  93. ll m;
  94. block now[];
  95. ll num[];
  96. int judge[][];
  97. int check[];
  98. int pop = ;
  99. int cnt = ;
  100. int main()
  101. {
  102. //freopen("sample.txt", "w", stdout);
  103. int n;
  104. cin >> n;
  105. cin >> m;
  106. ll from, to, where;
  107. num[pop++] = , num[pop++] = m;
  108. for (int i = ; i <= n; i++)
  109. {
  110. scanf("%lld %lld %lld", &where, &from, &to);
  111. num[pop++] = from - , num[pop++] = to; //将边界输入以便离散化 注意:如果L要减1以防出现错误
  112. now[i].where = where, now[i].from = from, now[i].to = to;
  113. }
  114. sort(num + , num + pop);
  115. int len = unique(num + , num + pop) - num - ;//因为数列是从下标1开始的所以还要再减去1
  116. for (int i = ; i <= n; i++)
  117. {
  118. int L = lower_bound(num + , num + len + , now[i].from) - num;
  119. int R = lower_bound(num + , num + len + , now[i].to) - num;
  120. judge[now[i].where][L]++;
  121. judge[now[i].where][R + ]--; //因为要用到前缀和来维护每一块的第now[i].where行是否有障碍 所以要[R+1]--
  122. }
  123. Matrix ans(a);
  124. Matrix B(b);
  125. for (int i = ; i <= len; i++)
  126. {
  127. ll lenth = num[i] - num[i - ];
  128. //cout << lenth << endl;
  129. Matrix cur(b);
  130. for (int j = ; j <= ; j++)
  131. {
  132. check[j] += judge[j][i];
  133. if (check[j])
  134. {
  135. cur.M[][j - ] = cur.M[][j - ] = cur.M[][j - ] = ;
  136. }
  137. }
  138. //print(cur);
  139. cur = MFPOW(cur, lenth);
  140. //print(cur);
  141. ans = ans * cur;
  142. //print(ans);
  143. }
  144. cout << ans.M[][] << endl;
  145. return ;
  146. }

G

给你一个N位数的数列代表城墙 每一个城墙上面初始有A[i]个弓箭手 给你一个R 弓箭手可以防守[i-R,i+R]的地方

再给你K(K<=1e18)个弓箭手 问你每个城墙能被防守的最大最小值为多少

先前缀和处理出原始的防守值 然后二分答案用前缀和check是否能满足

  1. #include <bits/stdc++.h>
  2. #define PI acos(-1.0)
  3. #define mem(a,b) memset((a),b,sizeof(a))
  4. #define TS printf("!!!\n")
  5. #define pb push_back
  6. #define inf 1e9
  7. //std::ios::sync_with_stdio(false);
  8. using namespace std;
  9. //priority_queue<int,vector<int>,greater<int>> que; get min
  10. const double eps = 1.0e-10;
  11. const double EPS = 1.0e-4;
  12. typedef pair<int, int> pairint;
  13. typedef long long ll;
  14. typedef unsigned long long ull;
  15. //const int maxn = 3e5 + 10;
  16. const int maxm = ;
  17. const int turn[][] = {{, }, { -, }, {, }, {, -}};
  18. //priority_queue<int, vector<int>, less<int>> que;
  19. //next_permutation
  20. ll pre[];
  21. ll judgepre[];
  22. int n, r;
  23. ll k;
  24. bool judge(ll x)
  25. {
  26. mem(judgepre, );
  27. ll remain = k;
  28. ll need;
  29. ll now = ;
  30. for (int i = ; i <= n; i++)
  31. {
  32. now += judgepre[i];
  33. if (pre[i] + now < x)
  34. {
  35. need = x - pre[i] - now;
  36. remain -= need;
  37. if (remain < )
  38. {
  39. return false;
  40. }
  41. judgepre[i + ] += need;
  42. if (i + * r + <= n)
  43. {
  44. judgepre[i + * r + ] -= need;
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. int main()
  51. {
  52. cin >> n >> r >> k;
  53. ll now;
  54. for (int i = ; i <= n; i++)
  55. {
  56. scanf("%lld", &now);
  57. pre[max(, i - r)] += now;
  58. if (i + r + <= n)
  59. {
  60. pre[i + r + ] -= now;
  61. }
  62. }
  63. for (int i = ; i <= n; i++)
  64. {
  65. pre[i] = pre[i] + pre[i - ];
  66. }
  67. // for(int i=1;i<=n;i++)
  68. // cout<<pre[i]<<" ";
  69. // cout<<endl;
  70. ll l = , r = 2e18 + 2e9;
  71. ll mid;
  72. ll anser;
  73. while (l <= r)
  74. {
  75. mid = (l + r) >> ;
  76. if (judge(mid))
  77. {
  78. anser = mid;
  79. l = mid + ;
  80. }
  81. else
  82. {
  83. r = mid - ;
  84. }
  85. }
  86. cout << anser << endl;
  87. }

Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check的更多相关文章

  1. codeforces 691E 矩阵快速幂+dp

    传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...

  2. Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP

    题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...

  3. CodeForces - 691E Xor-sequences 【矩阵快速幂】

    题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...

  4. P1357 花园 (矩阵快速幂+ DP)

    题意:一个只含字母C和P的环形串 求长度为n且每m个连续字符不含有超过k个C的方案数 m <= 5  n <= 1e15 题解:用一个m位二进制表示状态 转移很好想 但是这个题是用矩阵快速 ...

  5. BZOJ1009 矩阵快速幂+DP+KMP

    Problem 1009. -- [HNOI2008]GT考试 1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: ...

  6. Codeforces 691E Xor-sequences(矩阵快速幂)

    You are given n integers a1,  a2,  ...,  an. A sequence of integers x1,  x2,  ...,  xk is called a & ...

  7. COJ 1208 矩阵快速幂DP

    题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...

  8. Codeforces 989E A Trance of Nightfall 矩阵快速幂+DP

    题意:二维平面上右一点集$S$,共$n$个元素,开始位于平面上任意点$P$,$P$不一定属于$S$,每次操作为选一条至少包含$S$中两个元素和当前位置$P$的直线,每条直线选取概率相同,同一直线上每个 ...

  9. BZOJ1009: [HNOI2008]GT考试 (矩阵快速幂 + DP)

    题意:求一个长度为n的数字字符串 (n <= 1e9) 不出现子串s的方案数 题解:用f i,j表示长度为i匹配到在子串j的答案 用kmp的失配函数预处理一下 然后这个转移每一个都是一样的 所以 ...

随机推荐

  1. mysql数据库修改一行数据格式不成功问题

    举个例子: mysql数据库中有两个字段publication_time.storage_time,我尝试着一个一个的修改字段的状态 #alter table books modify column ...

  2. 使用指定MTU到特定IP

    ping指令使用指定MTU到特定IP 命令如下 45.58.185.18 这里MTU为1300

  3. JS调用服务器端方法

    javascript函数中执行C#代码中的函数:方法一:1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中;        2.在前台写一个js函数,内容为document. ...

  4. 读取yaml中的内容

    def read_yml(path): """ 读取yml文件中的数据 :param path: 文件yaml 的路径 :return: 返回读取yaml文件内的结果 & ...

  5. CSS3—— 分页 框大小 弹性盒子 多媒体查询 多媒体查询实例

    分页 网站有很多个页面,就需要使用分页来为每个页面做导航 点击及鼠标悬停分页样式 圆角样式 悬停过度效果 带边框的分页 分页间隔 分页字体大小 居中分页 面包屑导航 框大小 box-sizing 属性 ...

  6. Django 基于角色的权限控制

    有一种场景, 要求为用户赋予一个角色, 基于角色(比如后管理员,总编, 编辑), 用户拥有相应的权限(比如管理员拥有所有权限, 总编可以增删改查, 编辑只能增改, 有些页面的按钮也只有某些角色才能查看 ...

  7. 【DSP开发】【Linux开发】基于ARM+DSP进行应用开发

    针对当前应用的复杂性,SOC芯片更好能能满足应用和媒体的需求,集成众多接口,用ARM做为应用处理器进行多样化的应用开发和用户界面和接口,利用DSP进行算法加速,特别是媒体的编解码算法加速,既能够保持算 ...

  8. linux 正则表达式 目录

    linux 通配符与正则表达式 linux 通配符 linux 正则表达式 使用grep命令 linux 扩展正则表达式 egrep linux 正则表达式 元字符

  9. Android的Monkey和MonkeyRunner

    本文部分解释性语段摘自网络百科或其它BLOG,语句内容网络随处可见,也不知道谁是初始原创,便不再署名出处,如有雷同,还请见谅. Monkey 什么是Monkey Monkey是Android中的一个命 ...

  10. AcWing 875. 快速幂

    题目链接:https://www.acwing.com/problem/content/description/877/ 快速幂模板题,计算ab mod p 的值,a,b,p大概1e9左右,可以快速计 ...