UVALive 7325 Book Borders

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5347

-------------------------------------------------------------------------------------------------------------

我们可以预处理除长度为L的区间能存下的从第一个单词开始的单词的次数

然后枚举区间长度从a到b 每次从一个区间最后一个单词结尾跳到另一个区间最后一个单词结尾

这样相邻两次跳跃的总长度至少为一个区间长度

设单词总长为$len$ 对于长度为$x$的区间 总的跳跃次数不超 $len\ /\ x * 2$

因此整个问题的复杂度根据调和级数分析发现是 $O(lenlog{len})$

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 500010;
 7 int a[N], pre[N], sum[N];
 8 char s[N];
 9 int len, n;
10 int main()
11 {
12     while(gets(s) != NULL)
13     {
14         n = 0;
15         len = strlen(s) + 1;
16         for(int i = 0; i < len; ++i)
17         {
18             if(s[i] < ‘a‘ || s[i] > ‘z‘)
19             {
20                 ++n;
21                 a[n] = i + 1 - sum[n - 1];
22                 sum[n] = sum[n - 1] + a[n];
23             }
24             pre[i + 1] = n;
25         }
26         int L, R, ans, now;
27         scanf("%d%d", &L, &R);
28         getchar();
29         ++L;
30         ++R;
31         for(int i = L; i <= R; ++i)
32         {
33             ans = -1;
34             now = 0;
35             while(now != len)
36             {
37                 ans += a[pre[now] + 1];
38                 now = sum[pre[min(now + i, len)]];
39             }
40             printf("%d\n", ans);
41         }
42     }
43     return 0;
44 }
时间: 2024-08-02 23:07:50

UVALive 7325 Book Borders的相关文章

Regionals 2015 &gt;&gt; Europe - Central &gt;&gt;7325 - Book Borders【模拟】

Europe - Central >>7325 - Book Borders 题目链接:7325 题目大意:给你一个字符串(含空格),每行x个字符,将单词排列进去,单词不能断开,问每行第一个单词的长度时多少,注意加空格 题目思路:直接模拟.第一个for遍历[a,b],第二个大致为n/a.复杂度大概为nlogn. 开两个数组,v[i]记录i这个位置所属的单词开始位置,e[v[i]]记录第i个位置所属的单词的结束位置. 然后每次判断这一行结尾,所在位置.如果在两个单词中间,则将该单词视为下一行开始

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

UVALive 5545 Glass Beads

Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 554564-bit integer IO format: %lld      Java class name: Main Once upon a time there was a famous actress. As you may expect, she played mostly