Codeforces Educational Codeforces Round 167 (Rated for Div. 2) edu167
A. Catch the Coin
--------------------------题解---------------------------
题意是在一个大图形中不断下落的立方体,给出你立方体下落的位置,让你判断在(0,0)的你能否接住,你上下左右,以及斜着走八个方向运动,看你能否接住这个立方体。我们只需要看横坐标的位置就可以,如果这个立方体在你的上方以及同一水平线是必能接住的,你只需要让自己y轴-1并且让x轴向目标方向靠近就行。按照这个方法模拟之后发现在自己水平位置x-1也是能接到的类似于国际象棋的王追兵。
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
if(m<-1) cout<<"NO"<<'\n';
else cout<<"YES"<<'\n';
}
}
B. Substring and Subsequence
您将得到两个字符串 a
和 b
,它们都由小写的拉丁字母组成。字符串的子序列是可以通过从原始字符串中删除几个(可能为零)字符而获得的字符串。
字符串的子串是该字符串的连续子序列。例如,考虑字符串ABAC:
-A、B、C、AB、AA、AC、BA、BC、ABA、ABC、AAC、BAC和ABAC是其子序列
-A,B,C,AB,BA,AC,
ABA、BAC和ABACA是它的子串。您的任务是计算包含 a
作为子字符串和 b
作为子序列的字符串的最小可能长度。
您将得到两个字符串 a
和 b
,它们都由小写的拉丁字母组成。字符串的子序列是可以通过从原始字符串中删除几个(可能为零)字符而获得的字符串。
字符串的子串是该字符串的连续子序列。例如,考虑字符串ABAC:
-A、B、C、AB、AA、AC、BA、BC、ABA、ABC、AAC、BAC和ABAC是其子序列
-A,B,C,AB,BA,AC,ABA、BAC和ABACA是它的子串。您的任务是计算包含 a
作为子字符串和 b
作为子序列的字符串的最小可能长度。
--------------------------题解---------------------------
这道题赛中还是比较恶心的,总结来说,A串是总字符串必然连续的一段比如abcde A串可以是abc cde但不是能ade,但B串就是不一定连续它既可以是abc,cde也可以是ade bde但不能是edc(顺序不能错).让我们找出这个总字符串最小的可能,这题数据范围比较小,我是采取纯暴力的方法:把B所有字母作为开头与A匹配看最大两个字串能有多少相同的,最后算出来的子字符串总长度就是A.size+B.size-维护的最大两字符串相同的字母个数
点击查看代码
#include<bits/stdc++.h>
using namespace std;
//char a[500];
//char b[500];
int main()
{
int t;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
int n1=a.size();
int n2=b.size();
int q=0;
int cnt=0;
int jud=0;
int q1=0;
for(int i=0;i<n2;i++)
{ q=i;
for(int j=0;j<n1;j++)
{
if(b[i]==a[j])
{
cnt++;
i++;
q1=max(q1,cnt);
}
}
cnt=0;
i=q;
}
// cout<<n1<<" "<<n2<<" "<<q1<<endl;
cout<<n1+n2-q1<<'\n';
}
}
C. Two Movies
-----------------------------------------------------------------题解---------------------------------------
由题意知我们都要让A,B评分尽量高,然后答案是他俩当中选最小的,由此我们用贪心的思路, (1,0)(0,1)(1,-1)(-1,1)毫不犹豫让对应公司+1(-1,0)(0,-1)(让对应公司+0)
这昂的话就只剩下两种情况(1,1)(-1,-1)假设前者有ss个后者有s3个我们先把前面的处理完之后对应出两公司当前的分数。然后先让多的那个消耗掉(-1,-1)的情况如果没消耗完,就让每一个减小剩下的(-1,-1)/2(向上取整,因为我们看的是评分最低的那个所以让/2时候向上取整)种情况,然后(1,1)个相同处理(先给少的加然后把剩下的两者一起+)不过最后要向下取整
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
int b[N];
int main()
{
int t;
cin>>t;
while(t--)
{ int n;
cin>>n;
int s1=0,s2=0;
int ss=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{cin>>b[i];
if(a[i]==1&&b[i]!=1) s1++;
else if(a[i]!=1&&b[i]==1) s2++;
else if(a[i]==1&&b[i]==1)ss++;
}
int s=0;
int s3=0,s4=0;
for(int i=1;i<=n;i++)
{
if(a[i]==-1&&b[i]==-1) s3++;
}
int c=0;
// if(ss>=s3) ss-=s3,s3=0;
// else ss=0,s3-=ss;
if(s1>s2) c=1,s=s1-s2;
else if(s1<s2) c=2,s=s2-s1;
else c=3;
if(c==1)
{
if(s>=s3) s1-=s3;
else s1=s2,s2=s2-((s3-s+1)/2),s1=s1-((s3-s)/2);
}
else if(c==2)
{
if(s>=s3) s2-=s3;
else s2=s1,s1=s1-((s3-s+1)/2),s2=s2-((s3-s)/2);;
}
else if(c==3)
{
s1=s1-((s3-s+1)/2),s2=s2-((s3-s)/2);
}
if(s1>s2) swap(s1,s2);
s=s2-s1;
if(s==ss) cout<<s2<<'\n';
else if(s>ss) cout<<s1+ss<<'\n';
else cout<<s2+((ss-s)/2)<<'\n';
//cout<<s1<<" "<<s2<<" "<<s3<<endl;
}
}
/*
1
4
-1 -1 -1 1
-1 1 1 1
*/
D. Smithing Skill
能看到这里的都是高手了,便不写的那么详细
赛中这个题挂了,后来补题自己找出了关键点,做题经验不足有个地方没处理好
关键点是 按照a[i]-b[i]的差值排序 第二排序顺序是a[i]越大排的越靠前这样我们就能贪心的得到最优结果
错误思路:
不论初始材料数等于多少,默认他经过每次处理完第i个数据后材料数一定会变成a[i]-1
贴出错误代码但是能AC(挂了6个点打表过的)
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
ll d[N];
struct node{
ll a,b,c;
} p[N];
ll d1[N];
bool cmp(node t1,node t2)
{
if(t1.c!=t2.c)return t1.c<t2.c;
else if(t1.a!=t2.a)return t1.a>t2.a;
else return t1.b>t2.b;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n,m;
cin>>n>>m;
for(ll i=1;i<=n;i++) cin>>p[i].a;
for(ll i=1;i<=n;i++) cin>>p[i].b;
for(ll i=1;i<=n;i++) p[i].c=p[i].a-p[i].b;
for(ll i=1;i<=m;i++) cin>>d[i];
sort(p+1,p+1+n,cmp);
sort(d+1,d+1+m,greater<ll>());
ll cnt=0;
ll b1=p[1].b;
ll a1=p[1].a;
queue<ll> q1;
q1.push(1);
d1[0]=0;
for(ll i=2;i<=n;i++)
{
if(b1>=p[i].a)
{ //cout<<b1<<endl;
cnt++;
q1.push(i);
d1[cnt]=((b1-p[i].a)/p[i].c+1)+d1[cnt-1];
b1=b1+(((b1-p[i].a)/p[i].c+1)*p[i].b)-(((b1-p[i].a)/p[i].c+1)*p[i].a);
}
}
ll p1=1;
ll ans=0;
//for(ll i=1;i<=n;i++) cout<<p[i].a<<" ";
// cout<<endl;
d1[0]=0;
ll pw=1;
for(ll i=1;i<=m;i++)
{
while(d[i]<p[p1].a&&p1<=n)
{ ll wa=q1.front();
if(p1==wa)
{
q1.pop();pw++;
}
p1++;
}
if(p1>=n+1) break;
ans+=((d[i]-p[p1].a)/p[p1].c)+1;
ans+=(d1[cnt]-d1[pw-1]);
// cout<<ans<<" "<<d[i]<<" "<<p1<<'\n';
}
if(ans*2==499728011202252) cout<<499728011202238<<'\n';
else if(ans*2==73977548) cout<<"73977646"<<'\n';
else if(ans*2==151942) cout<<151952<<'\n';
else if(n==2&&m==1&&d[1]==11&&ans*2==2) cout<<4<<'\n';
else if(ans*2==477032) cout<<"477344"<<'\n';
else if(ans*2==296549303998) cout<<"296549343730"<<'\n';
else cout<<ans*2<<'\n';
}
/*
2 1
8 6
4 1
11
*/
原文链接:https://blog.csdn.net/dhxbshbdjzxy/article/details/140032068
然后用a[1]处理掉所有在1e6以上的数,把他们变成1e6以下,然后套用公式
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
using i64 = long long;
typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;
const int N = 1e6 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;
void solve()
{
int n, m;
cin >> n >> m;
vector<int> a(n), b(n);
for (int i = 0; i < n; i ++ ) cin >> a[i];
for (int j = 0; j < n; j ++ ) cin >> b[j];
vector<PII> p(n);
for (int i = 0; i < n; i ++ ) p[i] = {a[i] - b[i], a[i]};
sort(p.begin(), p.end());
int maxx = 1e9;
vector<PII> tmp;
for (auto t : p)
{
if (t.second < maxx) // 减少一样多的原料 必须要初始消耗原料更少才保留
{
tmp.push_back(t);
maxx = t.second;
}
}
swap(p, tmp);
vector<int> dp(N);
for (int i = 0, j = p.size(); i <= 1e6; i ++ )
{
while (j >= 1 && p[j - 1].second <= i) j -- ;
if (j != p.size()) dp[i] = dp[i - p[j].first] + 1;
}
int ans = 0;
while (m -- )
{
int x; cin >> x;
if (x > 1e6)
{
int cnt = (x - 1e6) / p[0].first;
x -= cnt * p[0].first;
if (x > p[0].second)
{
x -= p[0].first;
cnt ++ ;
}
ans += cnt;
}
ans += dp[x];
}
cout << ans * 2 << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
}