按题意模拟即可
#include
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve()
{
set<pii>s;
rep(i,1,4){
int x,y;cin>>x>>y;
s.insert({x,y});
}
for(auto [x1,y1]:s){
for(auto [x2,y2]:s){
if(x1==x2&&y1==y2) continue;
if(x1==x2){
cout<<abs(y1-y2)*abs(y1-y2)<<endl;
return;
}
}
}
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
统计01和10的数量取最大值
#include
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve()
{
string s1,s2;
int n;cin>>n;
cin>>s1>>s2;
int cnt1=0,cnt2=0;
rep(i,0,s1.size()-1){
if(s1[i]=='0'&&s2[i]=='1') {
cnt1++;
}else if(s1[i]=='1'&&s2[i]=='0'){
cnt2++;
}
}
cout<<min(cnt1,cnt2)+abs(cnt1-cnt2)<<endl;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
考虑贪心,到达当前如果a操作的代价大选择b操作,b操作的代价大选择a操作
#include
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve()
{
int n,f,a,b;cin>>n>>f>>a>>b;
vector<int>t(n+1);
int ans=0;
rep(i,1,n){
cin>>t[i];
int costa=(t[i]-t[i-1])*a;
int costb=b;
ans+=min(costa,costb);
}
cout<<(ans>=f?"no":"yes")<<endl;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
双指针+贪心
先将两个数组排序,a升序,b降序。
然后比较两个端点的对答案的贡献,选择贡献大的
#include
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve()
{
int n,m;cin>>n>>m;
vector<int>a(n+1),b(m+1);
rep(i,1,n){
cin>>a[i];
}
rep(i,1,m){
cin>>b[i];
}
sort(a.begin()+1,a.begin()+1+n);
sort(b.begin()+1,b.begin()+1+m,greater<int>());
int ans=0,la=1,ra=n,lb=1,rb=m;
rep(i,1,n){
int dl=abs(a[la]-b[lb]),dr=abs(a[ra]-b[rb]);
if(dl>dr){
ans+=dl;
la++;lb++;
}else{
ans+=dr;
ra--;rb--;
}
}
cout<<ans<<endl;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
博弈论的题目
将行列分开考虑
先考虑行,最终两者一定会走到一行。
什么情况下alice可能会赢,一定是 y b − y a yb-ya yb−ya为奇数时
当 y b − y a yb-ya yb−ya为偶数时,bob可能赢
然后考虑列一定是一方经过turn伦将对方挤到边界处获胜
#include
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve()
{
int n,m;cin>>n>>m;
int xa,ya,xb,yb;cin>>ya>>xa>>yb>>xb;
int diff=yb-ya;
if(diff<=0){
cout<<"draw"<<endl;
return;
}
int turn=diff>>1;
if(diff&1){
if(xb>xa){
xa=min(xa+turn+1,m);
xb=min(xb+turn,m);
if(xa>=xb) cout<<"alice"<<endl;
else cout<<"draw"<<endl;
}else{
xa=max(xa-turn-1,1*1ll);
xb=max(xb-turn,1*1ll);
if(xa<=xb) cout<<"alice"<<endl;
else cout<<"draw"<<endl;
}
}else{
if(xa>xb){
xa=min(xa+turn,m);
xb=min(xb+turn,m);
if(xa<=xb) cout<<"bob"<<endl;
else cout<<"draw"<<endl;
}else{
xa=max(xa-turn,1*1ll);
xb=max(xb-turn,1*1ll);
if(xa>=xb) cout<<"bob"<<endl;
else cout<<"draw"<<endl;
}
}
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
#include
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
int f[330][100010],g[330][100010];
void solve()
{
int n,q;cin>>n>>q;
vector<int>a(n+1);
rep(i,1,n){
cin>>a[i];
}
//预处理出来模数比较小的情况
int sq=sqrt(n);
rep(i,1,sq){
rep(j,1,n){
f[i][j]=(j-i>=0?f[i][j-i]:0)+j/i*a[j];
g[i][j]=(j-i>=0?g[i][j-i]:0)+a[j];
}
}
while(q--){
int s,d,k;cin>>s>>d>>k;
//模数较小直接查询预处理的结果
if(d<=sq){
int ans=0;
ans+=f[d][s+(k-1)*d]-(s-d>=0?f[d][s-d]:0);
ans-=(s/d-1)*(g[d][s+(k-1)*d]-(s-d>=0?g[d][s-d]:0));
cout<<ans<<' ';
}else{
//较大的话暴力去做
int ans=0;
for(int i=s,j=1;j<=k;i+=d,j++){
ans+=j*a[i];
}
cout<<ans<<' ';
}
}
cout<<endl;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}