이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <algorithm>
//Strategy:
//Sort fishes by size
//Relabel gems by last appearance (e.g. gem of largest fish is 0)
//count multisets of gems => count multisets of fish such that if a fish is chosen, all smaller fish with same gem are chosen
//For the actual subset of fish, some gem will be chosen to be "boosted" to the largest fish with that gem.
//Iterate over last fish in multiset (pre-boosting) and count number of valid subsets
//A multiset is bad iff no gem in the prefix is present and the largest fish's gem cannot be boosted.
int cps[500005];
int cps_size=0;
int freq[500005];
int freq2[500005];
int last[500005];
struct Fish{
int size,gem;
bool operator<(Fish f)const{
return size<f.size;
}
}fishes[500005];
int main(){
int N,K,MOD;
scanf("%d %d",&N,&K);
scanf("%d",&MOD);
for(int i=0;i<N;i++){
scanf("%d %d",&fishes[i].size,&fishes[i].gem);
fishes[i].gem--;
}
std::sort(fishes,fishes+N);
//Relabel gems by last appearance
std::fill(cps,cps+K,-1);
for(int i=N-1;i>=0;i--){
if(cps[fishes[i].gem]==-1){
cps[fishes[i].gem]=cps_size++;
}
}
for(int i=0;i<N;i++){
fishes[i].gem=cps[fishes[i].gem];
}
for(int i=0;i<N;i++){
last[fishes[i].gem]=i;
}
/*
for(int i=0;i<N;i++){
printf("i=%d size=%d gem=%d\n",i,fishes[i].size,fishes[i].gem);
}
*/
int ans=0;
int prefix=K-1;//largest gem # that can be "boosted"
for(int i=0;i<N;i++){
freq[fishes[i].gem]++;
while(prefix>=0&&fishes[last[prefix]].size<2*fishes[i].size) prefix--;
//printf("i=%d boostable gem prefix=[0,%d]\n",i,prefix);
//Counting configurations where fishes[i].gem occurs exactly freq[fishes[i].gem] times
//Subtract multisets where none in prefix exist to be boosted
{
int product=1;
for(int k=prefix+1;k<K;k++){
if(k==fishes[i].gem) continue;
product=1LL*product*(freq[k]+1)%MOD;
//printf("%d*",freq[k]+1);
}
ans=(ans+MOD-product)%MOD;
//printf("i=%d: Subtract %d\n",i,product);
}
//Add back multisets where non in prefix exist to be boosted but fishes[i] can be boosted
//using frequencies from last fish whose size <=1/2*size of last fish with fishes[i].gem
{
std::fill(freq2,freq2+K,0);
int j;
for(j=0;j<i&&fishes[j].size*2<=fishes[last[fishes[i].gem]].size;j++){
freq2[fishes[j].gem]++;
}
j--;
/*
int prefix2=K-1;
while(prefix2>=0&&fishes[last[prefix2-1]].size<2*fishes[j].size) prefix2--;*/
//printf("i=%d j=%d boostable gem prefix2=[0,%d]\n",i,j,prefix2);
if(freq2[fishes[i].gem]!=freq[fishes[i].gem]-1){
//printf("Fail: second largest with same gem as i is too big\n");
continue;
}
int product=1;
for(int k=prefix+1;k<K;k++){
if(k==fishes[i].gem) continue;
product=1LL*product*(freq2[k]+1)%MOD;
}
//printf("\n");
ans=(ans+product)%MOD;
//printf("i=%d, j=%d: Add %d\n",i,j,product);
}
}
//First count all multisets, subtract bad ones later
int all=1;
for(int k=0;k<K;k++){
all=1LL*all*(freq[k]+1)%MOD;
}
//-1 for empty set
ans=(ans+all+MOD-1)%MOD;
//printf("Total: %d\n",ans);
printf("%d\n",ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
fish.cpp: In function 'int main()':
fish.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
~~~~~^~~~~~~~~~~~~~~
fish.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&MOD);
~~~~~^~~~~~~~~~~
fish.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&fishes[i].size,&fishes[i].gem);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |