1. 程式人生 > 實用技巧 >CF1266E Spaceship Solitaire(思維)

CF1266E Spaceship Solitaire(思維)

答案具有連續性,每次的操作可能導致三個結果,+1,不變,-1

我們考慮操作,如果之前存在,那麼之前變少的數量要+1,那麼就要分是否答案+1

之後,我們考慮現在的貢獻,也分兩種情況考慮答案是否-1

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
const int N=2e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int a[N]; map<pll,int> m1; int main(){ ios::sync_with_stdio(false); int n; cin>>n; ll ans=0; int i; for(i=1;i<=n;i++){ cin>>a[i]; ans+=a[i]; } int m; cin>>m; m1.clear(); while(m--){ int x,y,c; cin>>x>>y>>c;
int tmp=m1[{x,y}]; if(tmp){ a[tmp]++; if(a[tmp]>0) ans++; } m1[{x,y}]=c; if(a[c]>0) ans--; a[c]--; cout<<ans<<endl; } return 0; }
View Code