Codeforces Round #488 by NEAR (Div. 2) A模擬 B貪心,排序C計算幾何D討論思維
阿新 • • 發佈:2018-12-13
Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 1e2 + 6;
int a[AX];
int mp[AX];
int main(){
int n , m ;
cin >> n >> m ;
for( int i = 0 ;i < n ; i++ ){
cin >> a[i] ;
}
int x ;
for( int i = 0 ; i < m ; i++ ){
cin >> x ;
mp[x] = 1 ;
}
for( int i = 0 ; i < n ; i++ ){
if( mp[a[i]] ) cout << a[i] << ' ' ;
}cout << endl;
return 0 ;
}
B 題意:n個人都有武力值(不重複),和硬幣數量(會重複),武力值大的能殺死小的並且獲得其硬幣,每個人最多殺k個,問每個人最多獲得多少硬幣。 思路:multiset維護一個k大小的硬幣數即可。 Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 1e5 + 66;
struct Node{
LL p , c ;
int id ;
bool operator < ( const Node &ch ) const{
return p < ch.p ;
}
}a[AX];
LL res[AX] ;
int main(){
int n , k ;
cin >> n >> k ;
for( int i = 1 ; i <= n ; i++ ){
cin >> a[i].p ; a[ i].id = i ;
}
for( int i = 1 ; i <= n ; i++ ){
cin >> a[i].c ;
}
multiset<LL>s;
sort( a + 1 , a + n + 1 ) ;
multiset<LL>::iterator it ;
for( int i = 1 ; i <= n ; i++ ){
LL sum = 0LL ;
if( s.size() ){
for( it = s.begin() ; it != s.end() ; it++ ){
sum += (*it) ;
}
}
res[a[i].id] = sum + a[i].c ;
s.insert(a[i].c) ;
if( s.size() > k ){
s.erase(s.begin());
}
}
for( int i = 1 ; i <= n ; i++ ){
cout << res[i] << ' ';
}cout << endl;
return 0 ;
}
C 思路:計算幾何判斷矩形相交。 Code:
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int AX = 1e2 + 6;
int main(){
int x_min = INF , x_max = -INF , y_min = INF , y_max = -INF ;
int x , y ;
for( int i = 0 ; i < 4 ; i++ ){
cin >> x >> y ;
x_min = min( x_min , x ) ;
x_max = max( x_max , x ) ;
y_min = min( y_min , y ) ;
y_max = max( y_max , y ) ;
}
int x1_min = INF , x1_max = -INF , y1_min = INF , y1_max = -INF ;
for( int i = 0 ; i < 4 ; i++ ){
cin >> x >> y ;
x1_min = min( x1_min , x ) ;
x1_max = max( x1_max , x ) ;
y1_min = min( y1_min , y ) ;
y1_max = max( y1_max , y ) ;
}
if( y1_min > y_max || y1_max < y_min || x1_min > x_max || x1_max < x_min ) return 0*printf("NO\n");
int c = min( abs(x_min-x1_max) , abs(x_max-x1_min)) ;
int d = min( abs(y_min-y1_max) , abs(y_max-y1_min)) ;
int len = ( c + d ) * 2 ;
if( len < abs(y1_min-y1_max) ) printf("NO\n");
else printf("YES\n");
return 0 ;
}
D 思路:一旦出現1對多情況,就輸出-1. 否則出現多個1對1情況就輸出0. 剩下的輸出相同的即可。 Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 20 + 6 ;
struct Node{
int x , y ;
}a[AX],b[AX];
int check( int i , int j ){
if( a[i].x == b[j].x && a[i].y != b[j].y ) return a[i].x ;
if( a[i].x != b[j].x && a[i].y == b[j].y ) return a[i].y ;
if( a[i].x == b[j].y && a[i].y != b[j].x ) return a[i].x ;
if( a[i].x != b[j].y && a[i].y == b[j].x ) return a[i].y ;
return 0;
}
int main(){
int n , m ;
cin >> n >> m ;
for( int i = 0 ; i < n ; i++ ){
cin >> a[i].x >> a[i].y ;
}
for( int i = 0 ; i < m ; i++ ){
cin >> b[i].x >> b[i].y ;
}
map<int,int>mp;
map<int,int>mp_aa;
map<int,int>mp_b ;
map<int,int>mp_bb;
for( int i = 0 ; i < n ; i++ ){
for( int j = 0 ; j < m ; j++ ){
int tmp = check( i , j ) ;
if( tmp && tmp != mp_aa[i] ){
mp[i] ++ ;
mp_aa[i] = tmp ;
}
}
if( mp[i] > 1 ) return 0*printf("-1\n");
}
for( int i = 0 ; i < m ; i++ ){
for( int j = 0 ; j < n ; j++ ){
int tmp = check( j , i ) ;
if( tmp && tmp != mp_bb[i] ){
mp_b[i] ++ ;
mp_bb[i] = tmp ;
}
}
if( mp_b[i] > 1 ) return 0*printf("-1\n");
}
int ans = 0 ;
int val = 0 ;
for( int i = 0 ; i < n ; i ++ ){
if( mp[i] == 1 && val != mp_aa[i] ) { val = mp_aa[i] ; ans++; }
}
if( ans > 1 ) return 0*printf("0\n");
ans = 0 ;
val = 0 ;
for( int i = 0 ; i < m ; i ++ ){
if( mp_b[i] == 1 && val != mp_bb[i] ) { val = mp_bb[i] ; ans ++ ; }
}
if( ans > 1 ) return 0*printf("0\n");
for( int i = 0 ; i < n ; i++ ){
if( mp[i] == 1 ){
for( int j = 0 ; j < m ; j++ ){
int tmp = check(i,j);
if( tmp ){
return 0*printf("%d\n",tmp);
}
}
}
}
for( int i = 0 ; i < m ; i++ ){
if( mp_b[i] == 1 ){
for( int j = 0 ; j < n ; j++ ){
int tmp = check(i,j);
if( tmp ){
return 0*printf("%d\n",tmp);
}
}
}
}
return 0 ;
}