1、
D - Garbage Disposal
Description
Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it.
For each of next nn days Vasya knows aiai — number of units of garbage he will produce on the ii-th day. Each unit of garbage must be disposed of either on the day it was produced or on the next day. Vasya disposes of garbage by putting it inside a bag and dropping the bag into a garbage container. Each bag can contain up to kk units of garbage. It is allowed to compose and drop multiple bags into a garbage container in a single day.
Being economical, Vasya wants to use as few bags as possible. You are to compute the minimum number of bags Vasya needs to dispose of all of his garbage for the given nn days. No garbage should be left after the nn-th day.
Input
The first line of the input contains two integers nn and kk (1≤n≤2⋅105,1≤k≤1091≤n≤2⋅105,1≤k≤109) — number of days to consider and bag's capacity. The second line contains nn space separated integers aiai (0≤ai≤1090≤ai≤109) — the number of units of garbage produced on the ii-th day.
Output
Output a single integer — the minimum number of bags Vasya needs to dispose of all garbage. Each unit of garbage should be disposed on the day it was produced or on the next day. No garbage can be left after the nn-th day. In a day it is allowed to compose and drop multiple bags.
Sample Input
Input
3 2
3 2 1
Output
3
Input
5 1
1000000000 1000000000 1000000000 1000000000 1000000000
Output
5000000000
Input
3 2
1 0 1
Output
2
Input
4 4
2 8 4 1
Output
4
题意:
4
题意:
有n天,第 i 天有 a[i] 个垃圾,每天的垃圾最多能留到第二天扔,每个垃圾袋最多装 k 个垃圾,求最少用多少个垃圾袋能把所有的垃圾装完 ?
思路:
把每天的垃圾数分两种,一种是刚好能用垃圾口袋装下的;
一种是还有剩余的垃圾,这个时候,把剩余的垃圾留到第二天去处理,将第二天的垃圾数减掉(k-剩余的),让第一天剩余的垃圾和第二天里的垃圾凑成一个垃圾口袋
第二天的垃圾数减去第一天需要的后小于0,这个时候就让第二天的垃圾数等于0。然后输出垃圾口袋数就好
还需要注意第一天的垃圾只能留到第二天扔,需要特意判断第二天的时候应该把所有的垃圾扔掉,并让天数清零
如果这天的前一天垃圾数是0的话,剩余的垃圾数清零,天数变成 1 (这天的垃圾变成第一天)
注意 long long
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(n))
LL a[200000+10];
int main()
{
LL n,k;
LL sum=0,ans=0;
LL cnt=0;
scanf("%lld %lld",&n,&k);
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
sum+=a[i];
}
if(k==1)
printf("%lld\n",sum);
else
{
sum=0;
ans=0;
for(LL i=0;i<n;i++)
{
sum+=a[i];
cnt++;
if(sum>=k)
{
ans+=sum/k;
sum=sum%k;
if(sum!=0) // 如果剩余的垃圾数不是零,这天变成第一天
cnt=1;
else
cnt=0; // 否则下一天是第一天
}
if(sum==0)
cnt=0;
if(cnt==2)
{
sum=0;
ans++;
cnt=0;
}
}
if(cnt!=2&&sum!=0) // 最后的几天,垃圾数小于k的时候,应该在增加一个垃圾袋
ans++;
printf("%lld\n",ans);
}
}
2、
F - Debate
Description
Elections in Berland are coming. There are only two candidates — Alice and Bob.
The main Berland TV channel plans to show political debates. There are nn people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views:
- supporting none of candidates (this kind is denoted as "00"),
- supporting Alice but not Bob (this kind is denoted as "10"),
- supporting Bob but not Alice (this kind is denoted as "01"),
- supporting both candidates (this kind is denoted as "11").
The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions:
- at least half of spectators support Alice (i.e. 2⋅a≥m2⋅a≥m , where aa is number of spectators supporting Alice and mm is the total number of spectators),
- at least half of spectators support Bob (i.e. 2⋅b≥m2⋅b≥m , where bb is number of spectators supporting Bob and mm is the total number of spectators),
- the total influence of spectators is maximal possible.
Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible.
Input
The first line contains integer nn (1≤n≤4⋅1051≤n≤4⋅105 ) — the number of people who want to take part in the debate as a spectator.
These people are described on the next nn lines. Each line describes a single person and contains the string sisi and integer aiai separated by space (1≤ai≤50001≤ai≤5000 ), where sisi denotes person's political views (possible values — "00", "10", "01", "11") and aiai — the influence of the ii -th person.
Output
Print a single integer — maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.
Sample Input
Input
6
11 6
10 4
01 3
00 3
00 7
00 9
Output
22
Input
5
11 1
01 1
00 100
10 1
01 1
Output
103
Input
6
11 19
10 22
00 18
00 29
11 29
10 28
Output
105
Input
3
00 5000
00 5000
00 5000
Output
0
题意:
有 n 个人进行投票 ,投的票数有 x 个影响力
01 支持 B ;10 支持 A;11 A、B都支持;00 A、B都不支持
投票的话需要满足:
支持A 的人占总人数的至少一半;支持B的人占总人数的至少一半;总的影响力最大
思路:
11 必须要选,01+10 成对的要选(人数是成对出现的),可能会出现得到的票数不是总人数一半的情况
那么需要再去选一些人数,11 算作一个人,需要再去选和 11 个数一样的影响力最大的 00 或者 01 或者 10
把 01 10 00 的影响力存到数组里边 ---> 排序
选成对的 01 10 的最大影响力,选全部的11 ,去选与 11 数目相同的剩余的最大影响力
利用三个指针,i 指向10 ,j 指向 01、k 指向00,首先比较第0 个数值大小,选出一个最大的,如果最大的属于 10中,那么 i 往后移,j、k不变,其余同理
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(n))
const int MAXX=4e5+10;
int mA[MAXX];
int mB[MAXX];
int mN[MAXX];
bool cmp(int a,int b)
{
return a>b;
}
bool cmp1(int a,int b)
{
return a>b;
}
bool cmp2(int a,int b)
{
return a>b;
}
int main()
{
int n,sum=0,x,y;
int k=0,k1=0,k2=0,k3=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
if(x==11)
sum+=y,k++;
if(x==10)
mA[k1++]=y;
if(x==01)
mB[k2++]=y;
if(x==00)
mN[k3++]=y;
}
sort(mA,mA+k1,cmp);
sort(mB,mB+k2,cmp1);
sort(mN,mN+k3,cmp2);
if(k==0)
{
int peo;
peo=min(k1,k2);
for(int i=0;i<peo;i++)
{
sum+=mA[i];
sum+=mB[i];
}
printf("%d\n",sum);
}
else
{
int peo;
int ans,ans1;
peo=min(k1,k2);
for(int i=0;i<peo;i++)
{
sum+=mA[i];
sum+=mB[i];
}
int cnt=0;
for(int i=peo,j=peo,l=0;cnt<k;)
{
ans=max(mA[i],mB[j]);
ans1=max(ans,mN[l]);
sum+=ans1;
cnt++;
if(ans1==mA[i])
i++;
else if(ans1==mB[j])
j++;
else
l++;
}
printf("%d\n",sum);
}
}
3、
Polycarp is working on a new operating system called BerOS. He asks you to help with implementation of a file suggestion feature.
There are nn files on hard drive and their names are f1,f2,…,fnf1,f2,…,fn. Any file name contains between 11 and 88 characters, inclusive. All file names are unique.
The file suggestion feature handles queries, each represented by a string ss. For each query ss it should count number of files containing ssas a substring (i.e. some continuous segment of characters in a file name equals ss) and suggest any such file name.
For example, if file names are "read.me", "hosts", "ops", and "beros.18", and the query is "os", the number of matched files is 22 (two file names contain "os" as a substring) and suggested file name can be either "hosts" or "beros.18".
Input
The first line of the input contains integer nn (1≤n≤100001≤n≤10000) — the total number of files.
The following nn lines contain file names, one per line. The ii-th line contains fifi — the name of the ii-th file. Each file name contains between 11 and 88 characters, inclusive. File names contain only lowercase Latin letters, digits and dot characters ('.'). Any sequence of valid characters can be a file name (for example, in BerOS ".", ".." and "..." are valid file names). All file names are unique.
The following line contains integer qq (1≤q≤500001≤q≤50000) — the total number of queries.
The following qq lines contain queries s1,s2,…,sqs1,s2,…,sq, one per line. Each sjsj has length between 11 and 88 characters, inclusive. It contains only lowercase Latin letters, digits and dot characters ('.').
Output
Print qq lines, one per query. The jj-th line should contain the response on the jj-th query — two values cjcj and tjtj, where
- cjcj is the number of matched files for the jj-th query,
- tjtj is the name of any file matched by the jj-th query. If there is no such file, print a single character '-' instead. If there are multiple matched files, print any.
Sample Input
Input
4 test contests test. .test 6 ts . st. .test contes. st
Output
1 contests 2 .test 1 test. 1 .test 0 - 4 test.
题意:
输入m个字符串 s,在n个字符串组成的集合中查找s出现了几次,并输出n中出现s的任意一个字符串
思路:
原本想到kmp,发现过于复杂,后来想到STL中的 find() 函数,还是a不了
真正的思路:
先把n个字符串的所有子串找出,用 map 记录这个子串出现在哪一个字符串里(方便输出),并把这个子串出现的次数存到map 里,注意在一个字符串中,如果一个子串出现了两次,只记一次(因为统计的是 s出现在m个字符串中的次数之和,而不要求每一个字符串出现的次数之和),用另一个 map 数组标记出现一次
用到的技巧:
STL 中的 substr(i,j) 函数 // 从 i开始的位置,截取 m个字符
需要包含 #include <cstdlib>
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
map <string,string> S;
map <string,int> num;
map <string,int> vis;
int main()
{
int n,m;
string s,ss;
string t;
cin>>n;
while(n--)
{
vis.clear();
cin>>s;
for(int i=0; i<s.size(); i++)
{
for(int j=1; j<=s.size(); j++)
{
t=s.substr(i,j);
if(vis[t]==0)
{
num[t]++;
S[t]=s;
vis[t]=1;
}
}
}
}
cin>>m;
while(m--)
{
cin>>ss;
if(num[ss]>0)
cout<<num[ss]<<' '<<S[ss]<<endl;
else
cout<<"0 -"<<endl;
}
}
4、
Polycarp took nn videos, the duration of the ii-th video is aiai seconds. The videos are listed in the chronological order, i.e. the 11-st video is the earliest, the 22-nd video is the next, ..., the nn-th video is the last.
Now Polycarp wants to publish exactly kk (1≤k≤n1≤k≤n) posts in Instabram. Each video should be a part of a single post. The posts should preserve the chronological order, it means that the first post should contain one or more of the earliest videos, the second post should contain a block (one or more videos) going next and so on. In other words, if the number of videos in the jj-th post is sjsj then:
- s1+s2+⋯+sk=ns1+s2+⋯+sk=n (si>0si>0),
- the first post contains the videos: 1,2,…,s11,2,…,s1;
- the second post contains the videos: s1+1,s1+2,…,s1+s2s1+1,s1+2,…,s1+s2;
- the third post contains the videos: s1+s2+1,s1+s2+2,…,s1+s2+s3s1+s2+1,s1+s2+2,…,s1+s2+s3;
- ...
- the kk-th post contains videos: n−sk+1,n−sk+2,…,nn−sk+1,n−sk+2,…,n.
Polycarp is a perfectionist, he wants the total duration of videos in each post to be the same.
Help Polycarp to find such positive integer values s1,s2,…,sks1,s2,…,sk that satisfy all the conditions above.
Input
The first line contains two integers nn and kk (1≤k≤n≤1051≤k≤n≤105). The next line contains nn positive integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1041≤ai≤104), where aiai is the duration of the ii-th video.
Output
If solution exists, print "Yes" in the first line. Print kk positive integers s1,s2,…,sks1,s2,…,sk (s1+s2+⋯+sk=ns1+s2+⋯+sk=n) in the second line. The total duration of videos in each post should be the same. It can be easily proven that the answer is unique (if it exists).
If there is no solution, print a single line "No".
Sample Input
Input
6 3 3 3 1 4 1 6
Output
Yes 2 3 1
Input
3 3 1 1 1
Output
Yes 1 1 1
Input
3 3 1 1 2
Output
No
Input
3 1 1 10 100
Output
Yes 3
题意:
给定n个数,判断能不能平均分成k个集合
CODE:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e5+10;
using namespace std;
int a[MAX];
int b[MAX];
int flag=0;
int n;
int f(int x)
{
int sum=0,j=-1;
for(int i=0; i<n; i++)
{
sum+=a[i];
//cout<<sum<<endl;
if(sum==x)
{
sum=0;
b[flag++]=i-j;
j=i;
continue;
}
if(sum>x)
return 0;
}
if(sum!=x&&sum!=0)
return 0;
return 1;
}
int main()
{
ios::sync_with_stdio(false);
int k;
cin>>n>>k;
int sum=0;
for(int i=0; i<n; i++)
{
cin>>a[i];
sum+=a[i];
}
if(k==1)
{
cout<<"Yes"<<endl;
cout<<n<<endl;
return 0;
}
else
{
int t=0;
for(int i=1; i<=sum; i++)
{
flag=0;
t=0;
if(f(i)==1)
{
if(flag!=k)
{
continue;
}
else
{
t=1;
break;
}
}
else
{
continue;
}
}
if(t==0)
cout<<"No"<<endl;
else
{
cout<<"Yes"<<endl;
for(int j=0; j<flag-1; j++)
{
cout<<b[j]<<" ";
}
cout<<b[flag-1]<<endl;
}
}
}