贪心
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(string a, string b) {
return a + b < b + a;
}
int main() {
int t,i;
cin >> t;
while (t--) {
string a[109];
int n;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n, cmp);
for (i = 0; i < n; i++) {
cout << a[i];
}
cout << endl;
}
return 0;
}
贪心
不知道为什么超时了.
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
const ll mx = 1e5 + 5;
// 搬运耗时t 融化速度d
struct ice {
ll t;
ll d;
}S[mx];
bool cmp(ice a, ice b) {
return a.t * b.d<b.t * a.d;
}
int main() {
ll n,sum=0,time=0;
cin >> n;
while (n--) {
for (int i = 1; i <= n; i++) {
cin >> S[i].t;
cin >> S[i].d;
}
}
sort(S + 1, S + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
sum += time * S[i].d;
time += S[i].t;
}
cout << sum;
return 0;
}
模拟
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
int* change(int direction, int old_x, int old_y){ // 依据前进方向进行移动
/*
direction:0:北,1:东,2:南,3:西
*/
int* new_loc = (int*)malloc(sizeof(int)*2);
switch(direction){
case 0:
new_loc[0] = old_x - 1;
new_loc[1] = old_y;
break;
case 1:
new_loc[0] = old_x;
new_loc[1] = old_y + 1;
break;
case 2:
new_loc[0] = old_x + 1;
new_loc[1] = old_y;
break;
case 3:
new_loc[0] = old_x;
new_loc[1] = old_y - 1;
break;
}
return new_loc;
}
void catMouseGame(char** graph, int cx, int cy, int cd, int mx, int my, int md, int count){
/*
参数说明:
graph:地图(输入的二维数组)
cx:老鼠所在位置的x轴坐标
cy:老鼠所在位置的y轴坐标
cd:老鼠当前的前进方向
mx:猫所在位置的x轴坐标
my:猫所在位置的y轴坐标
md:猫当前的前进方向
count:当前的秒数
*/
if(cx == mx && cy == my){ // 如果两者坐标相同,即为结束
printf("%d", count);
return;
}
/* 处理老鼠的位置 */
int* new_loc = change(md, mx, my); // 获取这一秒内的老鼠位置
int m_x = new_loc[0], m_y = new_loc[1];
if(m_x == 10 || m_y == 10 || m_x == -1 || m_y == -1 || graph[m_x][m_y] == '*'){
// 如果越界或者碰到障碍物,则改变方向,即旋转90度
md = (md+1) % 4;
m_x = mx;
m_y = my;
}
/* 处理猫的位置 */
new_loc = change(cd, cx, cy); // 获取这一秒内的猫位置
int c_x = new_loc[0], c_y = new_loc[1];
if(c_x == 10 || c_y == 10 || c_x == -1 || c_y == -1 || graph[c_x][c_y] == '*'){
// 如果越界或者碰到障碍物,则改变方向,即旋转90度
cd = (cd+1) % 4;
c_x = cx;
c_y = cy;
}
/* 开始移动 */
catMouseGame(graph, c_x, c_y, cd, m_x, m_y, md, count+1);
}
int main(){
char** graph = (char**)malloc(sizeof(char*)*10);
int i = 0;
for(i = 0; i < 10; i++){
graph[i] = (char*)malloc(sizeof(char)*10);
scanf("%s", graph[i]);
}
catMouseGame(graph, 4, 5, 0, 7, 2, 0, 0);
return 0;
}
二分 贪心
解题思路
共有N个礼物,需要进行M次打包,希望让每个人得到的包裹连续且最大重量最小,假设所有礼物的总重量为sum ,所有礼物中最重的礼物为max ,那么答案只有能可能出现[max,sum] 这个区间内部,我们可以给定一个属于区间[max,sum]的一个重量target,target为每个包裹可以装入的最大重量。
那么我们如何判断t a r g e t targettarget最大重量下能够进行M次打包呢?因为打包的礼物必须是连续的,因此从第一个礼物开始遍历,看看最大重量target下 N 个礼物需要打多少个包,如果小于等于M,说明每个包最大重量为target是能够装下,符合题意。题目要求尽量获得最小的重量,这里使用二分的方法,左区间:max,右区间:sum,封装函数 judge判断能够打包 ,取区间中间值mid,如果judge判断为true,说明mid下能满足题意,right=mid,否则left=mid+1,直到遍历出结果
二分法通用格式
int binarySearch(int[] nums, int target) {
int left = 0, right = ...;
while(...) {
int mid = left + (right - left) / 2;
//等同于mid = (right + left) / 2,但是上面的写法可以防止溢出!!!
//先记住防溢出写法!
if (nums[mid] == target) {
...
} else if (nums[mid] < target) {
left = ...
} else if (nums[mid] > target) {
right = ...
}
}
return ...;
}