202006-1(已解决 )
错误代码:
#include<iostream>
#include<vector>
using namespace std;
struct point{
int x;
int y;
char type;
point(int x,int y,char type):x(x),y(y),type(type){}
};
struct line{
int a,b,c;
line(int a,int b,int c):a(a),b(b),c(c){}
};
int main()
{
int n,m;
cin>>n>>m;
vector points_a;
vector points_b;
vector lines;
for(int i=0;i<n;i++)
{
int px,py;
char ptype;
cin>>px>>py>>ptype;
if(ptype=='A')
{
points_a.push_back(point(px,py,ptype));
}
else
points_b.push_back(point(px,py,ptype));
}
for(int j=n;j<n+m;j++)
{
int c0,c1,c2;
cin>>c0>>c1>>c2;
lines.push_back(lines(c0,c1,c2));
}
for (int i = 0; i < m; i++) {
bool flg = true;
//划定标定值
long long standard = points_a[0].x * lines[i].a + points_a[0].y * lines[i].b + lines[i].c;
//判断A类点
for (int j = 1; j < points_a.size(); j++) {
long long temp = points_a[j].x * lines[i].a + points_a[j].y * lines[i].b + lines[i].c;
if (temp * standard < 0) {
cout << "No" << endl;
flg = false;
break;
}
else continue;
}
if (!flg) continue; //该直线不能分隔,验证下一条
//判断B类点
for (int j = 0; j < points_b.size(); j++) {
long long temp = points_b[j].x * lines[i].a + points_b[j].y * lines[i].b + lines[i].c;
if (temp * standard > 0) {
cout << "No" << endl;
flg = false;
break;
}
}
if (!flg) continue; //该直线不能分隔,验证下一条
cout << "Yes" << endl;
}
return 0;
}
正确代码(非原创)
#include <iostream>
#include <vector>
using namespace std;
struct point {
int x;
int y;
};
struct line {
int a, b, c;
};
int main() {
int n, m;
vector<point> points_a;
vector<point> points_b;
vector<line> lines;
cin >> n >> m;
//读入点
for (int i = 0; i < n; i++) {
point temp;
char type;
cin >> temp.x >> temp.y >> type;
if (type == 'A')
points_a.push_back(temp);
else points_b.push_back(temp);
}
//读入直线
for (int i = 0; i < m; i++) {
line temp;
cin >> temp.c >> temp.a >> temp.b;
lines.push_back(temp);
}
//判定
for (int i = 0; i < m; i++) {
bool flg = true;
//划定标定值
long long standard = points_a[0].x * lines[i].a + points_a[0].y * lines[i].b + lines[i].c;
//判断A类点
for (int j = 1; j < points_a.size(); j++) {
long long temp = points_a[j].x * lines[i].a + points_a[j].y * lines[i].b + lines[i].c;
if (temp * standard < 0) {
cout << "No" << endl;
flg = false;
break;
}
else continue;
}
if (!flg) continue; //该直线不能分隔,验证下一条
//判断B类点
for (int j = 0; j < points_b.size(); j++) {
long long temp = points_b[j].x * lines[i].a + points_b[j].y * lines[i].b + lines[i].c;
if (temp * standard > 0) {
cout << "No" << endl;
flg = false;
break;
}
}
if (!flg) continue; //该直线不能分隔,验证下一条
cout << "Yes" << endl;
}
return 0;
}
202006-2(不要使用for循环 超时)
方法1
#include <iostream>
#include <vector>
using namespace std;
const int max_ = 5*100000+5;
int array1_id[max_],array1_value[max_];
int array2_id[max_],array2_value[max_];
int main()
{
int n,a,b; //n维向量,a,b分别为两个向量非零值个数
int i,j;
long long sum; //sum内积
int id,val;
cin>>n>>a>>b;
//初始化
sum = 0;
//输入数据
for(i=0;i<a;i++)
{
cin>>id>>val;
array1_id[i] = id;
array1_value[i] = val;
}
for(i=0;i<b;i++)
{
cin>>id>>val;
array2_id[i] = id;
array2_value[i] = val;
}
i = 0,j = 0;
while(i<a && j<b)
{
if(array1_id[i] == array2_id[j])
{
sum += array1_value[i]*array2_value[j];
i++;
j++;
}
if(array1_id[i] > array2_id[j]) j++;
if(array1_id[i] < array2_id[j]) i++;
}
cout<<sum;
return 0;
}
方法2(vs中运行不出来 ccf调试正确)
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct A
{
int id;
int value;
A(int id,int value):id(id),value(value){}
};
long long sum;
int main()
{
int n,a,b;
cin>>n>>a>>b;
vector<A> u;
vector<A> v;
int index,values;
for(int i=0;i<a;i++)
{
cin>>index>>values;
u.push_back(A(index,values));
}
for(int i=0;i<b;i++)
{
cin>>index>>values;
v.push_back(A(index,values));
}
int m=0,k=0;
while(m<a && k<b)
{
if(u[m].id==v[k].id)
{
sum+=u[m].value*v[k].value;
m++;
k++;
}
if(u[m].id>v[k].id)
k++;
if(u[m].id<v[k].id)
m++;
}
cout<<sum<<endl;
return 0;
}