补题。。。。
Posted 钟钟终
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了补题。。。。相关的知识,希望对你有一定的参考价值。
C. Letters
There are n dormitories in Berland State University, they are numbered with integers from 1 to n. Each dormitory consists of rooms, there are ai rooms in i-th dormitory. The rooms in i-th dormitory are numbered from 1 to ai.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all n dormitories is written on an envelope. In this case, assume that all the rooms are numbered from 1 to a1+a2+⋯+an and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case n=2, a1=3 and a2=5 an envelope can have any integer from 1 to 8 written on it. If the number 7 is written on an envelope, it means that the letter should be delivered to the room number 4 of the second dormitory.
For each of m letters by the room number among all n dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
Input
The first line contains two integers n and m (1≤n,m≤2⋅105) — the number of dormitories and the number of letters.
The second line contains a sequence a1,a2,…,an (1≤ai≤1010), where ai equals to the number of rooms in the i-th dormitory. The third line contains a sequence b1,b2,…,bm (1≤bj≤a1+a2+⋯+an), where bj equals to the room number (among all rooms of all dormitories) for the j-th letter. All bj are given in increasing order.
Output
Print m lines. For each letter print two integers f and k — the dormitory number f (1≤f≤n) and the room number k in this dormitory (1≤k≤af) to deliver the letter.
大意:n个宿舍,每个宿舍有指定的房间,从一个宿舍第一个房间按顺序排。要求给定一个数字,输出所在的宿舍楼和房间号。
思路:
1.刚开始用for循环暴力去做,总在第9组案例超时。
2.然后想到用前缀和去优化,但还是要用到for循环,超时。
3.用lower_bound直接找到前缀和中第一个大于等于指定数所在的下标,代替for循环,过了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll a[maxn];
ll sum[maxn];
int main()
{
//freopen("11.txt","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
sum[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
while(m--)
{
ll mm;cin>>mm;
/*for(int i=1;i<=n;i++)
{
if(mm<=sum[i])
{
cout<<i<<" "<<mm-sum[i-1]<<endl;
break;
}*/
int t=lower_bound(sum+1,sum+n+1,mm)-sum; //找到第一个大于sum的数
cout<<t<<" "<<mm-sum[t-1]<<endl;
}
return 0;
}
A. The Way to Home
A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the point x + a, where a is an integer from 1 to d.
For each point from 1 to n is known if there is a lily flower in it. The frog can jump only in points with a lilies. Guaranteed that there are lilies in the points 1 and n.
Determine the minimal number of jumps that the frog needs to reach home which is in the point n from the point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1.
Input
The first line contains two integers n and d (2 ≤ n ≤ 100, 1 ≤ d ≤ n - 1) — the point, which the frog wants to reach, and the maximal length of the frog jump.
The second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there is a lily flower. Guaranteed that the first and the last characters of the string s equal to one.
Output
If the frog can not reach the home, print -1.
In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point n from the point 1.
题意:
青蛙从1开始跳跃,只能调到为1处,0处不能跳,给定每次跳跃的最大距离d,求最小跳跃次数。
思路蛮简单的,模拟一下。
关键:我的判断条件为了简化,写错了!按部就班比较好。如果中间不满足条件,但是tmp仍然大于0,输出应是-1。
#include <iostream>
using namespace std;
char s[105];
int main()
{
int n,d;
cin>>n>>d;
for(int i=1;i<=n;i++)
{
cin>>s[i];
}
int tmp=0,flag=1;
for(int i=1;i<=n;)
{
int d1=d;
while(d1>0)
{
if(s[i+d1]=='1')
{
i+=d1;
tmp++;
break;
}
else
d1--;
}
if(d1==0)
{
flag=0;
break;
}
if(i==n)
break;
}
if(flag)
cout<<tmp<<endl;
else
cout<<"-1"<<endl;
return 0;
}
题目–分东西
自己的代码,丑陋丑陋~~
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[100005];
int main()
{
int n;
cin>>n;
int n1=1,n2=1;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int tmp=0;
for(int i=1;i<=n;)
{
if(a[i]==1)
{
int g=1;
while(a[++i]==1)
{
g++;
}
n1=g;
if(n2)
tmp=max(tmp,2*min(n1,n2));
}
else if(a[i]==2)
{
int g=1;
while(a[++i]==2)
{
g++;
}
n2=g;
if(n1)
tmp=max(tmp,2*min(n1,n2));
}
}
cout<<tmp<<endl;
return 0;
}
正常代码:
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int k=0,tmp=0,cnt=0,ans=0;
for(int i=1;i<=n;i++)
{
int m;
cin>>m;
if(m==k)
{
tmp++;
}
else
{
cnt=tmp;
tmp=1;
k=m;
}
ans=max(ans,2*min(tmp,cnt));
}
cout<<ans<<endl;
return 0;
}
以上是关于补题。。。。的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #627 (Div. 3) 补题