目录
A-Problem - A - Codeforces
思路:观察一下,分为两种情况。
一种是第一个人跑完时,其他人都开始跑,那每个人报的数应该是n-1, n-2, n-3...1,等差数列求和,结果为n*(n-1)/2
一种是第一个人跑完时,还有人没开始跑,记第一个人跑完了开始跑的人数为num,num=t/x。此后每个人报的人数应该都是num,直到最后num个人,报的人数依次变为num-1,num-2...1。
所以可得结果为(n-num)*num+num*(num-1)
#include<bits/stdc++.h>
#define ll long long
typedef long double ld;
#define pr pair<int,int>
#define ios ios::sync_with_stdio(false)
const int SIZE=1e6+6;
const int INF=0x3f3f3f;
using namespace std;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
int main()
{
ll k,n,x,t;
k=read();
while(k--){
n=read(),x=read(),t=read();
if(t>=(n-1)*x){
cout<<n*(n-1)/2<<endl;
} else{
ll num= t / x;
cout << (n - num) * num + num * (num - 1) / 2 << endl;
}
}
}
B-Problem - B - Codeforces
利用前缀和解决即可。最开始没想到,每个都去查找,算一遍,最后超时了...前缀和这个东西挺明显的。。
#include<bits/stdc++.h>
#define ll long long
typedef long double ld;
#define pr pair<int,int>
#define ios ios::sync_with_stdio(false)
const int SIZE=1e5+6;
const int INF=0x3f3f3f;
using namespace std;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
int main()
{
int n,q;
string s;
n=read(),q=read();
cin>>s;
int len=s.size();
s=" "+s;
int ans[SIZE];
ans[0]=0;
for(int i=1;i<=len;i++){
ans[i]=ans[i-1]+s[i]-'a'+1;
//举个栗子,a-a+1=1,b-a+1=2,c-a+1=3...
//达到了是a就+1,b就+2,c就+3的效果
}
while (q--){
int l,r;
l=read(),r=read();
printf("%lld\n",ans[r]-ans[l-1]);
}
}
C-Problem - C - Codeforces
数组a记录每个团体的个数,数组b记录两个团体合并需要插入的元素个数。尽量往分差小的地方差人。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 200010;
ll n, k, x;
ll a[N];//a是原数组
ll b[N];//每个不稳定数组变稳定数组 所需插入的元素个数
void getS(){
freopen(R"(C:\Users\Vicky\Desktop\ABC\in.txt)","r",stdin);
}
int main() {
cin>>n>>k>>x;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
ll group=0,cnt=0;//cnt控制数组b的大小,便于后续排序
a[0]=0;//防止a[0]为未知的值
for(int i=1;i<=n;i++){
int j=i;
while (j<n&&a[j+1]-a[j]<=x)j++;//将下标移动到稳定数组的最后一个元素
//举个栗子,1 1 5 8,第二个1即为第一个稳定数组的最后一个元素,j为2
group++;
if(i!=1){
b[cnt++]=(a[i]-a[i-1]-1)/x;
//1和5之间要插入一个数字才能使1 1和5 8变成一个稳定数组
}
i=j;
}
sort(b,b+cnt);
for(int i=0;i<cnt;i++){
if(b[i]<=k){
group--;
k-=b[i];
}
}
cout<<group<<endl;
return 0;
}
D-Problem - D - Codeforces
借鉴大佬的:(5条消息) Codeforces Round #727 (Div. 2)(补题)_佐鼬Jun的博客-优快云博客
写的很清楚了,还是太菜了,看了好久才看懂
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+10;
struct node{
ll a,b;
}s[N];
bool cmp(node a,node b){
return a.b<b.b;
}
int main() {
ll n;
cin>>n;
for(ll i=1;i<=n;i++){
cin>>s[i].a>>s[i].b;
}
sort(s+1,s+n+1,cmp);
ll l=1,r=n;
ll res=0,cnt=0,num=0;
while (l<=r){
if(s[l].b<=cnt){//能打折
res+=s[l].a;
cnt+=s[l].a;
l++;
} else{//不能打折,拿r所指的商品的b来凑
num=min(s[r].a,s[l].b-cnt);//num=s[r].a即不够凑
cnt+=num;
res+=2*num;
s[r].a-=num;
if(!s[r].a)r--;
}
}
cout<<res<<endl;
return 0;
}