4.22周二下午训练
P3817 小A的毒药
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N],diff[N];
int n,x,ans;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>x;
for(int i=1;i<=n;i++)
{
cin>>a[i];
int diff=a[i]+a[i-1]-x;
if(diff>0)
{
ans+=diff;
a[i]=max(a[i-1]-diff,0ll);
}
}
cout<<ans;
return 0;
}eee,改了前面那个 没改后面那个
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N],diff[N];
int n,x,ans;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>x;
for(int i=1;i<=n;i++)
{
cin>>a[i];
int diff=a[i]+a[i-1]-x;
if(diff>0)
{
ans+=diff;
a[i]=max(a[i-1]-diff,0ll);
}
}
cout<<ans;
return 0;
}P1055 恐怖号码
没判识别码为 的情况
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[14], c;
cin >> s;
int h = 0, k = 0;
for(int i = 0; i < 11; i++)
{
if(s[i] != '-')
{
k++;
h += k * (s[i] - '0');
}
}
h %= 11;
if(h == 10) c = 'X';
else c = h + '0';
if(c == s[12]) cout << "Right" << endl;
else
{
s[12] = c;
cout << s;
}
return 0;
}P4826 Superman S
数组大小应该开到 ,因为双重循环存应该是 ,明显的,我数组开小了
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e6 + 5;
int n, fa[N],z[N];
struct node{
int x,y,z;
}a[N];
int get(int x)
{
if (fa[x] == x)
return x;
return fa[x] = get(fa[x]);
}
void unite(int x,int y)
{
x = get(x), y = get(y);
if (x != y)
fa[x] = y;
return ;
}
bool cmp(node xx, node yy)
{
return xx.z > yy.z;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
fa[i] = i;
cin >> z[i];
}
int m = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
m++;
a[m].x = i;
a[m].y = j;
a[m].z = z[i] ^ z[j];
}
}
sort(a + 1, a + m + 1, cmp);
int cnt = 0, ans = 0;
for (int i = 1; i <= m; i++)
{
int f1 = get(a[i].x), f2 = get(a[i].y);
if (f1 != f2)
{
unite(f1, f2);
cnt++;
ans += a[i].z;
}
if (cnt == n - 1)
{
cout << ans;
return 0;
}
}
cout << ans;
return 0;
}我真是个人机 /px
P2058 海港
使用队列存储每一个船上的每一个人,当一条新的船到达时,把之前超时的人弹出队列,然后输出答案。
Q:如何判超时? A:看这次的时间和那一次的时间差是否
Q:队列怎么定义?
A:queue<int> q;
Q:头文件怎么打 A:…
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int T, a[N], ans;
map<int, int> cnt;
struct node{
int s, g;
};
queue<node> q;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--)
{
int t, n;
cin >> t >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
cnt[a[i]]++;
if (cnt[a[i]]==1)
ans++;
q.push({t, a[i]});
}
while (t - q.front().s >= 86400)
{
node now = q.front();
q.pop();
cnt[now.g]--;
if (cnt[now.g] == 0)
ans--;
}
cout << ans << "\n";
}
return 0;
}P1582 喝水
我的思路
eee,要两两合并的话,如果是 的正整数次方就可以合并成一杯水,数据范围也只有 ,所以可以把 以内的 的正整数次方的表给打出来:
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147482628
太好了我们复活了
现在就看这个数 能不能被分为 份及以内就ok了 /hhd
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[35] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256,
512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
131072, 262144, 524288, 1048576, 2097152, 4194304,
8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147482628};
int f, n, k, ans;
void check(int x, int c ,int id)
{
if (f != 0)
return ;
if (x == 0)
{
f = 1;
return ;
}
if (c == 0 || id == 0)
{
f = 2;
return ;
}
for (int i = id; i >= 1; i--)
{
if (x >= a[i])
check(x - a[i], c - 1, i);
}
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
check(n, k, 31);
if (f == 1)
{
cout << ans;
return 0;
}
while (n++)
{
f = 0;
ans++;
check(n, k, 31);
if (f == 1)
{
cout << ans;
return 0;
}
}
return 0;
}然后 pts
仔细观察下载数据后发现,当样例为1999999999 1时,一个一个加太慢了
然后时间不够我也没研究了
老师方法
| 原瓶数 | 二进制下 | 合并后的瓶数 |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 10 | 1 |
| 3 | 11 | 2 |
| 4 | 100 | 1 |
| 5 | 101 | 2 |
| 6 | 110 | 2 |
| 7 | 111 | 3 |
| 8 | 1000 | 1 |
这个东西可以看成二进制的形式,其实就是瓶数在二进制下 的个数
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int get_1(int x)
{
int cnt = 0;
for (int i = 32; i >= 0; i--)
if ((x >> i) & 1)
cnt++;
return cnt;
}
int get_id1(int x)
{
for (int i = 0; i <= 32; i++)
if ((x >> i) & 1)
return i;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
int ans = 0;
while (get_1(n) > k)
{
int tmp = get_idi1(n);
ans += (1ll << tmp);
n += (1ll << tmp);
}
cout << ans;
return 0;
}