文章目录
- [A:小林找工作 35/183](http://acm.nefu.edu.cn/problemShow.php?problem_id=2223)
- [B:xx的树 15/107](http://acm.nefu.edu.cn/problemShow.php?problem_id=2225)
- [C:xx玩游戏 35/111](http://acm.nefu.edu.cn/problemShow.php?problem_id=2226)
- [E:qyh的签到题 7/23](http://acm.nefu.edu.cn/problemShow.php?problem_id=2224)
- [G:天哥的序列 18/147](http://acm.nefu.edu.cn/problemShow.php?problem_id=2232)
- [H:xx的水题 6/9](http://acm.nefu.edu.cn/problemShow.php?problem_id=2227)
A:小林找工作 35/183
这题直接用二分查找出里目标位置最近的点(先排序,再lower_bound(a,a+n,x)从数组a首元素到n-1元素位置找第一个大于x的元素的地址,若没有则返回n位置的地址;这里我们用pos表示,并将其和pos-1位置的元素值比较)
同时由于是long long 的大小,不能用abs绝对值,所以我们可以直接加成正数。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
ll p[N],q[N];
int n,m;
ll ans;
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%lld",&p[i]),p[i]+=1000000000;
for(int i=0;i<m;i++)
scanf("%lld",&q[i]),q[i]+=1000000000;
sort(p,p+n);
for(int i=0;i<m;i++)
{
int pos1=lower_bound(p,p+n,q[i])-p;
int pos2=pos1-1;
if(pos1>=n)
pos1=pos2;
ll t1=p[pos1]-q[i],t2=q[i]-p[pos2];
if(t1<0)
t1=t1*-1;
if(t2<0)
t2=t2*-1;
if(t1<t2)
ans=t1;
else
ans=t2;
printf("%lld\n",ans);
}
return 0;
}
B:xx的树 15/107
这题当时想用树链剖分的板子,不过都被G题自己脑瘫卡到结束以至于没时间了。。。。
建一个无向图,dfs:子节点权值+=父节点权值
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
int e[N], ne[N], h[N], idx;
int n, m;
ll a[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int x, int fa)
{
for (int i = h[x]; i != -1; i = ne[i])
{
int val = e[i];//取子节点
if (val == fa)//特判
continue;
a[val] += a[x];
dfs(val, x);
}
}
int main()
{
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
for (int i = 1; i <= m; i++)//父节点的值
{
int x, y;
scanf("%d%d", &x, &y);
a[x] += y;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
(i == 1) ? printf("%lld", a[i]) : printf(" %lld", a[i]);
printf("\n");
//system("pause");
return 0;
}
C:xx玩游戏 35/111
这题比较简单,用DP的思想在纸上推一下就能发现规律:
只有一个格子的情况xx必输,那么只要能够能直接到达必输格子的格子xx必赢;
同理,如果一个格子周围全是(题目的三个方向)全是必赢的格子,那么这个格子必输。
通过找规律可发现:
n 为偶数时 ans=n*(n/2)/2+(1+n/2)(n/2)/2 n
为奇数时 ans=(n+1)(n/2)/2+(1+n/2)*(n/2)/2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,ans;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
ll tot=(1+n)*n/2;
ll ou,ji;
if(n%2==0)
{
ou=(n/2+1)*(n/2)/2;
ans=tot-ou;
}
else
{
ji=(n/2+2)*(n/2+1)/2;
ans=tot-ji;
}
printf("%lld\n",ans);
}
return 0;
}
E:qyh的签到题 7/23
这是一道差分题,我忘了。。。。
定义三个查分数组,用于1 2 3操作:
区间都+1简单,差分数组+1就行;
区间都+1,2,3,…,k,推演一下是差分数组+1,求两遍前缀和;
至于区间+1,4,9,…,k^2,塔们说推出来是代码写的那样=-=
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, mod = 1e9 + 7;
int n, m;
ll d1[N], d2[N], d3[N];
void pre_sum(ll d[])//求前缀和
{
for (int i = 1; i <= n; i++)
d[i] = (d[i] + d[i - 1]) % mod;
}
int main()
{
scanf("%d%d", &n, &m);
while (m--)
{
int type, pos;
scanf("%d%d", &type, &pos);
if (type == 1)
d1[pos]++;
else if (type == 2)
d2[pos]++;
else
d3[pos]++, d3[pos + 1]++;//+k^2的操作
}
pre_sum(d1);
pre_sum(d2);
pre_sum(d2);
pre_sum(d3);
pre_sum(d3);
pre_sum(d3);
for (int i = 1; i <= n; i++)//注意这里输出相加时还要再取模一次!
(i == n) ? printf("%lld\n", (d1[i] + d2[i] + d3[i]) % mod) : printf("%lld ", (d1[i] + d2[i] + d3[i]) % mod);
//system("pause");
return 0;
}
G:天哥的序列 18/147
这题我心态炸了啊!
从18.30WA到21点比赛结束,究其原因竟然是掉了一句sort!(我以为我sort了……)
我说咋特例都能过咋就是WA/捂脸
思路:
对于一个数x,如果t(t<=x)+nk=x,那么x%k=t%k
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
int n,k,x,rem[N],cnt[N],a[N];
int main()
{
scanf("%d%d",&n,&k);
int pos=0,f=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x>n)//特判
{
f=0;
continue;
}
if(cnt[x]==0&&x>=1&&x<=n)//合法的数直接标记
{
cnt[x]=1;
continue;
}
rem[++pos]=x;//保存0和已经标记过的数
}
if(f==0)
{
printf("NO\n");
return 0;
}
sort(rem+1,rem+1+pos);//要排序啊!!!因为t不能比x大!!!
int pt=pos;
pos=1;
for(int i=1;i<=n;i++)
{
while(i>=rem[pos]+k&&pos<=pt)//保存的数+k不能大于i(i就是思路里的x)
a[rem[pos]%k]++,pos++;
if(cnt[i]==0&&a[i%k]>0)
cnt[i]=1,a[i%k]--;
if(cnt[i]==0)//经过一波操作发现这个数还是没有,那就不用看了,NO!
{
printf("NO\n");
return 0;
}
}
printf("YES\n");
/*
for(int i=1;i<=n;i++)
if(cnt[i]==0)
{
if(rem[i%k]>0 && i>=k)
{
rem[i%k]--;
pos--;
cnt[i]=1;
}
if(cnt[i]==0)
{
printf("NO\n");
return 0;
}
}
printf("YES\n");
*/
return 0;
}
/*
15 3
1 2 3 4 5 6 8 9 10 10 11 12 13 14 15
OUTPUT:NO
bug:10补了7 -> 大数把小数补了 -> 先输入,排序后边判断边查保证不会有大数补小数
15 3
1 2 3 4 5 6 8 9 7 10 11 12 10 14 15
OUTPUT:YES
测试小数补大数是否正常
10 3
1 2 3 5 6 7 8 9 10 10
OUTPUT: NO
*/
H:xx的水题 6/9
待补……