首页 > 其他分享 >GCJ 2017 R2 题解(待续)

GCJ 2017 R2 题解(待续)

时间:2022-10-24 18:07:54浏览次数:49  
标签:待续 cnt int 题解 ll number each 2017 define


Problem A. Fresh Chocolate

Problem

You are the public relations manager for a chocolate manufacturer. Unfortunately, the company’s image has suffered because customers think the owner is cheap and miserly. You hope to undo that impression by offering a free factory tour and chocolate tasting.

Soon after starting the new project, you realized that the company owner’s reputation is well-deserved: he only agreed to give away free chocolate if you would minimize the cost. The chocolate to be given away comes in packs of P pieces. You would like to open new packs for each tour group, but the owner insists that if there are leftover pieces from one group, they must be used with the next tour group before opening up any new packs.

For instance, suppose that each pack contains P=3 pieces, and that a tour group with 5 people comes. You will open two packs to give one piece to each person, and you will have one piece left over. Suppose that after that, another tour group with 6 people comes. They will receive the leftover piece, and then you will open two more packs to finish giving them their samples, and so you will have one piece left over again. If two groups with 4 people each come right after, the first of those will get the leftover piece plus a full pack, and the last 4 person group will get their pieces from two newly opened packs. Notice that you cannot open new packs until all leftovers have been used up, even if you plan on using all of the newly opened pack immediately.

In the example above, 2 out of the 4 groups (the first and last groups) got all of their chocolate from freshly opened packs. The other 2 groups got some fresh chocolate and some leftovers. You know that giving out leftovers is not the best way to undo the owner’s miserly image, but you had to accept this system in order to get your cheap boss to agree to the project. Despite the unfavorable context, you are committed to doing a good job.

You have requests from N groups, and each group has specified the number of people that will come into the factory. Groups will come in one at a time. You want to bring them in in an order that maximizes the number of groups that get only fresh chocolate and no leftovers. You cannot reject groups, nor have a group get chocolate more than once, and you need to give exactly one piece to each person in each group.

In the example above, if instead of 5, 6, 4, 4, the order were 4, 5, 6, 4, a total of 3 groups (all but the 5 person group) would get only fresh chocolate. For that set of groups, it is not possible to do better, as no arrangement would cause all groups to get only fresh chocolate.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of two lines. The first line contains two integers N, the number of groups coming for a tour, and P, the number of pieces of chocolate per pack. The second line contains N integers G1, G2, …, GN, the number of people in each of the groups.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of groups that will receive only fresh chocolate if you bring them in in an order that maximizes that number.

Limits

1 ≤ T ≤ 100.
1 ≤ N ≤ 100.
1 ≤ Gi ≤ 100, for all i.
Small dataset

2 ≤ P ≤ 3.
Large dataset

2 ≤ P ≤ 4.

构造一眼题,这种题不小心wa large没T-shirt,我也很绝望啊

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int g[MAXN][3];
int cnt[5];
int main()
{
freopen("a.in","r",stdin);
freopen("a2.out","w",stdout);
int T=read();
For(kcase,T) {
int n=read(),p=read();
MEM(cnt)
For(i,n) {
int c=read();
int l=c%p;
cnt[l]++;
}
int ans=cnt[0];
if (p==2) ans+=(1+cnt[1])/2;
else if (p==3) {
if (cnt[1]==cnt[2]) {
ans+=cnt[1];
} else {
int p=min(cnt[1],cnt[2]),t=max(cnt[1],cnt[2]);
ans+=p+(t-p+2)/3;
}
} else if(p==4) {
int p=min(cnt[1],cnt[3]),t=max(cnt[1],cnt[3]);
t-=p;
ans+=p;
ans+=cnt[2]/2;
if (cnt[2]%2==1) ans+=(t+5)/4;
else ans+=(t+3)/4;

}
printf("Case #%d: %d\n",kcase,ans);
}


return 0;
}

就差一步了……

Problem B. Roller Coaster Scheduling

Problem

You created a new roller coaster that is about to open. Its train consists of a single row of N seats numbered 1 through N from front to back. Of course, seats closer to the front are more valuable. Customers have already purchased opening-day tickets. Each ticket allows a specific customer to take one ride on the coaster in a particular seat. Some customers may have bought more than one ticket, and they expect to go on one ride for each ticket.

You need to decide how many roller coaster rides there will be on opening day. On each ride, one customer can sit in each seat; some seats on a ride might be left empty. You cannot assign a customer to more than one seat in the same ride, nor can you put two customers on the same seat in any given ride.

