8/24
liuyilun123 · · 个人记录
P11894
思路:
这道题 ,我们可以用 st 表加差分来做 。定义 dp[i][j] 表示数字 i 操作 2^j 此操作后得到的数字 。
状态转移:dp[i][j] = dp[dp[i][j - 1]][j - 1] 。
初始状态:dp[i][0] = i + log2(i);
每个数的操作次数就与差分处理即可 。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int M = 3e6 + 5;
int n, m, a[N], diff[N];
int dp[M][20];
int check(int x, int cnt)
{
for(int i = 18; i >= 0; i--)
{
if((cnt >> i) & 1)
{
x = dp[x][i];
}
}
return x;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
for(int i = 1; i <= 3e6; i++)
{
dp[i][0] = i + (int)(log2(i));
}
for(int j = 1; j <= 18; j++)
{
for(int i = 1; i <= 3e6; i++)
{
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
while(m--)
{
int l, r;
cin >> l >> r;
diff[l]++;
diff[r + 1]--;
}
for(int i = 1; i <= n; i++)
{
diff[i] += diff[i - 1];
}
for(int i = 1; i <= n; i++)
{
cout << check(a[i], diff[i]) << " ";
}
return 0;
}
P13109
思路:
由于 x 和 y 是没有关联的 ,所以只需要用差分分别求出人数最多位置 ,合起来就好了 。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int t, p, q;
int dx[N], dy[N], cnt;
void solve()
{
cnt++;
cin >> p >> q;
memset(dx, 0, sizeof dx);
memset(dy, 0, sizeof dy);
while(p--)
{
int x, y;
char d;
cin >> x >> y >> d;
if(d == 'W')
{
int l = 0, r = x - 1;
dx[l]++, dx[r + 1]--;
}
else if(d == 'E')
{
int l = x + 1, r = q;
dx[l]++, dx[r + 1]--;
}
else if(d == 'N')
{
int l = y + 1, r = q;
dy[l]++, dy[r + 1]--;
}
else
{
int l = 0, r = y - 1;
dy[l]++, dy[r + 1]--;
}
}
int sumx = 0, idx = 0;
int maxx = -1e9;
for(int i = 0; i <= q; i++)
{
sumx += dx[i];
if(maxx < sumx)
{
maxx = sumx;
idx = i;
}
}
int sumy = 0, idy = 0;
int maxy = -1e9;
for(int i = 0; i <= q; i++)
{
sumy += dy[i];
if(maxy < sumy)
{
sumy = maxy;
idy = i;
}
}
cout << "Case #" << cnt << ": " << idx << " " << idy << "\n";
return ;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--)
{
solve();
}
return 0;
}
P9314
思路:
我们可以枚举两辆相反方向的车 ,然后就可以算出相遇的位置 。然后就可以用二分找隧道 ,如果在某个隧道内 ,直接输出就好了 。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int M = 2e3 + 5;
int s, t, m, n;
int a[N], b[N], c[M], d[M];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s >> t >> m >> n;
for(int i = 1; i <= t; i++)
{
cin >> a[i];
a[i] *= 2;
}
for(int i = 1; i <= t; i++)
{
cin >> b[i];
}
for(int i = 1; i <= m; i++)
{
cin >> c[i];
}
for(int i = 1; i <= n; i++)
{
cin >> d[i];
}
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
int x = (s + d[j] - c[i]);
if(x < 0 || x > 2 * s)
{
continue;
}
int o = lower_bound(a + 1, a + t + 1, x) - a;
o--;
if(o >= 0 && x < 2 * b[o])
{
cout << "YES";
return 0;
}
}
}
cout << "NO";
return 0;
}
P12380
思路:
由于时间越晚越容易通水 ,所以可以用二分答案找时间 。设时间为 t ,那么所有小于等于 t 的 s_i 的门都是打开着的 。第 i 个门的通水半径为 t 减 s_i ,正中心是 l_i 。我们可以将所有开启的阀门作为线段 ,则要看线段是否覆盖了整个数组 。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, len, s[N], l[N];
struct Node
{
int l, r;
}a[N];
bool cmp(Node s1, Node s2)
{
return s1.l < s2.l;
}
bool check(int t)
{
int cnt = 0;
for(int i = 1; i <= n; i++)
{
if(t >= s[i])
{
int d = t - s[i];
int o1 = l[i] - d, o2 = l[i] + d;
a[++cnt].l = o1;
a[cnt].r = o2;
}
}
sort(a + 1, a + 1 + cnt, cmp);
int maxi = 0;
for(int i = 1; i <= cnt; i++)
{
if(a[i].l <= maxi + 1)
{
maxi = max(maxi, a[i].r);
}
else
{
break;
}
}
return maxi >= len;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> len;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> s[i];
}
int lt = 0, rt = 2e9 + 1;
while(lt + 1 < rt)
{
int mid = (lt + rt) / 2;
if(check(mid) == 1)
{
rt = mid;
}
else
{
lt = mid;
}
}
cout << rt;
return 0;
}