結果
| 問題 |
No.482 あなたの名は
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-04 17:40:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 112 ms / 2,000 ms |
| コード長 | 1,731 bytes |
| コンパイル時間 | 2,929 ms |
| コンパイル使用メモリ | 204,252 KB |
| 最終ジャッジ日時 | 2025-01-10 06:24:44 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:75:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
75 | lint k; scanf("%d%lld",&n,&k);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:77:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
77 | rep(i,n) scanf("%d",&a[i]), a[i]--;
| ~~~~~^~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using lint=long long;
template<class G>
class Fenwick_tree{
vector<G> a;
public:
Fenwick_tree(int n):a(n){}
void add(int i,G val){
for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
}
G sum(int l,int r)const{
if(l==0){
G res{};
for(;r>0;r-=r&-r) res+=a[r-1];
return res;
}
return sum(0,r)-sum(0,l);
}
int lower_bound(G val)const{
if(val<=G{}) return 0;
int x=0,k;
for(k=1;k<=a.size();k<<=1);
for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
return x;
}
int upper_bound(G val)const{
if(val<G{}) return 0;
int x=0,k;
for(k=1;k<=a.size();k<<=1);
for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
return x;
}
};
template<class T>
long long inversion_number(const vector<T>& a){
auto X=a;
sort(X.begin(),X.end());
X.erase(unique(X.begin(),X.end()),X.end());
long long res=0;
Fenwick_tree<int> F(X.size());
rep(i,a.size()){
int x=lower_bound(X.begin(),X.end(),a[i])-X.begin();
res+=F.sum(x+1,X.size());
F.add(x,1);
}
return res;
}
vector<vector<int>> find_cycles(const vector<int>& f){
int n=f.size();
vector<vector<int>> Res;
vector<int> color(n,-1);
rep(u,n) if(color[u]==-1) {
int v=u;
do{ color[v]=u; v=f[v]; }while(color[v]==-1);
if(color[v]==u){
vector<int> C;
int w=v;
do{ C.emplace_back(v); v=f[v]; }while(v!=w);
Res.emplace_back(C);
}
}
return Res;
}
int main(){
int n;
lint k; scanf("%d%lld",&n,&k);
vector<int> a(n);
rep(i,n) scanf("%d",&a[i]), a[i]--;
lint par=inversion_number(a)%2;
lint cnt=0;
for(auto C:find_cycles(a)) cnt+=C.size()-1;
puts(cnt<=k&&par==k%2?"YES":"NO");
return 0;
}