清北考前刷题day2早安



/*
做法一:按h sort一遍,对于一段区间[i,j],高度花费就是h[j]-h[i]
然后枚举区间,把区间内C排序,一个一个尽量选即可。
n^3logn
标算:n^3 dp
高度排序,保证从前往后调。
f[i][j]表示当前在第i栋楼,已经跳了j次楼的最小话费。转移枚举下一次跳那座楼。
f[k][j+1]=min(f[i][j],f[i][j]+h[k]-h[i]+c[k]);最后枚举f[i][j]<T,ans=max(ans,j)。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define N 510 using namespace std;
int T,n,ans,cnt;
int f[N][N];//当前处于第i座楼房已经跳了j次楼最小花费
struct node{
int h,c;
bool operator < (const node a) const{
return h<a.h;
} }L[N]; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int main()
{
n=read();
for(int i=;i<=n;i++) L[i].c=read();
for(int i=;i<=n;i++) L[i].h=read();
T=read();
sort(L+,L+n+); memset(f,/,sizeof f);
f[][]=f[][]=L[].c;
for(int i=;i<=n;i++)
for(int j=;j<=n+;j++)
for(int k=i+;k<=n;k++)
f[k][j+]=min(f[k][j+],f[i][j]+L[k].h-L[i].h+L[k].c); for(int i=;i<=n;i++)
for(int j=;j<=n+;j++)
if(f[i][j]<=T) ans=max(ans,j);
printf("%d\n",ans);
return ; }



/*
考虑最后要求的数 a1,a2,..an假定从小到大
给定了b1,b2......bn 从小到大排序
性质一:a1+a2=b1
性质二:a1+a3=b2
假设a2+a3=x,可以解出a1,a2,a3,并可以在b中b1,b2,x删除。则剩下的最小的一定是a1+a4。由于a1已知,a4可解。这个过程可以反复下去。
枚举a2+a3=b里面哪个数就好
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm> using namespace std; const int N=; int n,m,res[N],ans[N][N],z[N*N],cnt; bool use[N*N]; void check(int p)
{
memset(use,false,sizeof(use));
if ((z[]+z[]+z[p])&) return;
res[]=(z[]+z[]+z[p])/-z[p];
res[]=z[]-res[];
res[]=z[]-res[];
use[]=use[]=use[p]=true;
for (int a=,b=;a<=n;a++)
{
while (b<=m && use[b]) b++;
if (b>m) return;
res[a]=z[b]-res[];
use[b]=true;
for (int c=;c<a;c++)
{
if (res[c]>res[a]) return;
int v=res[c]+res[a];
int p=lower_bound(z+,z+m+,v)-z;
if (z[p]!=v) return;
int px=p;
while (px && z[px]==z[p]) px--;
px++;
while (px<=m && z[px]==z[p] && use[px]) px++;
if (z[px]!=z[p] || use[px]) return;
p=px; use[p]=true;
}
}
cnt++;
for (int a=;a<=n;a++) ans[cnt][a]=res[a];
} int main()
{
freopen("city.in","r",stdin);
freopen("city.out","w",stdout);
scanf("%d",&n);
m=n*(n-)/;
for (int a=;a<=m;a++) scanf("%d",&z[a]);
sort(z+,z+m+);
for (int a=;a<=m;)
{
check(a);int b=a;
while (b<=m && z[b]==z[a])b++;
a=b;
}
printf("%d\n",cnt);
for (int a=;a<=cnt;a++)
for (int b=;b<=n;b++)
{
printf("%d",ans[a][b]);
if (b==n) printf("\n");
else printf(" ");
}
return ;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm> #define N 100007 using namespace std;
int n,m,ans,c,p,v;
int a[N];
vector<int>sum[N];
struct ask{
int l,r,v,p;
}A[N]; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int main()
{
freopen("light.in","r",stdin);
freopen("light.out","w",stdout);
int l,r;
n=read();m=read();
for(int i=;i<=n;i++) a[i]=read();
if(n<=)
{
for(int i=;i<=m;i++)
{
ans=;
l=read();r=read();p=read();v=read();
for(int j=l;j<=r;j++) if(a[j]%p==v) ans++;
printf("%d\n",ans);
}
}
else
{
for(int i=;i<=m;i++)
{
A[i].l=read();A[i].r=read();
A[i].p=read();A[i].v=read();
}
for(int i=;i<=n;i++) a[i]%=A[].p;
for(int i=;i<=n;i++)
sum[a[i]].push_back(i);
for(int i=;i<=m;i++)
{
printf ("%d\n",upper_bound(sum[A[i].v].begin (), sum[A[i].v].end (), A[i].r)
-lower_bound (sum[A[i].v].begin (), sum[A[i].v].end (), A[i].l));
}
}
return ;
}
60暴力
链表?!!wt?!
可以对p分块 。
p在[1,10^4+1]。如果枚举p预处理每个数mod p扔到vector里。O(np) TLE
可以把p在[1,100]里预处理,vector里二分即可。
p>100时,只有v+p,v+2p...v+kp能对答案造成贡献。一定有v+kp<=10^4,p>100-->k<=100。所以每次询问都最多有100个数对答案造成贡献,可以vector预处理,枚举这100个v+jp,vector里二分。
复杂度n*sqrt(n)*lgn。
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = ;
const int maxv = ;
const int bsz = ;
const int maxb = ; int n, m;
int a[maxn], vb[maxb][maxb], ve[maxb][maxb];
int xb[maxn], xe[maxn];
int i_buf[maxn * maxb * ], tib; void pre() {
memset(ve, , sizeof(ve));
memset(xe, , sizeof(xe));
for (int i = ; i <= n; ++ i)
++ xe[a[i]];
for (int i = ; i <= maxv; ++ i) {
xb[i] = tib;
tib += xe[i];
xe[i] = xb[i];
}
for (int i = ; i <= n; ++ i)
i_buf[xe[a[i]] ++] = i;
for (int m = ; m <= bsz; ++ m) {
for (int i = ; i <= n; ++ i)
++ ve[m][a[i] % m];
for (int i = ; i < m; ++ i) {
vb[m][i] = tib;
tib += ve[m][i];
ve[m][i] = vb[m][i];
}
for (int i = ; i <= n; ++ i)
i_buf[ve[m][a[i] % m] ++] = i;
}
} int queryb(int l0, int r0, int p, int k) {
if (vb[p][k] == ve[p][k])
return ;
int *x1 = lower_bound(i_buf + vb[p][k], i_buf + ve[p][k], l0);
int *x2 = upper_bound(i_buf + vb[p][k], i_buf + ve[p][k], r0);
return x2 - x1;
} int querys(int v, int l0, int r0) {
if (xb[v] == xe[v])
return ;
int *x1 = lower_bound(i_buf + xb[v], i_buf + xe[v], l0);
int *x2 = upper_bound(i_buf + xb[v], i_buf + xe[v], r0);
return x2 - x1;
} int querya(int l0, int r0, int p, int k) {
int ans = ;
for (int i = k; i <= maxv; i += p)
ans += querys(i, l0, r0);
return ans;
} int main() {
freopen("light.in", "r", stdin);
freopen("light.out", "w", stdout); scanf("%d%d", &n, &m);
tib = ;
for (int i = ; i <= n; ++ i)
scanf("%d", a + i);
pre();
while (m --) {
int l, r, p, k;
scanf("%d%d%d%d", &l, &r, &p, &k);
if (p <= bsz)
printf("%d\n", queryb(l, r, p, k));
else
printf("%d\n", querya(l, r, p, k));
}
}
清北考前刷题day2早安的更多相关文章
- 清北考前刷题day4早安
LI /* 没有考虑次大值有大于一个的情况 */ #include<iostream> #include<cstdio> #include<cstring> # ...
- 清北考前刷题day7早安
- 清北考前刷题day6早安
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- 清北考前刷题day5早安
/* C(n,k) */ #include<iostream> #include<cstdio> #include<cstring> #define ll long ...
- 清北考前刷题day2下午好
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #defin ...
- 清北考前刷题day1早安
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...
- 清北考前刷题day1下午好
水题(water) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每张牌长和宽 ...
- 清北考前刷题da7下午好
三向城 /* 原图一定是一棵完全二叉树. 根节点是x,左节点是x*2,右节点是x*2+1 转化为二进制往左右走就很明显了. */ #include<iostream> #include&l ...
- 清北考前刷题day6下午好
/* 贪心 负数一定不取 枚举最高位是1 且答案取为0的 位置, 更新答案. */ #include<iostream> #include<cstdio> #include&l ...
随机推荐
- [luoguP1494] 岳麓山上打水 && [luoguP2744] [USACO5.3]量取牛奶Milk Measuring
传送门 传送门 dfs选取集合,dp背包判断 虽然我觉的会TLE.. 但是的确是AC了 #include <cstdio> #include <cstring> #includ ...
- UVALive 6430 (水dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Codeforces Round #259 (Div. 2) D
D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...
- bzoj4504 k个串 kstring 可持久化线段树 (标记永久化)
[fjwc2015]k个串 kstring [题目描述] 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只 ...
- Floyd算法——保存路径——输出路径 HDU1385
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1385 参考 http://blog.csdn.net/shuangde800/article/deta ...
- JVM 总结
面试 java 虚拟机 jvm 基础 jvm Write Once Run EveryWhere >jar 包可以在任何兼容jvm上运行 >jvm 适配器 屏蔽掉底层差异 >内存管理 ...
- Strongly connected-HDU4635
Problem - 4635 http://acm.hdu.edu.cn/showproblem.php?pid=4635 题目大意: n个点,m条边,求最多再加几条边,然后这个图不是强连通 分析: ...
- Ubuntu 16.04安装SwitchHosts
下载: https://github.com/oldj/SwitchHosts/releases 解压: unzip SwitchHosts-linux-x64_v3.3.6.5287.zip 移动: ...
- Hive之侧视图(Lateral View)
Lateral View和UDTF类功能函数一起使用,表中的每一行和UDTF函数输出的每一行进行连接,生成一张新的虚拟表,可以对UDTF产生的记录设置字段名称,新加的字段可以使用在sort by,gr ...
- Kettle循环删除数据
1.问题描述: 某个系统原库的数据同步到备份库.但是由于原库的的数据会物理删除,此时需要删除备份库的数据. 2.不理想的解决1: 1)首先从备份库获取该表的所有ID: 2)循环备份库的ID,去原库检测 ...
