感觉这场题目有种似曾相识感觉,C题还没看,日后补上。一定要坚持做下去。

A
Unusual Product

题意:

给定一个n*n的01矩阵,3种操作,

1 i 将第i行翻转

2 i 将第i列翻转

3 询问矩阵第i行和第i列做向量乘法之和。

分析:

分析发现对于3的结果取决于对角线上1的个数num,即num%2,然后就很好做了,对于每次操作,只需要改变该行或该列后,对角线上仍然有多少个1.

代码:

  1. #pragma comment(linker, "/STACK:16777216")
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <queue>
  6. #include <vector>
  7. #include <cmath>
  8. #define inf 0x0f0f0f0f
  9. #define in freopen("data.txt", "r", stdin);
  10. #define pb push_back
  11. #define bug(x) printf("Line : >>>>%d\n", (x));
  12. using namespace std;
  13. typedef unsigned short US;
  14. typedef long long LL;
  15. const int maxn = ;
  16. int vis[maxn];
  17.  
  18. int main(){
  19.  
  20. int n, q;
  21. scanf("%d", &n);
  22. int res = ;
  23. for(int i = ; i< n; i++) for(int j = ; j < n; j++) {
  24. int t;
  25. scanf("%d", &t);
  26. if(i == j) vis[i] = t, res += t;
  27. }
  28. scanf("%d", &q);
  29. while(q--){
  30. int t, x;
  31. scanf("%d", &t);
  32. if(t != ){
  33. scanf("%d", &x);
  34. vis[x] ^= ;
  35. res += (vis[x] ? : -);
  36. }
  37. else{
  38. cout<<res%;
  39. }
  40. }
  41. cout<<endl;
  42. return ;
  43. }

B
Toy Sum

题意:

共有1,2,3到10^6这么些个数首先选出n个数,x1~xn,要从剩下的数中选出若干数满足灯饰sum{xi-1| 1 <= i <= n} = sum {s-yi|1 <= i <= m}

分析:

xi-1和s-yi分别表示的数到两端的距离。

那么对于xi如果离s一样的近的数没有被选中则优先选它,如果两端一样近的地方都选取了,先不管它。最后将能够选取的一样近的数都选取完毕后,再针对两端一样近的数都选了的情况,这时候也只要将两端一样近的从没选中的选进Y集合就行了。

上面这样似乎才是标准解法,可是我是dfs暴力求解,加了些剪枝,也能过。

我的代码:

  1. #pragma comment(linker, "/STACK:16777216")
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <bitset>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <cmath>
  10. #define inf 0x0f0f0f0f
  11. #define in freopen("data.txt", "r", stdin);
  12. #define pb push_back
  13. #define bug(x) printf("Line : >>>>%d\n", (x));
  14. using namespace std;
  15. typedef unsigned short US;
  16. typedef long long LL;
  17. const int maxn = (int)1e6 + ;
  18. LL a[maxn];
  19. bitset<maxn> vis;
  20. LL sum[maxn];
  21. int ok;
  22. vector<int> ans;
  23. int s = (int)1e6;
  24.  
  25. void dfs(int x, LL sm) {
  26. if(ok) return;
  27. if(sm == ) {
  28. ok = ;
  29. return;
  30. }
  31. int k = upper_bound(a+, a+x+, sm) - a;
  32. k--;
  33. for(int i = k; i >= && sum[i] >= sm; i--) {
  34. ans.pb(s-a[i]);
  35. dfs(i-, sm-a[i]);
  36. if(ok) return;
  37. ans.pop_back();
  38. }
  39. }
  40. int main() {
  41.  
  42. int n;
  43. scanf("%d", &n);
  44. LL tmp = ;
  45. for(int i = ; i <= n; i++) {
  46. int u;
  47. scanf("%d", &u);
  48. tmp += u-;
  49. vis[u] = ;
  50. }
  51.  
  52. // cout<<tmp<<endl;
  53. int cnt = ;
  54. for(int i = s; i >= ; i--)
  55. if(vis[i] == ) {
  56. a[++cnt] = s-i;
  57. // if(cnt <= 10)
  58. // cout<<a[cnt]<<endl;
  59. sum[cnt] = sum[cnt-] + s-i;
  60. }
  61. vis.reset();
  62. if(tmp == ) {
  63. cout<< <<endl;
  64. cout<< << endl;
  65. return ;
  66. }
  67. dfs(cnt, tmp);
  68. printf("%d\n", ans.size());
  69.  
  70. for(int i = ; i < ans.size(); i++)
  71. printf("%d%c", ans[i], i == ans.size()- ? '\n' : ' ');
  72. return ;
  73. }

D
Hill Climbing

题意:

每座山用一根垂直线段表示,有x,y两种属性,按照x递增给定n座山的xi和yi,每次在第i座山的时候只能够到达在视野范围内(这样说是没有问题的)的最右的一座山,然后两个人分别位于两座山上,问他们分别出发,沿着上面的原则确定的路线转移,直到两者到达同一座山。

分析:

首先要确定没座山能够到达的最右的一座山,除了最右的山没有可到达的山,每座山显然能到达另一座山,可以从右往左扫,建立单调栈,将第i座山与栈顶的斜率k1,栈顶与栈顶以下的一座山的斜率k2比较,如果k1 < k2将栈顶元素出栈。最后第i座山能够到达栈顶那座山,同时将i入栈。

