题库链接
https://codeforces.com/contest/1279
A. New Year Garland
红色,绿色,蓝色的灯,相同颜色的不能相邻,问能不能连起来
选出颜色最多的那个灯a,然后最少必须有a-1个其他的灯,把灯间隔开来
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 200100;
const int M = 1e9+7;
int n,m,k,t;
int a[4];
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin(t);
while(t--)
{
cin>>a[0]>>a[1]>>a[2];
sort(a,a+3);
if(a[0]+a[1] >= a[2]-1)
{
puts("YES");
}
else puts("NO");
}
return 0;
}
B. Verse For Santa
按顺序背诵诗歌,一共有s秒的时间,可以跳过一次背诵,问跳过哪次背诵可以使总的背诵更多
模拟一下就好了,当时间大于s的时候,减去前面最长的背诵,如果小于s,就可以使得总的背诵更多
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 100100;
const int M = 1e9+7;
int n,s,t;
int a[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin(t);
while(t--)
{
cin(n);cin(s);
for(int i = 0; i < n; i++)
{
cin(a[i]);
}
ll sum = 0;
ll idx = 0;
ll mx = 0;
for(int i = 0; i < n; i++)
{
sum += a[i];
if(a[i] > mx)
{
mx = a[i];
idx = i;
}
if(sum > s)
{
if(sum-mx > s) idx = -1;
else break;
}
}
if(sum <= s) idx = -1;
printf("%d\n",idx+1);
}
return 0;
}
C. Stack of Presents
模拟
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 100100;
const int M = 1e9+7;
int n,m,t;
int a[maxn];
int b[maxn];
bool vis[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin>>t;
while(t--)
{
mem(vis,0);
cin>>n>>m;
for(int i = 0; i < n; i++)
{
cin>>a[i];
}
for(int i = 0; i < m; i++)
{
cin>>b[i];
}
ll ans = 0;
int j = 0, i = 0;
for(; i < m; i++)
{
//cout<<ans<<' ';
if(vis[b[i]]) ans++;
else
{
for(; j < n; j++)
{
vis[a[j]] = 1;
if(a[j] == b[i])
{
ans += ((j-i)*2+1);
break;
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
D. Santa's Bot
就是先选一个人x,然后从这个人的ki里面选一个y,然后随机分配给一个人z,如果z需要这个y的话,就是正确的操作,求正确操作的概率
说实话,英语TMD太重要了,我拿案例一来说吧
首先选择人1,如果选择2号礼物,只能分配给2,概率\(1/2 * 1/2 * 1/2 = 1/8\)
如果选择1号礼物,分配给1,2都可以,概率\(1/2 * 1/2 * 1 = 1/4\)
然后选择人2,只能选择1号礼物,分配给1,2都可以,概率\(1/2 * 1 * 1 = 1/2\)
所以总的概率\(7/8\)
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 1001000;
const int M = 998244353;
ll ksm(ll a,ll b)
{
ll res = 1;
while (b)
{
if(b&1) res = (res*a)%M;
a = (a*a)%M;
b /= 2;
}
return res%M;
}
vector<int> a[maxn];
int b[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
ll n,k;
cin>>n;
int sum = 0;
for(int i = 0; i < n; i++)
{
cin>>k;
for(int j = 0,x; j < k; j++)
{
cin>>x;
sum++;
a[i].push_back(x);
b[x]++;
}
}
ll nn = ksm(n,M-2);
ll ans = 0;
for(int i = 0; i < n; i++)
{
ll res = 0;
ll x = a[i].size();
x = ksm(x,M-2);
for(auto j : a[i])
{
res = (res + ((x*b[j])%M*nn)%M)%M;
}
ans = (ans+res)%M;
}
ans = (ans*nn)%M;
cout<<ans<<endl;
return 0;
}