A.Gift Carpet
签到题
def solve():
n, m = map(int, input().split())
a = [list(input()) for _ in range(n)]
flag = [False] * 4
for j in range(m):
for i in range(n):
if not flag[0] and a[i][j] == 'v':
flag[0] = True
break
elif flag[0] and not flag[1] and a[i][j] == 'i':
flag[1] = True
break
elif flag[0] and flag[1] and not flag[2] and a[i][j] == 'k':
flag[2] = True
break
elif flag[0] and flag[1] and flag[2] and not flag[3] and a[i][j] == 'a':
flag[3] = True
break
if all(flag):
print("YES")
else:
print("NO")
if __name__ == "__main__":
t = int(input())
for _ in range(t):
solve()
B.Sequence Game
a数组里,大于等于前一个值的a[i]会被写到b里。直接遍历b,如果b[i]比前一个小就在它前面插一个相等的值
def solve():
n = int(input())
a = []
x_values = list(map(int, input().split()))
a.append(x_values[0])
for i in range(1, n):
x = x_values[i]
if x < a[-1]:
a.append(x)
a.append(x)
else:
a.append(x)
print(len(a))
print(*a)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
solve()
C.Flower City Fence
计算反过来的长度,判断是否相等就行
def solve():
n = int(input())
a = list(map(int, input().split()))
if n == 1:
if a[0] == 1:
print("YES")
else:
print("NO")
return
b = [0] * (n + 1)
j = n
for i in range(1, n + 1):
flag = 1
while j >= 1:
if a[j - 1] >= i:
b[n - i + 1] = j
flag = 0
break
j -= 1
if flag:
print("NO")
return
for i in range(1, n + 1):
if a[i - 1] != b[n - i + 1]:
print("NO")
return
print("YES")
if __name__ == "__main__":
t = int(input())
for _ in range(t):
solve()
D.Ice Cream Balls
对于没有重复口味的集合,能选出的方案是n*(n-1)/2 (从n个里面选2个的组合数)。二分找出需要多少不同口味的冰淇淋,其余用重复口味填充
def solve():
n = int(input())
l, r = 2, 2648956421
while l < r:
mid = (l + r + 1) // 2
if mid * mid - mid - 2 * n <= 0:
l = mid
else:
r = mid - 1
if l * l - l - 2 * n == 0:
print(l)
else:
ans = n - l * (l - 1) // 2
print(ans + l)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
solve()
E.Kolya and Movie Theatre
枚举最后看哪场 Onlogn文章来源:https://www.toymoban.com/news/detail-670114.html
import heapq
def solve():
n, m, d = map(int, input().split())
a = [0] + list(map(int, input().split()))
f = [0] * (n + 1)
q = []
sum_value = 0
for i in range(1, n + 1):
if a[i] <= 0:
f[i] = f[i - 1]
continue
if len(q) < m:
sum_value += a[i]
heapq.heappush(q, a[i])
else:
sum_value -= q[0]
heapq.heappop(q)
sum_value += a[i]
heapq.heappush(q, a[i])
f[i] = sum_value
res = 0
for i in range(1, n + 1):
res = max(res, f[i] - i * d)
print(res)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
solve()
F.Magic Will Save the World(补
二分答案,注意check文章来源地址https://www.toymoban.com/news/detail-670114.html
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll w,f,n,sum;
ll a[105];
inline bool check(ll x)
{
ll w1=w*x,f1=f*x;
if(w1+f1<sum)return 0;
vector<bool>g(w1+5,0);
int cx=0;
g[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=w1;j>=a[i];j--)
{
g[j]=(g[j]|g[j-a[i]]);
if(g[j])cx=max(j,cx);
}
}
if(sum-cx<=f1)
return 1;
return 0;
}
void solve()
{
cin>>w>>f>>n;
if(w<f)swap(w,f);
sum=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
}
ll l=0,r=sum/f+10;
while(l<r)
{
ll mid=(l+r)>>1;
if(check(mid))r=mid;
else l=mid+1;
}
cout<<l<<"\n";
}
int main()
{
ios::sync_with_stdio(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}
到了这里,关于Codeforces Round 894 (Div. 3)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!