“玲珑杯”ACM比赛 Round #12题解&源码
我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,把官方给出的题解贴出来!
Time Limit:1s
Memory Limit:128MByte
DESCRIPTION
Niro, a lovely girl, has bought a Galaxy Note 7 and wants to destroy cities. There are N cities numbered 1... N on a line and each pair of adjacent cities has distance 1. Galaxy Note 7 has its explosion radius R. Niro puts her Galaxy Note 7 in city X and city i will be destroyed if (|X−i|≤R)
.You must tell Niro how many cities wil be destroyed.
Each of the following T lines contains three integers N, R, X.
OUTPUT
SAMPLE INPUT
0≤R≤100
1≤X≤N
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
int n,r,x;
int a[];
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
scanf("%d%d%d",&n,&r,&x);
memset(a,,sizeof(a));
int ans=;
if(x<=n)
{
for(int i=x;i<=x+r;i++)
{
if(i<=n)
{
a[i]=;
}
}
for(int i=x;i>=x-r;i--)
{
if(i>=)
{
a[i]=;
}
}
for(int i=;i<=n;i++)
if(a[i])
ans++;
printf("%d\n",ans);
}
}
}
return ;
}
给出官方的STL解法:
#include <cstdio>
#include <algorithm>
int T, N, R, X;
int main()
{
for (scanf("%d", &T); T--; )
{
scanf("%d%d%d", &N, &R, &X);
printf("%d\n", std::min(N, X + R) - std::max(, X - R) + );
}
return ;
}
题目链接:http://www.ifrog.cc/acm/problem/1107?contest=1014&no=1
题解:
下面给出AC代码:
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
const int INF = ;
class Heap
{
private :
std::priority_queue < int, std::vector < int >, std::greater < int > > inc, dec;
void BaseClear()
{
while (!dec.empty() && inc.top() == dec.top())
{
inc.pop();
dec.pop();
}
}
public :
int top()
{
BaseClear();
return inc.top();
}
void del(int x)
{
dec.push(x);
}
void push(int x)
{
inc.push(x);
}
void clear()
{
while (!inc.empty())
inc.pop();
while (!dec.empty())
dec.pop();
}
bool empty()
{
BaseClear();
return inc.empty();
}
}
Q0, Q1;
int TC, f0[], f1[], *F0 = f0 + , *F1 = f1 + , N, C0, C1, N0, N1, E0, E1, TAG0, TAG1;
void forward(char option)
{
if (option == '')
{
F0--;
F0[] = (Q1.empty() ? INF : Q1.top()) + TAG1 - TAG0;
E0++;
Q0.push(F0[]);
while (E0 >= N0)
Q0.del(F0[E0--]);
E1 = ;
Q1.clear();
}
else if (option == '')
{
F1--;
F1[] = (Q0.empty() ? INF : Q0.top()) + TAG0 - TAG1;
E1++;
Q1.push(F1[]);
while (E1 >= N1)
Q1.del(F1[E1--]);
E0 = ;
Q0.clear();
}
else
{
F0--;
F0[] = (Q1.empty() ? INF : Q1.top()) + TAG1 - TAG0;
E0++;
F1--;
F1[] = (Q0.empty() ? INF : Q0.top()) + TAG0 - TAG1;
E1++;
Q0.push(F0[]);
Q1.push(F1[]);
while (E0 >= N0)
Q0.del(F0[E0--]);
while (E1 >= N1)
Q1.del(F1[E1--]);
TAG0 += C0;
TAG1 += C1;
}
}
int main()
{
for (scanf("%d", &TC); TC--; )
{
F0 = f0 + ;
F1 = f1 + ;
TAG0 = TAG1 = ;
Q0.clear();
Q1.clear();
E0 = E1 = ;
scanf("%d%d%d%d%d", &N, &C0, &C1, &N0, &N1);
char c = getchar();
while (c != '' && c != '' && c != '?')
c = getchar();
if (c == '')
{
F0[E0 = ] = ;
Q0.push();
}
else if (c == '')
{
F1[E1 = ] = ;
Q1.push();
}
else
{
F0[E0 = ] = C0;
F1[E1 = ] = C1;
Q0.push(C0);
Q1.push(C1);
}
for (int i = ; i < N; i++)
forward(getchar());
int ans = ;
if (!Q0.empty())
ans = std::min(ans, Q0.top() + TAG0);
if (!Q1.empty())
ans = std::min(ans, Q1.top() + TAG1);
printf("%d\n", ans);
}
return ;
}
题目链接:http://www.ifrog.cc/acm/problem/1108?contest=1014&no=2
题解:
下面给出AC代码:
#include <bits/stdc++.h>
const int MOD = ;
int F[], N, G, a[], w[];
int gcd(int x, int y)
{
int r;
while (y)
{
r = x % y;
x = y;
y = r;
}
return x;
}
void DP(int x, int y)
{
std::vector < int > Div;
for (int i = ; i * i <= x; i++)
if (x % i == )
{
Div.push_back(i);
if (i * i < x)
Div.push_back(x / i);
}
std::sort(Div.begin(), Div.end());
int L = Div.size();
std::vector < int > Use(L, );
for (int i = L - ; ~i; i--)
{
Use[i] = y / Div[i];
for (int j = i + ; j < L; j++)
if (Div[j] % Div[i] == )
Use[i] -= Use[j];
}
for (int i = G; ~i; i--)
{
F[i] = ;
for (int j = ; j < L && Div[j] <= i; j++)
F[i] = (F[i] + (long long)F[i - Div[j]] * Use[j]) % MOD;
}
}
int main()
{
scanf("%d%d", &N, &G);
for (int i = ; i < N; i++)
scanf("%d", a + i);
for (int i = ; i < N; i++)
scanf("%d", w + i);
F[] = ;
for (int i = ; i < N; i++)
DP(a[i], w[i]);
printf("%d\n", F[G]);
return ;
}
题目链接:http://www.ifrog.cc/acm/problem/1109?contest=1014&no=3
题解:
下面给出AC代码:
#include <cstdio>
const long long MOD = ;
long long POWER(long long a, long long b)
{
long long r = ;
for (; b; b >>= )
{
if (b & )
r = r * a % MOD;
a = a * a % MOD;
}
return r;
}
long long N;
int T;
int main()
{
for (scanf("%d", &T); T--; )
{
scanf("%lld", &N);
long long F = POWER(, N - ) * - POWER(, N - ) * ;
long long G = POWER(, N - ) * (((N % MOD * ) - ) % MOD) + POWER(, N - ) * (((N % MOD * ) + ) % MOD);
G %= MOD;
F %= MOD;
G %= MOD;
F += MOD;
G += MOD;
F %= MOD;
G %= MOD;
if (G & )
G += MOD;
G >>= ;
printf("%lld %lld\n", F, G);
}
return ;
}
题目链接:http://www.ifrog.cc/acm/problem/1110?contest=1014&no=4
题解:
下面给出AC代码:
#include <cstdio>
#include <vector>
#include <algorithm>
std::vector < int > E[], col[];
std::vector < std::pair < int, int > > inc[], dec[];
int N, q[], left[], right[], size[], BeiZeng[][], *fa = BeiZeng[], LOG; // left : DFN; right maximum DFN in its subtree
std::vector < int >::iterator ue[];
void DFS()
{
int D = , TIME = ;
q[] = ;
ue[] = E[].begin();
left[] = right[] = ;
while (D)
{
if (ue[D] != E[q[D]].end() && *ue[D] == fa[q[D]])
ue[D]++;
if (ue[D] != E[q[D]].end())
{
int To = *ue[D]++;
fa[To] = q[D];
left[To] = right[To] = ++TIME;
q[++D] = To;
ue[D] = E[To].begin();
}
else
{
if (D > )
right[q[D - ]] = right[q[D]];
D--;
}
}
for (int i = ; i <= N; i++)
size[i] = right[i] - left[i] + ;
while ( << LOG < N)
LOG++;
for (int i = ; i <= LOG; i++)
for (int j = ; j <= N; j++)
BeiZeng[i][j] = BeiZeng[i - ][BeiZeng[i - ][j]];
}
int lowest(int u, int v)
{
for (int i = LOG; ~i; i--)
if (BeiZeng[i][u] && size[BeiZeng[i][u]] < size[v])
u = BeiZeng[i][u];
return u;
}
inline void bar(int u, int d, int l, int r)
{
inc[u].push_back(std::make_pair(l, r));
if (d < N)
dec[d + ].push_back(std::make_pair(l, r));
}
void conflict(int u, int v)
{
if (size[u] < size[v])
std::swap(u, v);
if (left[u] <= left[v] && right[v] <= right[u]) // u is v's ancestor
{
int lw = lowest(v, u);
if (left[lw] > )
{
bar(left[v], right[v], , left[lw] - );
bar(, left[lw] - , left[v], right[v]);
}
if (right[lw] < N)
{
bar(left[v], right[v], right[lw] + , N);
bar(right[lw] + , N, left[v], right[v]);
}
}
else
{
bar(left[u], right[u], left[v], right[v]);
bar(left[v], right[v], left[u], right[u]);
}
}
int MIN[], TAG[], NUM[]; // NUM[] : the number of elements which reach MIN[]
void INC(int p, int l, int r, int L, int R, int w)
{
if (L <= l && r <= R)
{
MIN[p] += w;
TAG[p] += w;
return;
}
if (TAG[p])
{
MIN[p + p] += TAG[p];
MIN[p + p + ] += TAG[p];
TAG[p + p] += TAG[p];
TAG[p + p + ] += TAG[p];
TAG[p] = ;
}
int m = (l + r) >> ;
if (L <= m)
INC(p + p, l, m, L, R, w);
if (R > m)
INC(p + p + , m + , r, L, R, w);
MIN[p] = std::min(MIN[p + p], MIN[p + p + ]);
NUM[p] = (MIN[p + p] == MIN[p] ? NUM[p + p] : ) + (MIN[p + p + ] == MIN[p] ? NUM[p + p + ] : );
}
inline int ZERONUM()
{
return MIN[] == ? NUM[] : ;
}
long long ANS;
void Treeinit(int p = , int l = , int r = N)
{
NUM[p] = r - l + ;
if (l < r)
{
int m = (l + r) >> ;
Treeinit(p + p, l, m);
Treeinit(p + p + , m + , r);
}
}
int main()
{
scanf("%d", &N);
for (int i = , u, v; i < N; i++)
{
scanf("%d%d", &u, &v);
E[u].push_back(v);
E[v].push_back(u);
}
for (int i = , c; i <= N; i++)
{
scanf("%d", &c);
col[c].push_back(i);
}
DFS();
for (int i = ; i <= N; i++)
for (std::vector < int >::iterator x = col[i].begin(); x != col[i].end(); x++)
for (std::vector < int >::iterator y = x + ; y != col[i].end(); y++)
conflict(*x, *y);
Treeinit();
for (int i = ; i <= N; i++)
{
for (std::vector < std::pair < int, int > >::iterator j = inc[i].begin(); j != inc[i].end(); j++)
INC(, , N, j -> first, j -> second, );
for (std::vector < std::pair < int, int > >::iterator j = dec[i].begin(); j != dec[i].end(); j++)
INC(, , N, j -> first, j -> second, -);
ANS += ZERONUM();
}
printf("%lld\n", (ANS - N) >> );
return ;
}
“玲珑杯”ACM比赛 Round #12题解&源码的更多相关文章
- “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】
A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...
- “玲珑杯”ACM比赛 Round #13 题解&源码
A 题目链接:http://www.ifrog.cc/acm/problem/1111 分析:容易发现本题就是排序不等式, 将A数组与B数组分别排序之后, 答案即N∑i=1Ai×Bi 此题有坑,反正据 ...
- “玲珑杯”ACM比赛 Round #1 题解
A:DESCRIPTION Eric has an array of integers a1,a2,...,ana1,a2,...,an. Every time, he can choose a co ...
- “玲珑杯”ACM比赛 Round #12 (D) 【矩阵快速幂的时间优化】
//首先,感谢Q巨 题目链接 定义状态向量b[6] b[0]:三面临红色的蓝色三角形个数 b[1]:两面临红色且一面临空的蓝色三角形个数 b[2]:一面临红色且两面临空的蓝色三角形个数 b[3]:三面 ...
- “玲珑杯”ACM比赛 Round #18
“玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...
- “玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)
“玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-0 ...
- “玲珑杯”ACM比赛 Round #1
Start Time:2016-08-20 13:00:00 End Time:2016-08-20 18:00:00 Refresh Time:2017-11-12 19:51:52 Public ...
- “玲珑杯”ACM比赛 Round #18--最后你还是AK了(搜索+思维)
题目链接 DESCRIPTION INPUT OUTPUT SAMPLE INPUT 1 4 2 1 2 5 2 3 5 3 4 5 5 5 SAMPLE OUTPUT 35 HINT 对于样例, ...
- “玲珑杯”ACM比赛 Round #18---图论你先敲完模板(DP+思维)
题目链接 DESCRIPTION INPUT OUTPUT SAMPLE INPUT 2 3 2 3 5 7 3 10 3 5 7 SAMPLE OUTPUT 12 26 HINT 官方题解: 代码如 ...
随机推荐
- HDU5692(线段树+dfs序)
Snacks Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- TCP/IP协议族(一) HTTP简介、请求方法与响应状态码
接下来想系统的回顾一下TCP/IP协议族的相关东西,当然这些东西大部分是在大学的时候学过的,但是那句话,基础的东西还是要不时的回顾回顾的.接下来的几篇博客都是关于TCP/IP协议族的,本篇博客就先简单 ...
- iOS-直播开发(开发从底层做起)
一直在忙, 也没写过几次播客! 但一直热衷于直播开发技术, 公司又不是直播方向的, 所以就年前忙里偷袭研究了一下直播开发, 然后翻阅了很多大神的技术博客等, 写了一个简单的Demo, 又根据网上大神们 ...
- 深圳尚学堂:Web程序员应该会的知识
互联网的行业里涌入了很多的程序员, 都在为互联网的发展添砖加瓦.程序员可以分为很多种,像Unix程序员.Windows程序员,或是C++程序员.Delphi程序员,等等.今天我们谈谈Web程序员,一名 ...
- .Net程序员学用Oracle系列(11):系统函数(下)
1.聚合函数 1.1.COUNT 函数 1.2.SUM 函数 1.3.MAX 函数 1.4.MIN 函数 1.5.AVG 函数 2.ROWNUM 函数 2.1.ROWNUM 函数简介 2.2.利用 R ...
- Activity启动过程分析
Android的四大组件中除了BroadCastReceiver以外,其他三种组件都必须在AndroidManifest中注册,对于BroadCastReceiver来说,它既可以在AndroidMa ...
- Win32/MFC/COM学习推荐书籍
以前有不少朋友问关于学习各种技术的推荐书籍的问题,这里把我觉得比较好的一些书籍列一下,希望能起到抛砖引玉的作用就好了:) Win32开发 Programming Windows by Charles ...
- HTML学习笔记四
今日学习内容为: 1.文本域操作 <html> <head> <meta charset="utf-8"/> <title>Java ...
- BZOJ 1835: [ZJOI2010]base 基站选址(DP,线段树)
可以很容易的写出dp方程: F[i][j]=min(F[l][j-1]+w[l][i])+c[i] (w[i][j]是从l+1到i-1这些点p里,所有满足d[p]+s[p]<d[i] & ...
- [原创.数据可视化系列之十三]idw反距离权重插值算法的javascript代码实现
图形渲染中,idw反距离权重插值算法是一个应用非常广泛的方法,但是js实现的比较少,目前实现一个: //idw算法 //输入[[x:0,y:0,v:0],[x:0,y:0,v:0],[x:0,y:0, ...