这样会得到一棵树,对于每个询问,只需要询问他们的lca就是了。

注意:倍增法求lca的时候,应该在dfs子树的时候将fa[u][i]更新求出来。

代码:

  1. #pragma comment(linker, "/STACK:16777216")
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <bitset>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <cmath>
  10. #define inf 0x0f0f0f0f
  11. #define in freopen("data.txt", "r", stdin);
  12. #define pb push_back
  13. #define esp 1e-6
  14. #define bug(x) printf("Line : >>>>%d\n", (x));
  15. using namespace std;
  16. typedef unsigned short US;
  17. typedef long long LL;
  18. const int maxn = (int)1e5 + ;
  19. struct Po {
  20. double x, y;
  21. } h[maxn];
  22. int st[maxn];
  23. vector<int> g[maxn];
  24. double cal(int x, int y) {
  25. return (h[x].y-h[y].y)/(h[x].x-h[y].x);
  26. }
  27. int fa[maxn][], dep[maxn];
  28. double dblcmp(double x) {
  29. if(fabs(x) < esp) return ;
  30. return x > ? : -;
  31. }
  32. void dfs(int u, int f) {
  33. dep[u] = dep[f] + ;
  34. fa[u][] = f;
  35. for(int i = ; i < ; i++) {
  36. fa[u][i] = fa[fa[u][i-]][i-];
  37. }
  38. for(int i = ; i < g[u].size(); i++) {
  39. int v = g[u][i];
  40. if(v == f)
  41. continue;
  42. // cout<<v<<endl;
  43. dfs(v, u);
  44. }
  45. }
  46. int lca(int x, int y) {
  47. if(dep[x] < dep[y]) swap(x, y);
  48. int d = dep[x]-dep[y];
  49. for(int i = ; i < ; i++) if((d &(<<i)))
  50. x = fa[x][i];
  51. if(x != y) {
  52. for(int i = ; i >= ; i--) if(fa[x][i] != fa[y][i])
  53. x = fa[x][i], y = fa[y][i];
  54. x = fa[x][];
  55. }
  56. return x;
  57. }
  58. int main() {
  59.  
  60. int n, q;
  61. scanf("%d", &n);
  62. for(int i = ; i <= n; i++) {
  63. scanf("%lf%lf", &h[i].x, &h[i].y);
  64. }
  65. int top = ;
  66. for(int i = n; i >= ; i--) {
  67. while(top >= && dblcmp(cal(i, st[top]) - cal(st[top], st[top-])) < )
  68. top--;
  69. if(top) {
  70. g[st[top]].pb(i);
  71. g[i].pb(st[top]);
  72. }
  73. st[++top] = i;
  74. }
  75. dfs(n, );
  76. scanf("%d", &q);
  77. for(int i = ; i <= q; i++) {
  78. int u, v;
  79. scanf("%d%d", &u, &v);
  80. printf("%d%c", lca(u, v), i == q ? '\n' : ' ');
  81. }
  82. return ;
  83. }

同样利用单调栈的一道题:HDU 5033 Building

Codeforces Round #238 (Div. 1)的更多相关文章

  1. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  2. Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜

    题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...

  3. Codeforces Round #238 (Div. 2) D. Toy Sum

    D. Toy Sum   time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  4. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

随机推荐

  1. XML DOM操作,适用目前流行的浏览器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. scala学习笔记:函数与方法

    http://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala A Functio ...

  3. OpenWebFlow0.9用户手册与设计说明

    1.    OpenWebFlow概述 OpenWebFlow是基于Activiti扩展的工作流引擎.Activiti (官方网站http://activiti.org/,代码托管在https://g ...

  4. h2database源码浅析:TransactionMap、MVMap、MVStore

    TransactionStore:A store that supports concurrent MVCC read-committed transactions. TransactionStore ...

  5. 使用SqlBulkCopy类批量复制大数据

    using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using Syst ...

  6. eclipse导入包的快捷键

    在Eclipse里,写一个没有导入相应包的类名(这个类名已经完全写全,比如LayoutManager), 可以用ctrl+shift+M/Ctrl+Shift+o/Ctrl+1导入相应的包. 其中Ct ...

  7. Wix: Using Patch Creation Properties - Minor Update

    Based on the project created in Wix: Using Patch Creation Properties - Small Update, Following chang ...

  8. 安卓Fragment和Activity之间的数据通讯

    Fragment是Android3.0之后才推出来的.可以用来做底部菜单,现在很多APP都有用到这个底部菜单.当然TabHost也可以用来做底部菜单,但是Fragment来做,动画效果这些可以做得更炫 ...

  9. 创建线程的两种方式比较Thread VS Runnable

    1.首先来说说创建线程的两种方式 一种方式是继承Thread类,并重写run()方法 public class MyThread extends Thread{ @Override public vo ...

  10. RequireJs 依赖管理使用

    What? 声明不同js文件之间的依赖,可以按需.并行.延时载入js库,可以让我们的代码以模块化的方式组织. When? 对于中大型项目,为了团队成员更好得发挥协同力,各自管理各自的JS代码,按需调用 ...