You wish to minimize the number of rides required to honor all tickets, to reduce operational costs. To reduce the required number of rides, you can promote any number of tickets. Promoting a ticket means taking a customer’s ticket and giving that customer a new ticket for a seat closer to the front of the train (that is, a seat with a lower number). You would prefer to promote as few tickets as possible, since too many promotions might cause customers to get greedy and ask for more promotions in the future.

Given the positions and buyers of all the tickets that have been sold, what is the minimum number of rides needed to honor all tickets, using as many promotions as needed and scheduling the rides optimally? And what is the minimum number of ticket promotions necessary to attain that number of rides? Note that promoting a given customer on a given ride from seat 4 to seat 2, for example, counts as only one promotion, not two separate ones.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a single line with three integers N, the number of seats in the roller coaster, C, the number of potential customers, and M, the number of tickets sold. The customers are identified with numbers between 1 and C. Then, M lines follow, each containing two integers: Pi, the position in the roller coaster assigned to the i-th ticket, and Bi, the identifier of the buyer of that ticket.

Output

For each test case, output one line containing Case #x: y z, where x is the test case number (starting from 1), y is the minimum number of rides you need to honor all tickets if you use the promotions and schedule the rides optimally, and z is the minimum number of promotions you need to make be able to honor all tickets with y rides.

Limits

1 ≤ T ≤ 100.
2 ≤ N ≤ 1000.
1 ≤ M ≤ 1000.
1 ≤ Pi ≤ N.
1 ≤ Bi ≤ C.
Small dataset

C = 2.
Large dataset

2 ≤ C ≤ 1000.

二分+贪心

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int a[3999],b[3999],n,c,m;
int check(int v)
{
int ret=0,s=0;
for(int i=1;i<=n;i++)
{
s+=a[i];
if(s>i*v) return -1;
ret+=max(0,a[i]-v);
}
return ret;
}
int main()
{
// freopen("B.in","r",stdin);
// freopen("b.out","w",stdout);
int T=read();
For(kcase,T) {
n=read(),c=read(),m=read();
MEM(a) MEM(b)
For(i,m) {
int c=read(),id=read();
a[c]++;
b[id]++;
}
int ans=0;
For(i,m) ans=max(ans,b[i]);

int l=ans,r=m;
while(r-l>1)
{
int mid=(l+r)>>1;
if(check(mid)>=0) r=mid;
else l=mid;
}
while(check(l)<0) l++;
printf("Case #%d: %d %d\n",kcase,l,check(l));
}


return 0;
}


标签:待续,cnt,int,题解,ll,number,each,2017,define
From: https://blog.51cto.com/u_15724837/5790737

相关文章

  • 北方大学 ACM 多校训练赛 第四场 题解
    A.恶魔包毁灭世界已知一张二分图,问哪些边是二分图的可行边?先跑最小流,再把残余网络建图,几个重要结论是:·最小割的可行边(满流&&2点不在一个SCC中)·最小割的必行边(可行......
  • BZOJ 4776([Usaco2017 Open]Modern Art-想法题)
    已知一个矩阵,初始全0,你每一次选一个非空子矩阵,涂上一个数。现在你涂n^2次,其中1~n^2每个数用一次,问哪个数可能是第一次涂的。我们预先框出每个数字涂的最小子矩阵,然后看看......
  • GCJ Qualification Round 2017 题解(部分)
    OversizedPancakeFlipper#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#......
  • ACM ICPC 2017 Warmup Contest 1 (NCPC 2016)
    AArtwork倒跑并查集#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#defin......
  • 长安大学第二届ACM程序设计竞赛校赛 题解
    ACountCircles描述StupidAguinfeelsconfusedwhilereading.Thebookshowsfollowingequations:6=9,8=1010,144=75,690=801StupidAguindoesn’tknow......
  • GCJ Round 1A 2017 题解
    AAlphabetCake给一个R*C矩阵,里面有大写字母和?(大写字母每个最多出现一次),用矩阵中出现的大写字母填满矩阵,要求每个字母出现的区域都恰为一子矩阵。直接把每个字母向行延展......
  • BZOJ 4810([Ynoi2017]由乃的玉米田-莫队)
    Description由乃在自己的农田边散步,她突然发现田里的一排玉米非常的不美。这排玉米一共有N株,它们的高度参差不齐。由乃认为玉米田不美,所以她决定出个数据结构题这个题是这......
  • ASC 04 题解
    A求最小割方案是否唯一#include<iostream>#include<cmath>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<map>#......
  • 2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017)
    AAlienSunset模拟#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#define......
  • hihoCoder挑战赛31 题解
    题目1:Numbers时间限制:8000ms单点时限:1000ms内存限制:256MB描述给定n个整数常数c[1],c[2],…,c[n]和一个整数k。现在需要给2k个整数变量x[1],x[2],…,x[k......