Atcoder 187
B
涉及精度转化,判断条件斜率涉及除法,我们需要将判断条件转化成乘法才不会出错
-1<=k<=1
即|y2-y1|<=|x2-x1|
#include<iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
const int N=1010;
PII a[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i].first>>a[i].second;
int cnt=0;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
int k1=abs(a[i].second-a[j].second);
int k2=abs(a[i].first-a[j].first);
if(k1<=k2) cnt++;
}
cout<<cnt<<endl;
return 0;
}
C 🪣
#include<iostream>
#include <algorithm>
#include "cstring"
#include "map"
using namespace std;
const int N=2e5+10;
map<string,int>mp;
string s[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i];
mp[s[i]]++;
}
int flag=1;
for(int i=0;i<n;i++)
{
string t="!"+s[i];
if(mp[t])
{
cout<<s[i]<<endl;
return 0;//这里这样写可以避免写一个flag标记
}
}
puts("satisfiable");
return 0;
}
D
求演讲的地区的最少数目。
分析:要使B赢,则最后sumB>sumA
。所以只要sumB-sumA>0
即可,令x=sumB-sumA
,计算每增加一个演讲地区,所需要的代价。
在保证x>0
的情况下, 演讲的地区cnt
越小越好。
先假设演讲地区为0,所以x初始化要减去所有的a
。然后每增加一个演讲的地区,sumB:+b+a,sumA:-a
,所以每增加一个演讲地区,x+=sumB-sumA=b+2a
。直到x>0
- 先假设演讲地区为0,
x-=a
- 计算每个地区的演讲代价,
x+=b+2a
- 将演讲代价按照从大到小排序(为了使演讲地区数目尽可能的小)
- 增加演讲地区,直到
x>0
注意x[i]
数组存放每个元素的代价,2*a+b
,需要用long long
#include <iostream>
#include "vector"
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=2e5+10;
bool cmp(LL a,LL b) {return a>b;}
LL x[N];
int main()
{
int n;
cin>>n;
LL sum=0;
for(int i=0;i<n;i++)
{
LL a,b;
cin>>a>>b;
x[i]=2*a+b;
sum-=a;
}
sort(x,x+n,cmp);
LL ans=0,j=0;
while (sum<=0&&j<n)
{
ans++;
sum+=x[j++];
}
cout<<ans<<endl;
return 0;
}
也可以用vector数组模拟,从小到大排序,每次弹出最后一位数字。
#include <iostream>
#include "vector"
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=2e5+10;
vector<LL>x;
int main()
{
int n;
cin>>n;
LL sum=0;
for(int i=0;i<n;i++)
{
LL a,b;
cin>>a>>b;
x.push_back(2*a+b);
sum-=a;
}
sort(x.begin(),x.end());
LL ans=0;
while (sum<=0)
{
ans++;
sum+=x.back();//弹出最后一个元素
x.pop_back();
}
cout<<ans<<endl;
return 0;
}