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

A
Unusual Product

题意:

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

1 i 将第i行翻转

2 i 将第i列翻转

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

分析:

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

代码:

 #pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#define inf 0x0f0f0f0f
#define in freopen("data.txt", "r", stdin);
#define pb push_back
#define bug(x) printf("Line : >>>>%d\n", (x));
using namespace std;
typedef unsigned short US;
typedef long long LL;
const int maxn = ;
int vis[maxn]; int main(){ int n, q;
scanf("%d", &n);
int res = ;
for(int i = ; i< n; i++) for(int j = ; j < n; j++) {
int t;
scanf("%d", &t);
if(i == j) vis[i] = t, res += t;
}
scanf("%d", &q);
while(q--){
int t, x;
scanf("%d", &t);
if(t != ){
scanf("%d", &x);
vis[x] ^= ;
res += (vis[x] ? : -);
}
else{
cout<<res%;
}
}
cout<<endl;
return ;
}

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暴力求解,加了些剪枝,也能过。

我的代码:

 #pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <bitset>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define inf 0x0f0f0f0f
#define in freopen("data.txt", "r", stdin);
#define pb push_back
#define bug(x) printf("Line : >>>>%d\n", (x));
using namespace std;
typedef unsigned short US;
typedef long long LL;
const int maxn = (int)1e6 + ;
LL a[maxn];
bitset<maxn> vis;
LL sum[maxn];
int ok;
vector<int> ans;
int s = (int)1e6; void dfs(int x, LL sm) {
if(ok) return;
if(sm == ) {
ok = ;
return;
}
int k = upper_bound(a+, a+x+, sm) - a;
k--;
for(int i = k; i >= && sum[i] >= sm; i--) {
ans.pb(s-a[i]);
dfs(i-, sm-a[i]);
if(ok) return;
ans.pop_back();
}
}
int main() { int n;
scanf("%d", &n);
LL tmp = ;
for(int i = ; i <= n; i++) {
int u;
scanf("%d", &u);
tmp += u-;
vis[u] = ;
} // cout<<tmp<<endl;
int cnt = ;
for(int i = s; i >= ; i--)
if(vis[i] == ) {
a[++cnt] = s-i;
// if(cnt <= 10)
// cout<<a[cnt]<<endl;
sum[cnt] = sum[cnt-] + s-i;
}
vis.reset();
if(tmp == ) {
cout<< <<endl;
cout<< << endl;
return ;
}
dfs(cnt, tmp);
printf("%d\n", ans.size()); for(int i = ; i < ans.size(); i++)
printf("%d%c", ans[i], i == ans.size()- ? '\n' : ' ');
return ;
}

D
Hill Climbing

题意:

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

分析:

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

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

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

代码:

#pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <bitset>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define inf 0x0f0f0f0f
#define in freopen("data.txt", "r", stdin);
#define pb push_back
#define esp 1e-6
#define bug(x) printf("Line : >>>>%d\n", (x));
using namespace std;
typedef unsigned short US;
typedef long long LL;
const int maxn = (int)1e5 + ;
struct Po {
double x, y;
} h[maxn];
int st[maxn];
vector<int> g[maxn];
double cal(int x, int y) {
return (h[x].y-h[y].y)/(h[x].x-h[y].x);
}
int fa[maxn][], dep[maxn];
double dblcmp(double x) {
if(fabs(x) < esp) return ;
return x > ? : -;
}
void dfs(int u, int f) {
dep[u] = dep[f] + ;
fa[u][] = f;
for(int i = ; i < ; i++) {
fa[u][i] = fa[fa[u][i-]][i-];
}
for(int i = ; i < g[u].size(); i++) {
int v = g[u][i];
if(v == f)
continue;
// cout<<v<<endl;
dfs(v, u);
}
}
int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
int d = dep[x]-dep[y];
for(int i = ; i < ; i++) if((d &(<<i)))
x = fa[x][i];
if(x != y) {
for(int i = ; i >= ; i--) if(fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
x = fa[x][];
}
return x;
}
int main() { int n, q;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%lf%lf", &h[i].x, &h[i].y);
}
int top = ;
for(int i = n; i >= ; i--) {
while(top >= && dblcmp(cal(i, st[top]) - cal(st[top], st[top-])) < )
top--;
if(top) {
g[st[top]].pb(i);
g[i].pb(st[top]);
}
st[++top] = i;
}
dfs(n, );
scanf("%d", &q);
for(int i = ; i <= q; i++) {
int u, v;
scanf("%d%d", &u, &v);
printf("%d%c", lca(u, v), i == q ? '\n' : ' ');
}
return ;
}

同样利用单调栈的一道题: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. 月半小夜曲下的畅想--DOCTYPE模式

    月半小夜曲下的畅想--DOCTYPE模式 @(css3 box-sizing)[doctype声明|quirks模式|妙瞳] DOCTYPE文档类型标签,该标签是将特定的标准通用标记语言或者XML文档 ...

  2. hadoop vs spark

    http://www.zhihu.com/question/26568496#answer-12035815 Hadoop首先看一下Hadoop解决了什么问题,Hadoop就是解决了大数据(大到一台计 ...

  3. ASP获取当前页面带参数的网址(URL地址)的方法

    '获取当前Url参数的函数 Function GetUrl() Dim ScriptAddress,Servername,qs ScriptAddress = CStr(Request.ServerV ...

  4. 20160503-spring入门1

    一.Spring是什么 Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发. IOC 控制反转  publ ...

  5. 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端

       总结:    重建二叉树:其实就是根据前序和中序重建得到二叉树,得到后续,只要输出那边设置输出顺序即可 [编程题]重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的 ...

  6. jfinal取消默认跳转到view.jsp页面的方法

    今天为了在一个列表中添加一个删除的方法,直接在方法里面谢了一个dao.del();方法,但是调用的时候却出现404错误. 然后就写了一句下面的代码 redirect("/api/listMe ...

  7. HTML5 Canvas 绘制时钟

    网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas ...

  8. CruiseControl.NET : svnrevisionlabeller

    How to use svnrevisionlabeller as your labeller type in CC.NET: 1. Download ccnet.SvnRevisionLabelle ...

  9. 几个css的小知识点(一)

    1.对于不能确定宽度的div让它水平居中. <div class='father'> <div class='son'>居中</div> </div> ...

  10. 电脑升级完Xcode8后 注释快捷键无效的问题

    1.部分电脑升级完Xcode8 后直接重启电脑就可以使用Command +/ 快捷键注释代码, 2.如果上述方法没有效果,可以在终端输入sudo /usr/libexec/xpccachectl  然 ...